Search code examples
sasretain

SAS Fill In Blanks With Retain Statement


So I am trying to retain values on SAS but with a condition that includes my ID field. I have the following dataset:

ID Amount
1 230
1 450
1 .
1 .
2 .
2 .

I want to retain provided the ID is the same and zero if its a dot, see results below:

ID Amount
1 230
1 450
1 450
1 450
2 0
2 0

Solution

  • How about the UPDATE trick.

    data test;
       infile cards expandtabs;
       input id amount;
       cards;
    1   230
    1   450
    1   .
    1   .
    2   .
    2   .
    ;;;;
       run;
    proc print;
       run;
    data test2;
       update test(obs=0) test;
       by id;
       if first.id and missing(amount) then amount=0;
       output;
       run;
    proc print;
       run;
    

    enter image description here