I have data:
data have;
input id type $ date $;
datalines;
1 c 2019-08-01
1 a 2019-08-04
1 b 2019-08-05
1 c 2019-08-11
2 c 2019-10-13
2 c 2019-10-14
2 a 2019-10-20
2 b 2019-10-25
;
I want to remove observations that occur before type a, if they are not type a. Therefore, each ID should have their first observation being equal to type a.
Desired output:
data want;
input id type $ date $;
datalines;
1 a 2019-08-04
1 b 2019-08-05
1 c 2019-08-11
2 a 2019-10-20
2 b 2019-10-25
;
Try this:
data have ;
input id type $ date :$10. ;
datalines;
1 c 2019-08-01
1 a 2019-08-04
1 b 2019-08-05
1 c 2019-08-11
2 c 2019-10-13
2 c 2019-10-14
2 a 2019-10-20
2 b 2019-10-25
;
run ;
data want ;
do _n_ = 0 by 0 until (last.id) ;
set have ;
by id ;
if type = "a" then _n_ = 1 ;
if _n_ then output ;
end ;
run ;
Kind regards
Paul D.
Assuming your data set is already sorted by ID:
data want;
del_flag = 'N';
do until (last.id);
set have;
by ID;
if type='a' then del_flag = 'Y';
end;
do until (last.id);
set have;
by ID;
if type='a' then del_flag = 'N';
if del_flag = 'N' then output;
end;
drop del_flag;
run;
This assumes that if an ID has no type "a" observations, you want to keep all its observations. It's actually simpler if you want to delete them all instead:
data want;
del_flag = 'N';
do until (last.id);
set have;
by ID;
if type='a' then del_flag = 'N';
if del_flag = 'N' then output;
end;
drop del_flag;
run;
Try this:
data have ;
input id type $ date :$10. ;
datalines;
1 c 2019-08-01
1 a 2019-08-04
1 b 2019-08-05
1 c 2019-08-11
2 c 2019-10-13
2 c 2019-10-14
2 a 2019-10-20
2 b 2019-10-25
;
run ;
data want ;
do _n_ = 0 by 0 until (last.id) ;
set have ;
by id ;
if type = "a" then _n_ = 1 ;
if _n_ then output ;
end ;
run ;
Kind regards
Paul D.
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 25. 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.