BookmarkSubscribeRSS Feed
erinlee
Calcite | Level 5

Hi! I'm trying to subset some data to one row per id per week (currently is multiple rows/id/week). I want to keep the row associated with the latest date. Event date just corresponds to if the event occurred in 2021 (2) or before (1). Please help! Simple question but I'm super stuck! Thanks in advance!

Have:

weekid yearevent_date
1120151
1120161
1120212
2120151
2120161
2120212
3120151
3120161
3120212
1220151
1220161
1220171
2220151
2220161
2220171
3220151
3220161
3220171

 

Want:

weekidyearevent_date
1120212
1220171
2120212
2220171
3120212
3220171
1 REPLY 1
maguiremq
SAS Super FREQ

Here you go:

 

data have;
infile datalines delimiter = '	';
input week id year event_date;
datalines;
1	1	2015	1
1	1	2016	1
1	1	2021	2
2	1	2015	1
2	1	2016	1
2	1	2021	2
3	1	2015	1
3	1	2016	1
3	1	2021	2
1	2	2015	1
1	2	2016	1
1	2	2017	1
2	2	2015	1
2	2	2016	1
2	2	2017	1
3	2	2015	1
3	2	2016	1
3	2	2017	1
;
run;

proc sort data = have;
	by week id year;
run;

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

Hopefully didn't miss something - let me know if I did.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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