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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

Register now

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