Reputation: 101
Based on the simplified dataset below, I would like to create a new variable X3 that is equal to 1 if the last observation by
variable id
has default = 1 and if X1 = X2, else
X3 = 0.
data example;
input id default X1 X2;
cards;
1 0 0 1
1 0 0 1
1 0 1 1
1 0 0 1
1 1 0 1
2 0 0 1
2 0 1 1
2 0 0 1
2 0 0 1
2 1 0 1
3 0 0 1
3 0 1 1
;
run;
The new dataset should look like the following:
id default X1 X2 X3;
1 0 0 1 0
1 0 0 1 0
1 0 1 1 1
1 0 0 1 0
1 1 0 1 0
2 0 0 1 0
2 0 1 1 1
2 0 0 1 0
2 0 0 1 0
2 1 0 1 0
3 0 0 1 0
3 0 0 1 0
I've tried quite a few things and have been unable to find any solutions. Any suggestions would be greatly appreciated!
Upvotes: 0
Views: 76
Reputation: 9569
Double DOW-loop is the way to go here:
data want;
do _n_ = 1 by 1 until (last.ID);
set example;
by ID;
if last.ID and default = 1 then last_default = 1;
drop last_default;
end;
do _n_ = 1 to _n_;
set example;
x3 = x1 = x2 and last_default;
output;
end;
run;
Upvotes: 0