if statement - Multiple conditions for same variable in SAS -
i'm trying detect specific values of 1 variable , create new 1 if conditions fulfilled.
here's part of data (i have 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 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 populated when result exists @ timepoint (time) 2. if there's no result baseline should @ time=1.
if result="" , time=2 do; if time=10 , result ne "" base=x; end; if result ne "" , time=2 base=x; `
it works correctly when time=2 , results exists. if results missing, there's wrong.
the question seems bit off. "else if time="" , time=1" there seems typo there somewhere.
however, syntax seems solid. i've worked example given data. first condition works, second (else if ) assumption. updating question 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 , result ne "" base='x'; else if time=1 , id=2 base='x'; run;
edit based on revisited question.
assuming time-point (1) next point (2). (if not, add more lags.) simulating lead function sort data backwards , utilize lag.
proc sort data=begin; id descending time; run; data flagged; set begin; if lag(time)=2 , lag(result) eq "" base='x'; if time=2 , result ne "" base='x'; run;
more opposite of lag: https://communities.sas.com/t5/sas-communities-library/how-to-simulate-the-lead-function-opposite-of-lag/ta-p/232151
Comments
Post a Comment