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 |
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 |
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 |
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.