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;
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 lock in 2025 pricing—just $495!
Still thinking about your presentation idea? The submission deadline has been extended to Friday, Nov. 14, at 11:59 p.m. ET.
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.