BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
blackandwhite
Obsidian | Level 7

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;

1 ACCEPTED SOLUTION

Accepted Solutions
novinosrin
Tourmaline | Level 20
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;

View solution in original post

2 REPLIES 2
novinosrin
Tourmaline | Level 20
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;

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 855 views
  • 1 like
  • 2 in conversation