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.

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 278 views
  • 0 likes
  • 2 in conversation