DATA Step, Macro, Functions and more

retain the last values

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 13
Accepted Solution

retain the last values

I have been trying to get this input:

ID     VAR_1       VAR_2      VAR_3

001      1                              3    

001                    2

002                    2

002    1

002                     2               3

To this output:

ID     VAR_1       VAR_2     VAR_3

001     1               2               3

002     1               2               3

Here is my test dataset. I used RETAIN to get them all line up at the end, then take the LAST.ID. However the retain does not seem to work...

data a;

input id $ 1-3 var_1 $ 4-4 var_2 $ 5-5 var_3 $ 6-6;

cards;

0011 3

001 2

002 23

0021

002 3

;

Any ideas. Thanks!

-K


Accepted Solutions
Solution
‎01-03-2012 05:14 PM
Super User
Super User
Posts: 6,498

Re: retain the last values

Use the UPDATE statement.  Only non-missing values will cause changes to previous value.

data have ;

input ID VAR_1 VAR_2 VAR_3 ;

cards;

001 1 . 3

001 . 2 .

002 . 2 .

002 1 . .

002 . 2 3

run;

data want ;

  update have (obs=0) have;

  by id;

run;

data _null_;

  set want ;

  put (_all_) (=);

run;

View solution in original post


All Replies
Solution
‎01-03-2012 05:14 PM
Super User
Super User
Posts: 6,498

Re: retain the last values

Use the UPDATE statement.  Only non-missing values will cause changes to previous value.

data have ;

input ID VAR_1 VAR_2 VAR_3 ;

cards;

001 1 . 3

001 . 2 .

002 . 2 .

002 1 . .

002 . 2 3

run;

data want ;

  update have (obs=0) have;

  by id;

run;

data _null_;

  set want ;

  put (_all_) (=);

run;

Occasional Contributor
Posts: 13

Re: retain the last values

Thanks Tom, that was quickSmiley Happy

☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 2 replies
  • 332 views
  • 0 likes
  • 2 in conversation