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

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

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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