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

I am a beginner to SAS. I am following the tutorials. I am confused on sorting and grouping.

 

proc print data= orion.sales;
	by country;
run;

This code works well, and group the data set. When I run following code, it displays error. 

 

 

proc sort data=orion.sales
	out=work.sort_sales;
	by salary ;
run;

proc print data=work.sort_sales;
	by country;
Run;
ERROR: Data set WORK.SORT_SALES is not sorted in ascending sequence. The current BY group has Country = US and the next BY group has Country = AU.
 
I do not understand the reasoning behind having the same variable which we use to group by, in sorting statement. 
 
Thanks in advance. 
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

I do not understand the reasoning behind having the same variable which we use to group by, in sorting statement. 

SAS processess data line by line, so when you use a BY statement it expects the BY variables to be in order in the data. To ensure this is the case, you would first need to sort the data to have that specified order. So the BY variable list needs to be the same in your SORT and then your BY statements in further processes.

 

If you need a more detailed explanation, I suggest reading through the chapter on BY group processing in the documentation.

 

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0tq11jtmrhsd4n1co4p5tu1fbsi.htm&docsetVer...

 

 

View solution in original post

3 REPLIES 3
Reeza
Super User

I do not understand the reasoning behind having the same variable which we use to group by, in sorting statement. 

SAS processess data line by line, so when you use a BY statement it expects the BY variables to be in order in the data. To ensure this is the case, you would first need to sort the data to have that specified order. So the BY variable list needs to be the same in your SORT and then your BY statements in further processes.

 

If you need a more detailed explanation, I suggest reading through the chapter on BY group processing in the documentation.

 

http://documentation.sas.com/?docsetId=lrcon&docsetTarget=p0tq11jtmrhsd4n1co4p5tu1fbsi.htm&docsetVer...

 

 

Myurathan
Quartz | Level 8

Thank you so much for your quick reply. I will read the materials. 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 3 replies
  • 4858 views
  • 2 likes
  • 3 in conversation