BookmarkSubscribeRSS Feed
chsprogramming
Calcite | Level 5

I have a column with IDs that repeat, and I would like to translate the second column to repeat the first value for each unique ID. EXCEPT for when the value is NULL, the value needs to stay NULL. Below is an example of the column I have, and the column I want. 

ID  HAVE  WANT
1   12    12
1   19    12
1   23    12
2   40    40
2   NULL  NULL
2 25 40
3 NULL NULL
3 11 11
3 NULL NULL
4 24 24
4 NULL NULL
4 72 24
4 NULL NULL

 

1 REPLY 1
Patrick
Opal | Level 21

Something like below should do.

data have;
  input id have want;
  datalines;
1 12 12
1 19 12
1 23 12
2 40 40
2 . .
2 25 40
3 . .
3 11 11
3 . .
4 24 24
4 . .
4 72 24
4 . .
;

data want(drop=_:);
  set have;
  by id;
  retain _tmp;
  if first.id then call missing(_tmp);
  if not missing(have) then
    do;
      if missing(_tmp) then _tmp=have;
      derived=_tmp;
    end;
run;

proc print data=want;
run;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 1 reply
  • 370 views
  • 0 likes
  • 2 in conversation