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?
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.