BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
NewUsrStat
Quartz | Level 8

Hi guys, 

suppose to have the following: 

 

ID               Year         

 A              2018

 A              2018

 A              2020

 A              2020

 A              2021

 B              2020

 B              2020

.......           .............

 

Is there a way to get the following? 

 

ID               Year         

 A              2018

 B              2020

.......           .............

 

Meaning: if ID has more than one year like "A" then take the most ancient and take it one time; if ID has more records of the same year, then take the ID and the relative year one time. 

 

Thank you in advance

 

1 ACCEPTED SOLUTION

Accepted Solutions
antonbcristina
SAS Employee

You can achieve this with a simple DATA step.

 

You'll first need to sort the data by ID and YEAR.

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

Then use a DATA step with a BY statement, keeping only the first occurence of each ID. Since the data is sorted by ID and YEAR, you'll automatically keep the earliest occurence of each ID.

 

data want;
	set have; 
	by id;
	if first.id;
run;	

View solution in original post

1 REPLY 1
antonbcristina
SAS Employee

You can achieve this with a simple DATA step.

 

You'll first need to sort the data by ID and YEAR.

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

Then use a DATA step with a BY statement, keeping only the first occurence of each ID. Since the data is sorted by ID and YEAR, you'll automatically keep the earliest occurence of each ID.

 

data want;
	set have; 
	by id;
	if first.id;
run;	

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

5 Steps to Your First Analytics Project Using SAS

For SAS newbies, this video is a great way to get started. James Harroun walks through the process using SAS Studio for SAS OnDemand for Academics, but the same steps apply to any analytics project.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 282 views
  • 1 like
  • 2 in conversation