data T;
input ID L EXP;
Datalines;
A1 1 10
A1 1 10
A1 2 20
A1 2 20
A1 4 20
A1 4 20
A1 5 20
A2 1 10
A2 2 60
A2 2 60
A2 3 60
A2 3 60
A2 5 50
A2 7 50
A3 2 60
A3 4 60
;
data want;
do until (last.id);
set T;
by id;
if l <= 1 then flag = 1;
end;
put flag=;
do until(last.id);
set T;
by id;
if not first.id and lag(l) ne l - 1 then flag = 0;
if not flag then exp = 0;
output;
end;
drop flag;
run;
Hi,
On this dataset (already sorted by ID, L) , I want to update EXP to 0 in these cases:
1. The First.L per ID is not 1 (and all the subsequent to it)
2. The first time the sequence of L is broken per ID and all the subsequent to it. (there are repetitions of L)
i.e the output should be
A1 1 10
A1 1 10
A1 2 20
A1 2 20
A1 4 0
A1 4 0
A1 5 0
A2 1 10
A2 2 60
A2 2 60
A2 3 60
A2 3 60
A2 5 0
A2 7 0
A3 2 0
A3 4 0
The above code does not properly work in this case.
How should be amended?
Thanks.
Please explain what "The first time the sequence of L is broken per ID" means.
Please explain what "The first time the sequence of L is broken per ID" means.
Maybe this solves it, i renamed "L" to increase readability.
data want;
set have;
by ID;
retain reset;
LastLorax = lag(Lorax);
if first.Id then do;
LastLorax = .;
reset = 0;
if Lorax ^= 1 then do;
reset = 1;
end;
end;
else do;
if Lorax - LastLorax > 1 or Lorax = 4 then do;
reset = 1;
end;
end;
if reset then do;
exp = 0;
end;
keep ID Lorax Exp;
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: