BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
jackmelbourne
Fluorite | Level 6

Hello everyone,

 

I was wondering how to first sort by one variable then, keeping that sorting fixed, sort by another variable. For example, first sorting by NET then sorting by AGE as below

HAVE
====

Up to 40 obs WORK.HAVE total obs=4

Obs    NAME   AGE  NET  REG  LOSS

 1     home   300  200   50     .
 2     home   100  300    .     5
 3     TDR     80  500    .     3
 4     TDR    100  200    5     .

WANT
====
Obs    NAME   AGE  NET  REG  LOSS

1 TDR 100 200 5 . 2 home 300 200 50 . 3 home 100 300 . 5 4 TDR 80 500 . 3
 

Thanks in advance,

Jack 

1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

That is just a proc sort then, position in the by statement indicates precendance:

proc sort data=have out=want;
  by net age;
run;

Sorts the data by age within the sort of data net.

View solution in original post

4 REPLIES 4
RW9
Diamond | Level 26 RW9
Diamond | Level 26

That is just a proc sort then, position in the by statement indicates precendance:

proc sort data=have out=want;
  by net age;
run;

Sorts the data by age within the sort of data net.

jackmelbourne
Fluorite | Level 6
Thank you 🙂
GPNaveen
Fluorite | Level 6

Hi,

 

If we need to sort any variable within another variable (nested sorting) ,we could get this done with the help of PROC SORT.
We can use the By variable and get the task done.

 

Example:

 

Data try;
Input NAME$ AGE NET REG LOSS;
Datalines;
home 300 200 50 .
home 100 300 . 5
TDR 80 500 . 3
TDR 100 200 5 .
;
run;


Proc sort data=try out=try2;by net age;
run;

 

Now the variable NET is sorted first within which the variable Age is sorted.

 

Thanks ,
GP

jackmelbourne
Fluorite | Level 6
Thank you :))

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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.

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
  • 4 replies
  • 896 views
  • 0 likes
  • 3 in conversation