Summing a Column By Group In a Dataset With Macros

前端 未结 3 426
说谎
说谎 2021-01-27 11:34

I have a dataset that looks like:

 Month   Cost_Center      Account    Actual    Annual_Budget
 June     53410           Postage       13      234
 June     5342         


        
3条回答
  •  广开言路
    2021-01-27 12:10

    SAS DATA step behavior is quite complex ("About DATA Step Execution" in SAS Language Reference: Concepts). The default behavior, that you're seeing, is: at the end of each iteration (i.e. for each input row) the row is written to the output data set, and the PDV - all data step variables - is reset.

    You can't expect to write Base SAS "intuitively" without spending a few days learning it first, so I recommend using PROC SQL, unless you have a reason not to.

    If you really want to aggregate in data step, you have to use something called BY groups processing: after ensuring the input data set is sorted by the BY vars, you can use something like the following:

    data Test (keep = Month Account Sum_Actual Sum_Annual /*...your Run_Rate and Over_Budget_Alarm...*/);
        set Combined; /* the input table */
        by Month Account; /* must be sorted by these */
    
        retain Sum_Actual Sum_Annual; /* don't clobber for each input row */
        if first.account then do; /* instead do it manually for each group */
            Sum_Actual = 0;
            Sum_Annual = 0;
        end;
    
        /* accumulate the values from each row */
        Sum_Actual = sum(Sum_Actual, Actual);
        Sum_Annual = sum(Sum_Annual, Annual_Budget);
        /* Note that Sum_Actual = Sum_Actual+Actual; will not work if any of the input values is 'missing'. */
    
        if last.account then do;
            /* The group has been processed.
               Do any additional processing for the group as a whole, e.g.
               calculate Over_Budget_Alarm. */
            output; /* write one output row per group */
        end;
    run;
    

提交回复
热议问题