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

Hi!

 

For unique id, I want to keep the record; for non-unique id, I want to keep the records with non-missing date. Could anyone help with it? Thank you!!

 

What I have:

id date
1 .
2 .
2 2000-06-10
2 2002-08-26
3 2010-02-03
4 .
4 2000-06-10
4 2005-07-26
4 2016-04-03

 

What I want:

id date
1 .
2 2000-06-10
2 2002-08-26
3 2010-02-03
4 2000-06-10
4 2005-07-26
4 2016-04-03
1 ACCEPTED SOLUTION

Accepted Solutions
SASJedi
Ammonite | Level 13

Try this:

data have;
	infile datalines truncover;
	input id date:yymmdd10.;
	format date yymmdd10.;
datalines;
1	.
2	.
2	2000-06-10
2	2002-08-26
3	2010-02-03
4	.
4	2000-06-10
4	2005-07-26
4	2016-04-03
;

/* The data must be sorted at least by ID */
proc sort data=have;
	by id date;
run;

data want;
	set have;
	by id;
	if (first.id and last.id)
		or not missing(date);
run;

My result:

Want

Obs id date
1 1 .
2 2 2000-06-10
3 2 2002-08-26
4 3 2010-02-03
5 4 2000-06-10
6 4 2005-07-26
7 4 2016-04-03
Check out my Jedi SAS Tricks for SAS Users

View solution in original post

1 REPLY 1
SASJedi
Ammonite | Level 13

Try this:

data have;
	infile datalines truncover;
	input id date:yymmdd10.;
	format date yymmdd10.;
datalines;
1	.
2	.
2	2000-06-10
2	2002-08-26
3	2010-02-03
4	.
4	2000-06-10
4	2005-07-26
4	2016-04-03
;

/* The data must be sorted at least by ID */
proc sort data=have;
	by id date;
run;

data want;
	set have;
	by id;
	if (first.id and last.id)
		or not missing(date);
run;

My result:

Want

Obs id date
1 1 .
2 2 2000-06-10
3 2 2002-08-26
4 3 2010-02-03
5 4 2000-06-10
6 4 2005-07-26
7 4 2016-04-03
Check out my Jedi SAS Tricks for SAS Users

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 1 reply
  • 691 views
  • 3 likes
  • 2 in conversation