I want to fill missing observation in status variable on 2 condition
1. if bucket =4 then status= "bad"
2. if previous bucket ="bad" and current bucket <4 then status = "recover"
so based on these two condition all missing obs should be "recover" but could not apply this logic on sas. Please help
id bucket status
1 0 good
1 4 bad
1 0 ""
1 0 ""
1 1 ""
1 2 ""
1 3 ""
1 4 bad
2 points:
Try the code below.
data have;
input id bucket;
datalines;
1 0
1 4
1 0
1 0
1 1
1 2
1 3
1 4
2 0
2 4
2 0
2 0
2 1
2 2
2 3
2 4
;
data want(drop = prev_4);
set have;
by id;
if first.id then prev_4 = 0;
if bucket = 4 then prev_4 = 1;
length status $20;
status = ifc(bucket = 4, 'bad', 'good');
if prev_4 = 1 & bucket < 4 then status = 'recover';
retain prev_4;
run;
Result:
id bucket status 1 0 good 1 4 bad 1 0 recover 1 0 recover 1 1 recover 1 2 recover 1 3 recover 1 4 bad 2 0 good 2 4 bad 2 0 recover 2 0 recover 2 1 recover 2 2 recover 2 3 recover 2 4 bad
What should happen in this case?
id bucket status 42 0 good 42 0 ? 42 0 ? 42 1 ? 42 2 ? 42 3 ? 42 4 bad
Or is such a case not possible?
April 27 – 30 | Gaylord Texan | Grapevine, Texas
Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and save with the early bird rate—just $795!
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.