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

Hello,

I have a data set where each record is identified by a record id. Multiple records share a similar unique id based on identifying individuals. Some records with same unique id have different dates.

Could I identify the record with the unique id that has the earliest date by creating a flag variable with 0 (for no) and 1 (for yes) values?

sample code below:

data have;
input recordID uniqueid date:ddmmyy10.;
format date ddmmyy10.;
datalines;

1234625 55 08/03/2006
1234632 72 06/03/2005
1234634 8 11/11/1997
1234636 104 09/03/2007
1234650 88 12/10/1999
1234617 55 05/12/2000
;
run;

 

data want;
input recordID uniqueid date:ddmmyy10. flag;
format date ddmmyy10.;
datalines;

1234625 55 08/03/2006 0
1234632 72 06/03/2005 0
1234634 8 11/11/1997 0
1234636 104 09/03/2007 0
1234650 88 12/10/1999 0
1234617 55 05/12/2000 1
;
run;

 

Thank you!

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
data have;
input recordID uniqueid date:ddmmyy10.;
format date ddmmyy10.;
datalines;
1234625 55 08/03/2006
1234632 72 06/03/2005
1234634 8 11/11/1997
1234636 104 09/03/2007
1234650 88 12/10/1999
1234617 55 05/12/2000
;
run;

proc sort data=have out=temp;
by uniqueid date;
run;
data want;
 set temp;
 by uniqueid;
 if first.uniqueid and last.uniqueid then want=0;
  else want=ifn(first.uniqueid,1,0);
run;

View solution in original post

4 REPLIES 4
Ksharp
Super User
data have;
input recordID uniqueid date:ddmmyy10.;
format date ddmmyy10.;
datalines;
1234625 55 08/03/2006
1234632 72 06/03/2005
1234634 8 11/11/1997
1234636 104 09/03/2007
1234650 88 12/10/1999
1234617 55 05/12/2000
;
run;

proc sort data=have out=temp;
by uniqueid date;
run;
data want;
 set temp;
 by uniqueid;
 if first.uniqueid and last.uniqueid then want=0;
  else want=ifn(first.uniqueid,1,0);
run;
Kurt_Bremser
Super User

I think this would do the same:

want = ifn(first.uniqueid and not last.uniqueid,1,0);

or, done with IF:

if first.uniqueid and not last,uniqueid
then want = 1;
else want = 0;

To get the want dataset as shown in the original question, an additional sort by recordid is required.

Ihsan-Mahdi
Quartz | Level 8

Thanks Kurt! This works great as well!

Ihsan-Mahdi
Quartz | Level 8

Thank you so much! This worked great!

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 4 replies
  • 427 views
  • 4 likes
  • 3 in conversation