Hi,
I encounter this issue where I am sorting the data.
The following is the procedure;
The following is a sample data i used;
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;
Please provide me with some insights into this matter!
Thank you in advance!
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.
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.
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?
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.