data range; *set pf_lemon; length score_lbl $9. sort_order 8.; score_lnl="500<=score<540"; sort_order=1; output; score_lnl="540<=score<560"; sort_order=2; output; score_lnl="560<=score<580"; sort_order=3; output; score_lnl="580<=score<600"; sort_order=5; output; run; proc sort data=range; by score_lnl;run;
I tried this but is it how it suppose to be used? do I add a set statement in to include data set I am looking at? I find this very strange.
Please, from now on, be specific about the desired output. Do not show us the wrong output and then not state what the correct/desired output is (which is what you did). In this case, I can guess. In many other cases, having us guess is not a good way to get the answer you need.
If you want the results sorted by the variable sort_order, then you need to tell PROC SORT to sort by that variable.
proc sort data=range; by sort_order; run;
Your program does not produce the results you display. I ran your program:
data range;
*set pf_lemon;
length score_lbl $9. sort_order 8.;
score_lnl="500<=score<540"; sort_order=1; output;
score_lnl="540<=score<560"; sort_order=2; output;
score_lnl="560<=score<580"; sort_order=3; output;
score_lnl="580<=score<600"; sort_order=5; output;
run;
proc sort data=range; by score_lnl;run;
proc print;run;
The proc print produces this:
The SAS System 23:12 Friday, March 4, 2022 8 score_ sort_ Obs lbl order score_lnl 1 1 500<=score<540 2 2 540<=score<560 3 3 560<=score<580 4 5 580<=score<600
which is not the order you display.
Now, I don't recommend using a variable like score_1n1 for sorting purposes, because, as a character variable, it will use lexicographic order. That is, a value like.
'80<=score<=95'
WILL FOLLOW values like
'500<=score<540'.
Better to use the sort_order variable you created.
Hi Mark,
I generally agree with you recommendation to not to use "score_1n1" like variables for sorting, but just to remind, there is "numeric_collation=on" option available.
data test;
score_lnl='500<=score<540'; output;
score_lnl='80<=score<=95'; output;
score_lnl='1500<=score<1540'; output;
run;
proc SORT
data=test
out =test2
SORTSEQ=LINGUISTIC(NUMERIC_COLLATION=ON);
by score_lnl;
run;
proc print;
run;
Bart
Great tip, @yabwon. I keep forgetting about that option myself.
... As do I (obviously). Thanks @yabwon
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.