0
votes

I have a following code:

  visitnum = put(visitn,visitnumber.);

which is directed to

  proc format;
   value visitnumber
  'Screening Assessment' = 1
  'Treatment Visit - Day 1' = 2
  'Treatment Visit - Day 7' = 3
  'Treatment Visit - Day 14/15' = 4
  'Follow-Up Visit - Day 28' = 5
 run;

However when I output the 'visitnum' variable, it gives the 'Treatmen' as the output for the matching columns.

2
Are you sure that your code is correct? If you are converting numeric to character, the equals signs should be flipped. e.g. 1 = 'Screening Assessment'Stu Sztukowski
I understand what you mean however in this case it really is character to numeric encoding.AishwaryaKulkarni
Note if you really want to move from character to numeric you need to use an INFORMAT, since you are using a FORMAT it is still character to character.Tom
What is the length of the VISITN variable? Did you define the length of the new VISITNUM variable before the assignment statement? You might want to include an other category in your format definition to set the value to return when the input value is not on the list.Tom

2 Answers

2
votes

If this is character to numeric, you need to use an informat rather than a format.

proc format;
   invalue visitnumber
      'Screening Assessment'        = 1
      'Treatment Visit - Day 1'     = 2
      'Treatment Visit - Day 7'     = 3
      'Treatment Visit - Day 14/15' = 4
      'Follow-Up Visit - Day 28'    = 5
  ;
 run;

Example:

data foo;
    length visitn $27.;

    do visitn = 'Screening Assessment'
              , 'Treatment Visit - Day 1'
              , 'Treatment Visit - Day 7'
              , 'Treatment Visit - Day 14/15'
              , 'Follow-Up Visit - Day 28'
    ;
        visitnum = input(visitn, visitnumber.);
        output;
    end;
run;

Output:

visitn                        visitnum
Screening Assessment          1
Treatment Visit - Day 1       2
Treatment Visit - Day 7       3
Treatment Visit - Day 14/15   4
Follow-Up Visit - Day 28      5
0
votes

Your description of the result appears to indicate that the input string did not match any of the values the format knows about. In that case the PUT() function will just echo the input. Truncation could occur if the width of the format specification used in the PUT() function call is shorter than the length of the input string. Or truncation could happen if the target variable VISITNUM is defined shorter than the string returned by the PUT() function.

So either the value of VISITN does not match any of the values the FORMAT knows about or the format was not found.