BookmarkSubscribeRSS Feed
hellorc
Obsidian | Level 7

Hello SAS community, I have a quick question. Consider the following simple data:

 

data have;
input id city $ week money @@;
datalines;
1 A 1 100
1 A 2 105
1 A 5 108
1 B 5 206
1 B 7 99
1 C 8 101
1 C 10 110
2 B 5 202
2 C 5 100
2 D 8 95
2 E 8 98
;
run;

I would like the observations for each ID have unique week. If for each subject there are duplicate 'week', the 'week' with larger money variable would be kept. So:

data want;
input id city $ week money @@;
datalines;
1 A 1 100
1 A 2 105
1 B 5 206
1 B 7 99
1 C 8 101
1 C 10 110
2 B 5 202
2 E 8 98
;
run;

Can this be done via some data steps? Or SQL is a must? Might someone be willing to assist? Thank you in advance!

 

 

 

 

1 REPLY 1
Kurt_Bremser
Super User

Sort, then use a data step with BY:

proc sort data=have,
by id week money;
run;

data want;
set have;
by id week;
if last.week;
run;

Untested, posted from my tablet.

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

Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 494 views
  • 0 likes
  • 2 in conversation