Using put and proc format to create a new column in SAS

602 views Asked by At

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

There are 2 answers

0
Tom On

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.

0
Stu Sztukowski On

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