I wish base SAS had an linked list object. I've needed something like this before. And I ache for the days I could find a C library or even make a linked list object to code with. A hash object could be used but it is not neccessary, and it doesn't help code readability, IMO. Even though I've done this before with my own data, doing it with this data I feel like I came up with a completely different solution than I remembered before. This code assumes a well formed input dataset, as I understand the problem.
data have;
length n 8 col1 $ 1 col2 8 col3 $ 3 ;
infile datalines delimiter=',' truncover;
input col1 col2 col3 ;
n=_N_;
datalines;
A,1,
A,2,foo
A,3,
B,1,bar
B,2,
B,1,
B,2,foo
C,1,
C,2,bar
C,3,
C,4,
C,3,
C,2,
C,3,
C,4,
D,1,
D,2,
D,3,
;
run;
data want;
do until (last.col1);
set have;
by col1;
length _col2_x 8 col4 $3;
retain _col2_x col4;
/*_col2_x is the value col2 equaled when col3 was not missing; IOW the
col3 property set to be applied to the children of that parent. */
drop _:;
if not missing(_col2_x) then do;
if _col2_x>=col2
then call missing(_col2_x,col4);
end;
if not missing(col3) then do;
_col2_x=col2;
col4=col3;
end;
output;
end;
call missing(_col2_x, col4);
run;
Concepts for anyone looking up these methods, search for DOW loops, aka. Whitlock loops.
... View more