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;

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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