Dear Experts,
I want to delete the rows using a condition given below. Kindly go through my sample dataset for better details.
data have;
input Id Events value;
cards;
101 0 12
101 0 15
101 1 14
101 0 18
108 0 45
108 1 25
108 0 23
;
In the given set. I want to delete the row which occurs after the event meets 1.
Rows need to delete :
101 0 18
108 0 23
Kindly suggest some code to solve the task
Thank you!
data have;
input Id Events value;
cards;
101 0 12
101 0 15
101 1 14
101 0 18
108 0 45
108 1 25
108 0 23
;
data want;
do until(last.id);
set have;
by id;
if not n then output;
if EVENTS then n=1;
end;
drop n;
run;
Please try
data want;
set have;
by id;
flag=lag(events);
if first.id then flag=.;
if flag=1 then delete;
drop flag;
run;
data have;
input Id Events value;
cards;
101 0 12
101 0 15
101 1 14
101 0 18
108 0 45
108 1 25
108 0 23
;
data want;
do until(last.id);
set have;
by id;
if not n then output;
if EVENTS then n=1;
end;
drop n;
run;
Just for fun.
data have;
input Id Events value;
cards;
101 0 12
101 0 15
101 1 14
101 0 18
108 0 45
108 1 25
108 0 23
;
data want;
set have;
by id;
retain flag;
lag=lag(events);
if first.id then call missing(lag,flag);
if lag then flag=1;
if flag then delete;
run;
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.