Search code examples
sas

I need to add a new column test4 from test3 where test2 has a value 0. in SAS?


Input

Tl  LineNo Amnt_pd
01      0     .
01      1     93
01      2     0
01      3     25
01      4      0
02      0     40
02      1     0

Output

Tl  LineNo Amnt_pd  Amt_H
01      1     93    118
01      2     0     118
01      3     25    118
01      4     0     118
02      1     0      40

If the line no is zero for tl and amntpaid has value than keep the amntpaid value itself for rest of the rows of lineno. If lineno is zero and missing(.) then sum up the values for all other line no and fill the same sum amount for rest of the rows of Lineno for the new var Amnt_H. I need to code this in SAS. Anyhelp is appreciated?


Solution

  • Have a look at this already answered question.

    One way using a DoW Loop

    proc sort data=have; by Tl; run;
    
    data want;
        do _n_ = 1 by 1 until (last.Tl);
            set have;
            by Tl;
            Amt_H=sum(Amt_H, amnt_pd);
        end;
    
        do until (last.Tl);
            set have;
            by Tl;
            if LineNo ne 0 then output;
        end;
    run;
    
    Tl LineNo Amnt_pd Amt_H
    01    1     93     118
    01    2      0     118
    01    3     25     118
    01    4      0     118
    02    1      0      40