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

Hi,
I encounter this issue where I am sorting the data.
The following is the procedure;
1.PNG

The following is a sample data i used;
2.PNG

Based on my understanding, it should print out the second row because CREATED is sorting in descending order.
But the result i got is completely different, it prints out the one in between.
As follow;
3.PNG

Please provide me with some insights into this matter!
Thank you in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

Don't rely on SAS preserving the order within the groups. This can be dependent on the type of SAS storage.

Do this as your second step:

data want_emails;
set emails;
by ch_phpartyid sourcesystemclient;
if first.sourcesystemclient;
run;

BTW, don't post code as a picture. Copy/paste the text to a code window ("little running man" or {i} icon). It saves us re-typing the code.

View solution in original post

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

What prints out?

Your first sort works as you intend, however when you run the second sort that changes the data.  What is it you want, just the last record per group?  Then:

proc sort data=emails;
  by ch_phpartyid sourcesystemclient descending created;
run;

data want;
  set emails
  by ch_phpartyid sourcesystemclient descending created;
  if last.ch_phpartyid;
run;

Do note how I use formatting, consistent casing, indentations etc. to make the code easy to read.

Wken1122
Obsidian | Level 7

But the thing is, if Created is sorted in descending order then why would 18/8/2016's row appears in the sorted table instead of 6/10/2016?

Kurt_Bremser
Super User

Don't rely on SAS preserving the order within the groups. This can be dependent on the type of SAS storage.

Do this as your second step:

data want_emails;
set emails;
by ch_phpartyid sourcesystemclient;
if first.sourcesystemclient;
run;

BTW, don't post code as a picture. Copy/paste the text to a code window ("little running man" or {i} icon). It saves us re-typing the code.

ballardw
Super User

This may seem a silly question but is your Created variable numeric or character? If it is character then you will get all the day 31 records first, then day 30, then day 29 as character variables will sort differently than an actual Datetime variable. Since your value shown has a space between the year and the time component it is very likely that variable is character as most of the SAS datetime display formats do not include that space.

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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