data range; length score_lbl $15. sort_order 8.; score_lbl="score<540"; sort_order=1; output; score_lbl="540<=score<550"; sort_order=2; output; score_lbl="550<score<560"; sort+order=3;output; run;
how does this work? assign value to score_lbl and sort_order in turn and output together?
Maxim 1: Read the Documentation.
Your head will not explode, and you won't lose your eyesight or suffer similar mishaps. Once you've done that, and there are parts of the doc which are unclear to you, come back here, and we'll clear them up.
And, of course, Maxim 4. Just run the code and look at the resulting dataset.
@HeatherNewton wrote:
data range; length score_lbl $15. sort_order 8.; score_lbl="score<540"; sort_order=1; output; score_lbl="540<=score<550"; sort_order=2; output; score_lbl="550<score<560"; sort+order=3;output; run;how does this work? assign value to score_lbl and sort_order in turn and output together?
It doesn't work, because you have no SET statement or INPUT statement. All values are missing.
Of course, if we take a step back, the text string of SCORE_LBL has to be sorted to get them into order, because SAS by default sort text alphabetically, not numerically. It would be far better if SCORE were kept as a numeric value and not converted into a text variable, and then the values of score will sort numerically. How to use SCORE as a numeric variable? Assign custom formats, rather the creating a new text variable.
proc format;
value scoref low-<540='Score < 540'
540-<550='540<=Score<550'
550-<560='550<=Score<560';
run;
/* Example of use */
proc freq data=have order=internal;
table score;
format score scoref.;
run;
So don't assign ranges as text strings; assign ranges as a custom format.
proc format; value SCOREFMT low-539="SCORE<540" 540-559="540<=SCORE<560" ; RUN; DATA RANGE; LENGTH SCORE_LBL $25. SORT_ORDER 8.; SCORE_LBL="SCORE<540"; SORT_ORDER=1; OUTPUT; SCORE_LBL="540<=SCORE<560"; SORT_ORDER=2; OUTPUT; RUN; PROC PRINT DATA=RANGE; RUN;
actually this is the code.
when print, I get score_lbl instead of score as variable name,
it shows score_lbl and sort_order not score
how does this work?
proc format;
value scoref low-<540='Score < 540'
540-<550='540<=Score<550'
550-<560='550<=Score<560';
run;
/* Example of use */
proc print data=range(drop=score_lbl);
format score scoref.;
run;
It is just creating a dataset with three observations and two variables.
So it is just an overly complicated way of writing this data step.
data range;
sort_order +1 ;
input score_lbl $15. ;
cards;
score<540
540<=score<550
550<score<560
;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
Learn how use the CAT functions in SAS to join values from multiple variables into a single value.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.