Reputation: 45
I'm trying to detect specific values of one variable and create a new one if those conditions are fulfilled.
Here's a part of my data (I have much more rows) :
id time result
1 1 normal
1 2 normal
1 3 abnormal
2 1 normal
2 2
3 3 normal
4 1 normal
4 2 normal
4 3 abnormal
5 1 normal
5 2 normal
5 3
What I want
id time result base
1 1 normal
1 2 normal x
1 3 abnormal
2 1 normal x
2 2
2 3 normal
3 3 normal
4 1 normal
4 2 normal x
4 3 abnormal
5 1 normal
5 2 normal x
5 3
My baseline value (base) should be populated when result exists at timepoint (time) 2. If there's no result then baseline should be at time=1.
if result="" and time=2 then do;
if time=10 and result ne "" then base=X; end;
if result ne "" and time=2 then base=X; `
It works correctly when time=2 and results exists. But if results missing, then there's something wrong.
Upvotes: 0
Views: 2279
Reputation: 1396
The question seems a bit off. "Else if time="" and time=1" There seems to be a typo there somewhere.
However, your syntax seems solid. I've worked an example with your given data. The first condition works, but second (else if ) is assumption. Updating as question is updated.
options missing='';
data begin;
input id time result $ 5-20 ;
datalines;
1 1 normal
1 2 normal
1 3 abnormal
2 1 normal
2 2
3 3 normal
4 1 normal
4 2 normal
4 3 abnormal
;
run;
data flagged;
set begin;
if time=2 and result NE "" then base='X';
else if time=1 and id=2 then base='X';
run;
Edit based on revisited question.
Assuming that the time-point (1) is always next to the point (2). (If not, then add more lags.) Simulating the Lead function we sort the data backwards and utilize lag.
proc sort data=begin; by id descending time; run;
data flagged;
set begin;
if lag(time)=2 and lag(result) EQ "" then base='X';
if time=2 and result NE "" then base='X';
run;
More about opposite of lag: https://communities.sas.com/t5/SAS-Communities-Library/How-to-simulate-the-LEAD-function-opposite-of-LAG/ta-p/232151
Upvotes: 2