Hi:
SAS functions resolve from the innermost function call to the outermost function call. So your LEFT function isn't being applied
until after the PUT using $DIAG.
You have a couple of choices as shown in the progam below. If DIAG is a numeric variable, then you could just
make a numeric format for diagnosis codes and that would simplify everything (see NDIAG format and the CC_FROM_NUM
variable in the output.
Putting the LEFT in a different position works (CC_LEFT variable) also, using STRIP, COMPRESS or SCAN
functions return the desired results. However, the TRIM function does NOT work, because you need to remove ALL
blanks or have all blanks disregarded, not just the trailing blanks.
cynthia
The program:
[pre]
data diagcode;
infile datalines;
input name $ diag;
return;
datalines;
Alan 72
Bob 359
Carla 3388
Dana 23995
;
run;
proc format;
value $diag '72'='Tennis Elbow'
'359' = 'Migraine'
'3388' = 'Imaginary Voices'
'23995' = 'Text Messaging Thumb';
value ndiag 72 = 'Tennis Elbow'
359 = 'Migraine'
3388 = 'Imaginary Voices'
23995 = 'Text Messaging Thumb';
run;
options nodate nonumber nocenter;
title 'Use Diag Format';
data putdiag;
set diagcode;
cc_orig= LEFT(PUT(put(DIAG,5.),$diag.));
** break your original creation into 3 steps;
** you can see that the 2nd PUT of the character value;
** does NOT result in the desired code because;
** the numbers are still right justified when the;
** second PUT is performed. The LEFT function does not;
** have an effect after the second PUT.;
orig_1=put(DIAG,5.);
orig_2 = PUT(orig_1,$diag.);
orig_3 = left(orig_2);
* use a numeric format instead of a character format;
cc_from_num = put(diag,ndiag.);
** if you do use a character format, make sure to use;
** functions that result in your "trailing" spaces being ;
** ignored or disregarded for the put of $DIAG.;
** note how the LEFT function is used AFTER the first put;
** and BEFORE the second PUT (nested functions resolve from the inside out);
cc_left = put(left(put(diag,8.)),$diag.);
cc_str = put(strip(put(diag,8.)),$diag.);
cc_comp = put(compress(put(diag,8.)),$diag.);
cc_scan = put(scan(put(diag,8.),1),$diag.);
** trim does not produce the desired results;
cc_trim = put(trim(put(diag,8.)),$diag.);
run;
options linesize=200;
proc print data=putdiag;
run;
[/pre]
The output:
[pre]
Use Diag Format
Obs name diag cc_orig orig_1 orig_2 orig_3 cc_from_num cc_left cc_str cc_comp cc_scan cc_trim
1 Alan 72 72 72 72 72 Tennis Elbow Tennis Elbow Tennis Elbow Tennis Elbow Tennis Elbow 72
2 Bob 359 359 359 359 359 Migraine Migraine Migraine Migraine Migraine 359
3 Carla 3388 3388 3388 3388 3388 Imaginary Voices Imaginary Voices Imaginary Voices Imaginary Voices Imaginary Voices 3388
4 Dana 23995 Text Messaging Thumb 23995 Text Messaging Thumb Text Messaging Thumb Text Messaging Thumb Text Messaging Thumb Text Messaging Thumb Text Messaging Thumb Text Messaging Thumb 23995
[/pre]