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 |
It's your turn to help shape SAS Innovate 2027. Share your expertise and inspire the SAS community.
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.
Ready to level-up your skills? Choose your own adventure.