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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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