DATA Step, Macro, Functions and more

Delete first observations in BY group on condition

Reply
Occasional Learner
Posts: 1

Delete first observations in BY group on condition

[ Edited ]

Hello, I am looking to delete all initial observations of VAR, within each ID group, until we hit the first 0. If none of the values within an ID group are 0, then I want to remove all of them.

 

Here is a example of the data I have. I would like to remove observations 1, 10, 11, 12, 13 and 14

 

Thanks in advance!

observationIDVAR
111
210
311
410
511
611
720
821
920
1031
1131
1231
1341
1441
1540
1641
Super User
Posts: 9,874

Re: Delete first observations in BY group on condition

Posted in reply to blackandwhite
data want;
set have;
by id;
retain flag;
if first.id then flag = 0;
if var = 0 then flag = 1;
if flag;
drop flag;
run;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Respected Advisor
Posts: 3,167

Re: Delete first observations in BY group on condition

Posted in reply to blackandwhite
data want;
do until (last.var);
set have;
by id var notsorted ;
if first.id and var=1 then flag=1;
if flag ne 1 then output ;
end;
run;
Ask a Question
Discussion stats
  • 2 replies
  • 320 views
  • 1 like
  • 3 in conversation