Hi,
I have attached here the code to the dataset that I have and what I am trying to get to but have some difficulty. Essentially, I have a dataset with multiple visits (visit_no) by ID (different IDs have different number of total visits). For each ID, I am trying to fill in the missing values for "result" as "positive" for all visits following a visit in which this variable was already recorded as being "positive". Note: only looking forward when replacing the missing as positive. Note2: if result is negative, leave missing as is.
Many thanks in advance for your help
**
data have;
input ID visit_no result $;
datalines;
1 1 positive
1 2 .
1 3 .
1 4 positive
2 1 negative
2 2 negative
2 3 positive
2 4 .
2 5 .
3 1 .
3 2 negative
3 3 .
4 1 .
4 2 positive
;
run;
data want;
input ID visit_no result $;
datalines;
1 1 positive
1 2 positive
1 3 positive
1 4 positive
2 1 negative
2 2 negative
2 3 positive
2 4 positive
2 5 positive
3 1 .
3 2 negative
3 3 .
4 1 .
4 2 positive
;
run;
data have;
input ID visit_no result $;
datalines;
1 1 positive
1 2 .
1 3 .
1 4 positive
2 1 negative
2 2 negative
2 3 positive
2 4 .
2 5 .
3 1 .
3 2 negative
3 3 .
4 1 .
4 2 positive
;
run;
data want;
set have;
by id;
retain t;
length t $20;
if first.id then t=' ';
if not missing(result) then t=result;
else if t='positive' then result=t;
drop t;
run;
data have;
input ID visit_no result $;
datalines;
1 1 positive
1 2 .
1 3 .
1 4 positive
2 1 negative
2 2 negative
2 3 positive
2 4 .
2 5 .
3 1 .
3 2 negative
3 3 .
4 1 .
4 2 positive
;
run;
data want;
set have;
by id;
retain t;
length t $20;
if first.id then t=' ';
if not missing(result) then t=result;
else if t='positive' then result=t;
drop t;
run;
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.