I have the data like below;
data have;
input Id 1-2 name $ 4.;
cards;
1
2
3
4
5 x
6
7
8
9
10
11 y
;
and the expected out put is
data want output:
ID Name
1 x
2 x
3 x
4 x
5 x
6 y
7 y
8 y
9 y
10 y
11 y
;
data have;
infile cards truncover;
input Id name $;
cards;
1
2
3
4
5 x
6
7
8
9
10
11 y
;
run;
proc sort data=have out=temp;
by descending id;
run;
data want;
set temp;
length new_name $ 40;
retain new_name;
if not missing(name) then new_name=name;
run;
proc sort data=want;
by id;
run;
Your data suggests the non-missing value at the end of the series. First, read the data from backwards, collect the non-missing NAME.
Then fill the missing Names. Then Sort the temporary data set by ID.
data have; input Id 1-2 name $ 4.; cards; 1 2 3 4 5 x 6 7 8 9 10 11 y ; run; data check; do i = num to 1 by -1; set have nobs = num point = i; if name NE ' ' then temp = name; else name = temp; output; end; stop; drop temp; run; proc sort data = check out = want; by id; run; proc print data = want; run;
data have;
infile cards truncover;
input Id name $;
cards;
1
2
3
4
5 x
6
7
8
9
10
11 y
;
run;
proc sort data=have out=temp;
by descending id;
run;
data want;
set temp;
length new_name $ 40;
retain new_name;
if not missing(name) then new_name=name;
run;
proc sort data=want;
by id;
run;
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
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.