- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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!
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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?
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
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.