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

Hi All, 

 

I'm trying to rank order transactions by multiple variables. I've tried using the proc rank function but keep running into the ERROR: Data set WORK.TABLE1 is not sorted in ascending sequence.

 

I have the table properly ordered in the below, but can't seem to get the ranking to work. 

 

Table1

TransactionsDistrict_Manager_NameSales_Associate_NameMonth_NumYear_Date
12John SmithSally Smith42019
11John SmithJohn Roberts42019
10John SmithAndrew Jones42019
9John SmithCam Barry42019
20John SmithSally Smith52019
19John SmithJohn Roberts52019
18John SmithAndrew Jones52019
17John SmithCam Barry52019
12Michael BrownJoanne Low42019
11Michael BrownJimmy Lary42019
10Michael BrownJoe King42019
9Michael BrownSherry Bowman42019
20Michael BrownJoanne Low52019
19Michael BrownJimmy Lary52019
18Michael BrownJoe King52019
17Michael BrownSherry Bowman52019

 

Desired Output

Rank TransactionsDistrict_Manager_NameSales_Associate_NameMonth_NumYear_Date
112John SmithSally Smith42019
211John SmithJohn Roberts42019
310John SmithAndrew Jones42019
49John SmithCam Barry42019
120John SmithSally Smith52019
219John SmithJohn Roberts52019
318John SmithAndrew Jones52019
417John SmithCam Barry52019
112Michael BrownJoanne Low42019
211Michael BrownJimmy Lary42019
310Michael BrownJoe King42019
49Michael BrownSherry Bowman42019
120Michael BrownJoanne Low52019
219Michael BrownJimmy Lary52019
318Michael BrownJoe King52019
417Michael BrownSherry Bowman52019
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Please show your code and log. This would require having multiple variables in the BY statement but should work fine as long as you the DESCENDING key word set in both. 

 

It does look like a basic enumeration problem so assuming your data is sorted correctly (which may not be true given your error) the following will also work but does require the correct sort.

 

proc sort data=have;
by district_manager_name month_num descending transactions;
run;

data want;
set have;
by district_manager_name month_num;
if first.month_num the rank=0;
rank+1;
run;

@ekthomas13 wrote:

Hi All, 

 

I'm trying to rank order transactions by multiple variables. I've tried using the proc rank function but keep running into the ERROR: Data set WORK.TABLE1 is not sorted in ascending sequence.

 

I have the table properly ordered in the below, but can't seem to get the ranking to work. 

 

Table1

Transactions District_Manager_Name Sales_Associate_Name Month_Num Year_Date
12 John Smith Sally Smith 4 2019
11 John Smith John Roberts 4 2019
10 John Smith Andrew Jones 4 2019
9 John Smith Cam Barry 4 2019
20 John Smith Sally Smith 5 2019
19 John Smith John Roberts 5 2019
18 John Smith Andrew Jones 5 2019
17 John Smith Cam Barry 5 2019
12 Michael Brown Joanne Low 4 2019
11 Michael Brown Jimmy Lary 4 2019
10 Michael Brown Joe King 4 2019
9 Michael Brown Sherry Bowman 4 2019
20 Michael Brown Joanne Low 5 2019
19 Michael Brown Jimmy Lary 5 2019
18 Michael Brown Joe King 5 2019
17 Michael Brown Sherry Bowman 5 2019

 

Desired Output

Rank  Transactions District_Manager_Name Sales_Associate_Name Month_Num Year_Date
1 12 John Smith Sally Smith 4 2019
2 11 John Smith John Roberts 4 2019
3 10 John Smith Andrew Jones 4 2019
4 9 John Smith Cam Barry 4 2019
1 20 John Smith Sally Smith 5 2019
2 19 John Smith John Roberts 5 2019
3 18 John Smith Andrew Jones 5 2019
4 17 John Smith Cam Barry 5 2019
1 12 Michael Brown Joanne Low 4 2019
2 11 Michael Brown Jimmy Lary 4 2019
3 10 Michael Brown Joe King 4 2019
4 9 Michael Brown Sherry Bowman 4 2019
1 20 Michael Brown Joanne Low 5 2019
2 19 Michael Brown Jimmy Lary 5 2019
3 18 Michael Brown Joe King 5 2019
4 17 Michael Brown Sherry Bowman 5 2019

 

 

 

View solution in original post

1 REPLY 1
Reeza
Super User

Please show your code and log. This would require having multiple variables in the BY statement but should work fine as long as you the DESCENDING key word set in both. 

 

It does look like a basic enumeration problem so assuming your data is sorted correctly (which may not be true given your error) the following will also work but does require the correct sort.

 

proc sort data=have;
by district_manager_name month_num descending transactions;
run;

data want;
set have;
by district_manager_name month_num;
if first.month_num the rank=0;
rank+1;
run;

@ekthomas13 wrote:

Hi All, 

 

I'm trying to rank order transactions by multiple variables. I've tried using the proc rank function but keep running into the ERROR: Data set WORK.TABLE1 is not sorted in ascending sequence.

 

I have the table properly ordered in the below, but can't seem to get the ranking to work. 

 

Table1

Transactions District_Manager_Name Sales_Associate_Name Month_Num Year_Date
12 John Smith Sally Smith 4 2019
11 John Smith John Roberts 4 2019
10 John Smith Andrew Jones 4 2019
9 John Smith Cam Barry 4 2019
20 John Smith Sally Smith 5 2019
19 John Smith John Roberts 5 2019
18 John Smith Andrew Jones 5 2019
17 John Smith Cam Barry 5 2019
12 Michael Brown Joanne Low 4 2019
11 Michael Brown Jimmy Lary 4 2019
10 Michael Brown Joe King 4 2019
9 Michael Brown Sherry Bowman 4 2019
20 Michael Brown Joanne Low 5 2019
19 Michael Brown Jimmy Lary 5 2019
18 Michael Brown Joe King 5 2019
17 Michael Brown Sherry Bowman 5 2019

 

Desired Output

Rank  Transactions District_Manager_Name Sales_Associate_Name Month_Num Year_Date
1 12 John Smith Sally Smith 4 2019
2 11 John Smith John Roberts 4 2019
3 10 John Smith Andrew Jones 4 2019
4 9 John Smith Cam Barry 4 2019
1 20 John Smith Sally Smith 5 2019
2 19 John Smith John Roberts 5 2019
3 18 John Smith Andrew Jones 5 2019
4 17 John Smith Cam Barry 5 2019
1 12 Michael Brown Joanne Low 4 2019
2 11 Michael Brown Jimmy Lary 4 2019
3 10 Michael Brown Joe King 4 2019
4 9 Michael Brown Sherry Bowman 4 2019
1 20 Michael Brown Joanne Low 5 2019
2 19 Michael Brown Jimmy Lary 5 2019
3 18 Michael Brown Joe King 5 2019
4 17 Michael Brown Sherry Bowman 5 2019

 

 

 

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 1 reply
  • 2945 views
  • 0 likes
  • 2 in conversation