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

Hello SAS Communities, 

I have the following dataset HypTabs.Vitals:

 

Obs    SSN                 VisitDt         Ht       Wt    SBP  DBP

1000-00-000113JAN20106614911779
2000-00-000121JAN20116614911780
3000-00-000113JAN20126614812180
4000-00-000122JAN20136615311179
5000-00-000115JAN20146615211679
6000-00-000214JUN20106314111872
7000-00-000220JUN20126314011876
8000-00-000217JUN20136313811774
9000-00-000217JUN20146313811172
10000-00-000303JAN20116614912077
11000-00-000302JAN20126615111176
12000-00-000331DEC20126615212678

I want to create a dataset with only the last observation per SSN group. So, my final dataset will be as follows:

Obs    SSN                 VisitDt         Ht       Wt    SBP  DBP

1000-00-000115JAN20146615211679
2000-00-000217JUN20146313811172
3000-00-000331DEC20126615212678

Can you help me with the SAS DATA Step which will help me create the above temporary reduced dataset? 

Thanks,
Adhuna Mukhopadhyay

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

SQL:

proc sql;
create table want as
  select *
  from have
  group by ssn
  having visitdt = max(visitdt)
;
quit;

Data step:

proc sort data=have;
by ssn visitdt;
run;

data want;
set have;
by ssn;
if last.ssn;
run;

View solution in original post

3 REPLIES 3
Kurt_Bremser
Super User

SQL:

proc sql;
create table want as
  select *
  from have
  group by ssn
  having visitdt = max(visitdt)
;
quit;

Data step:

proc sort data=have;
by ssn visitdt;
run;

data want;
set have;
by ssn;
if last.ssn;
run;
mdakkak
Fluorite | Level 6

Hi,

 

The way I was able to do it was to include IF LAST.SSN in my code. This allows you to only obtain the most recent visit from a single SSN. 

DATA WORK.VitalLast;
	SET HYPTABS.Vitals;
	BY SSN;
	IF LAST.SSN;
RUN;

Hope that helps!

bfreeman0402
Calcite | Level 5

Hi!

 

I'm not sure if by most recent observation you meant most recent date (your questions states most recent observation in a group however the temporary data set does not reflect this), BUT since your question appears to have already been answered I will answer as if you wanted to obtain the most recent observation (by date) using an alternative to the answers already provided. I want to say you may be able to sort SSN by ascending and LastVisit by descending and use NODUPKEY to only keep the first observation. 

 

Best,

Brandon 

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