Hi,
I have the following table grouped by ID.
I have to keep all the rows till 1 is encounted in Entry variable for first time and delete the remaining rows which follows afterwards for that particular ID.
For example in the below table, value 1 for variable Entry is encountered for first time in 3rd row for the ID value 10, so delete the rows after that for the corresponding ID i.e delete rows4,5,6.
for ID value 11, Entry value 1 is encountered first time in 9th row. So, delete rows after that, i.e rows 10&11 should be deleted. Thanks. your help is much appreciated.
RowNo ID Entry
1 10 2
2 10 2
3 10 1
4 10 1
5 10 2
6 10 1
7 11 2
8 11 2
9 11 1
10 11 2
11 11 1
Thanks,
Saravanan
One way would be:
data want (drop=_:);
set have;
by id;
retain _keep;
if first.id then _keep=1;
if _keep then output;
if entry eq 1 then _keep=0;
run;
One way would be:
data want (drop=_:);
set have;
by id;
retain _keep;
if first.id then _keep=1;
if _keep then output;
if entry eq 1 then _keep=0;
run;
Thank you very much for the reply. Much appreciated.
Saravanan
data want;
set have;
by id;
if first.id then flag=0;
if flag=1 then delete;
if entry=1 then flag=1;
run;
Thank you very much for the reply.
Saravanan
Since conveniently you have rowno, SQL can also be an option:
proc sql;
select a.* from have a,
(select id, min(rowno) as mrow from have group by id ,entry having entry=1) b
where a.id=b.id and a.rowno<=b.mrow
;
quit;
Haikuo
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!
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.