Reputation: 2583
Suppose I have the following:
data DB;
input ID $ Class1 Class2 Class3 Index;
cards;
0001 . . . 2
0001 . . . 2
0001 . 3.2 . 1
0001 . 3.2 5.3 .
0002 2.1 . . 1
0002 2.1 0.2 . .
0002 . 3.2 . 1
0002 . 3.2 1.1 .
;
Is there a way to get the following?
data DB1;
input ID $ Class1 Class2 Class3 Index;
cards;
0001 . . . 2
0001 . . . 2
0001 . 3.2 . 1
0001 . . 5.3 .
0002 2.1 . . 1
0002 . 0.2 . .
0002 . 3.2 . 1
0002 . . 1.1 .
;
In other words when Index = . then delete the replicated value for each Class: (the second of replicated values by ID but only at line where Index = .).
Upvotes: 0
Views: 55
Reputation: 11
If I understand correctly, you want to set a CLASS variable to missing if the current record has INDEX=. and the immediately preceding record has INDEX=1 and an identical value for the CLASS variable. If that is correct, then
data want (drop=_:);
set db;
by id;
array CL class: ;
_lgindex=ifn(first.id,.,lag(index));
do over CL;
if CL=lag(CL) and _lgindex=1 and first.ID=0 then CL=.;
end;
run;
Upvotes: 0
Reputation: 4937
Try this. And comment out the Drop Statement if you want to understand the logic.
data want;
set DB;
by ID;
array lag_{3};
array c{*} class:;
lag_index = ifn(first.ID, ., lag(Index));
if lag_index = 1 then do;
do i = 1 to dim(lag_);
if lag_[i] = c[i] then c[i] = .;
end;
end;
output;
do i = 1 to dim(lag_);
lag_[i] = c[i];
end;
retain lag_:;
drop lag_: i lag_index;
run;
Result:
ID Class1 Class2 Class3 Index
0001 . . . 2
0001 . . . 2
0001 . 3.2 . 1
0001 . . 5.3 .
0002 2.1 . . 1
0002 . 0.2 . .
0002 . 3.2 . 1
0002 . . 1.1 .
Upvotes: 1