## Sorting and grouping

Solved
Occasional Contributor
Posts: 13

# Sorting and grouping

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.

Accepted Solutions
Solution
‎11-05-2017 04:11 PM
Super User
Posts: 23,323

## Re: Sorting and grouping

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...

All Replies
Super User
Posts: 9,923

## Re: Sorting and grouping

You sorted by salary, but tried to print by country. When sorting by salary, you destroyed the country sequence.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎11-05-2017 04:11 PM
Super User
Posts: 23,323

## Re: Sorting and grouping

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...

Occasional Contributor
Posts: 13