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
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.