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

Dear team member,

Thanks for your post. Thanks for this approach. I can use proc sql to do get what I want. How about proc sort nodupkey approach?

Thanks,

blueblue

Blue Blue
GN0001
Barite | Level 11
Hello Kurt,
I think this works if I sort ascending by date, no dupkey. I need to make sure which observation SAS keeps after I sort it by ascending.
if I have:
1 03/14/2021
1 04/14/2021
1 08/09/2021
Then when I use proc sql no dupkey ascending, then I will get 08/09/2021, SAS doesn't remove the max date? I want to know how SAS processes the observations in proc sort nodupkey ascending.
Please advise me.
Regards,
bluebue
Blue Blue
Patrick
Opal | Level 21

@GN0001 IF you would have posted sample data with the date variable containing a proper SAS date value then I assume the discussion here would have been much shorter/quicker.

IF you've got a date variable that sorts properly then a double Proc Sort is one way to go.

The first Proc Sort to order your data with the newest date per member id on top, the 2nd Proc Sort with a NODUPKEY to then pick the first row per member id (same that what a data step if first.member_id would do).

data have;
  input MemberID $ date1:ddmmyy10.;
  format date1 date9.;
  datalines;
1 04/02/2021
1 04/03/2021
1 05/03/2019
;

proc sort data=have out=want;
  by MemberID DESCENDING date1;
run;
proc sort nodupkey out=want;
  by MemberID;
run;

 

GN0001
Barite | Level 11
Sorry, please see my response to Kurt.
Can you please read
My response to Kurt. Did you see what Kurt had responded?
Respectfully
Blueblue
Blue Blue
GN0001
Barite | Level 11
Hello Patrick,
Something came to my mind. If memberId are same but observations are different across rows, then what happens?
Thanks for your input,
Blueblue
Blue Blue
Kurt_Bremser
Super User

@GN0001 wrote:
Hello Patrick,
Something came to my mind. If memberId are same but observations are different across rows, then what happens?
Thanks for your input,
Blueblue

Once again: try it.

Especially test PROC SORT with the NODUPKEY vs. the NODUPREC options.

Kurt_Bremser
Super User

Using the double SORT may not work under certain circumstances (SPDE datasets, for example).

After the ascending sort, do

data want;
set have;
by id;
if last.id;
run;
GN0001
Barite | Level 11
Hello Kurt, I found what I was looking for, so if it keeps the max in the first observation, this should work.
Thanks for your response.
By the way, I posted my data very first time I posted this question.
I truly appreciate your response.
Thanks,
Blue blue
Blue Blue
GN0001
Barite | Level 11

Kurt,

Can you please explain about this?
thanks

Blue blue

Blue Blue

CFC_SAS_Communities_400x225.jpg

Call for content now open!

It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.

Submit your proposal →

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
  • 24 replies
  • 13008 views
  • 10 likes
  • 6 in conversation