BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
vThanu
Calcite | Level 5

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
;

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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;

View solution in original post

2 REPLIES 2
KachiM
Rhodochrosite | Level 12

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;
Ksharp
Super User
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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 2 replies
  • 6450 views
  • 1 like
  • 3 in conversation