Macro that outputs table with testing results of SAS table

后端 未结 2 1149
野的像风
野的像风 2021-01-27 08:51

Problem

I\'m not a very experienced SAS user, but unfortunately the lab where I can access data is restricted to SAS. Also, I don\'t currently have access to the data

2条回答
  •  深忆病人
    2021-01-27 09:10

    Here are some initial steps and pointers in a non macro approach which restructures the data in such a manner that no array processing is required. This approach should be good for teaching you a bit about manipulating data in SAS but will not be as fast a single pass approach (like the macros you originally posted) as it transposes and sorts the data.

    First create some nice looking dummy data.

    /* Create some dummy data with three variables to assess */
    data have;
        do firm = 1 to 3;
            revenue = rand("uniform");
            costs = rand("uniform");
            profits = rand("uniform");
            output;
        end;
    run;
    

    Transpose the data so all the values are in one column (with the variable names in another).

    /* Move from wide to deep table */
    proc transpose 
            data = have 
            out = trans 
            name = Variable;
        by firm;
        var revenue costs profits;
    run;
    

    Sort the data so each variable is in a contiguous group of rows and the highest values are at the end of each Variable group.

    /* Sort by Variable and then value 
        so the biggest values are at the end of each Variable group */
    proc sort data = trans;
        by Variable COL1;
    run;
    

    Because of the structure of this data, you could go down through each observation in turn, creating a running total, which when you get to the final observation in a Variable group would be the Variable total. In this observation you also have the largest value (the second largest was in the previous observation).

    At this point you can create a data step that:

    • Is aware when it is in the first and last values of each variable group
      • by statement to make the data step aware of your groups
      • first.Variable temporary variable so you can initialise your total variable to 0
      • last.Variable temporary variable so you can output only the last line of each group
    • Sums up the values in each group
      • retain statement so SAS doesn't empty your total with each new observation
      • sum() function or + operator to create your total
    • Creates and populates new variables for the largest and second largest values in each group
      • lag() function or retain statement to keep the previous value (the second largest)
    • Creates your flag
    • Outputs your new variables at the end of each group
      • output statement to request an observation be stored
      • keep statement to select which variables you want

    The macros you posted originally looked like they were meant to perform the analysis you are describing but with some extras (only positive values contributed to the Total, an arbitrary number of values could be included rather than just the top 2, the total was multiplied by another variable k1198, negative values where caught in the second largest, extra flags and values were calculated).

提交回复
热议问题