BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8
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. 

HeatherNewton_0-1646567116060.png

 

 

5 REPLIES 5
PaigeMiller
Diamond | Level 26

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;

 

--
Paige Miller
mkeintz
PROC Star

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.

 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
yabwon
Onyx | Level 15

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

_______________
Polish SAS Users Group: www.polsug.com and communities.sas.com/polsug

"SAS Packages: the way to share" at SGF2020 Proceedings (the latest version), GitHub Repository, and YouTube Video.
Hands-on-Workshop: "Share your code with SAS Packages"
"My First SAS Package: A How-To" at SGF2021 Proceedings

SAS Ballot Ideas: one: SPF in SAS, two, and three
SAS Documentation



PaigeMiller
Diamond | Level 26

Great tip, @yabwon. I keep forgetting about that option myself.

--
Paige Miller
mkeintz
PROC Star

... As do I (obviously).  Thanks @yabwon 

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 611 views
  • 3 likes
  • 4 in conversation