如何平均计算SAS“ proc report”中的计算列

时间:2020-07-15 12:45:15

标签: sas mean proc-report

在SAS proc report中,计算的列逐行计算。 这也适用于摘要行,但这并不总是您想要的。

例如,以SASHELP.CLASS中的体重指数研究为例:

title Study Body Mass Index (BMI) by sex in class;
title2 Erroneously calculate average BMI from the average weight and height;
proc report data=sasHelp.class nowindows headline headskip split='*'
    style(summary) = {font_style=italic foreground=blue};

    where Name contains 'J'; * reduce the size to facilitate manual calculation ;
    columns sex name age height m weight kg BMI ;
    define sex    / group ;
    define age    / analysis mean format = 6.2;
    define height / analysis mean noprint;
    define weight / analysis mean noprint;
    define kg     / computed      format = 6.2 'Weight*(kg)';
    define m      / computed      format = 6.2 'Height*(meter)';
    define BMI    / computed      format = 6.2 'BMI*(kg/m²)';
    compute m;
        m = height.mean * .02540;
    endcomp;
    compute kg;
        kg = weight.mean * 0.45359237;
    endcomp;
    compute BMI;
        BMI = kg/m/m;
        if name eq '' then name = 'mean';
    endcomp;
    break after sex /summarize;
run;

这是错误的,因为BMI不在摘要中,即mean,不是上述BMI的平均值,而是根据其剩余的高度和重量来计算的。

这是一个正确的计算,是对BMI进行求和并手动计算学生人数。

title2 manually : summing BMI and counting students;
proc report data=sasHelp.class nowindows headline headskip split='*'
    style(summary) = {font_style=italic foreground=blue};

    where Name contains 'J'; * reduce the size to facilitate manual calculation ;
    columns sex name age height m weight kg BMI ;
    define sex      / group ;
    define age      / analysis mean format = 6.2;
    define height   / analysis mean noprint;
    define weight   / analysis mean noprint;
    define kg       / computed      format = 6.2 'weight*(kg)';
    define m        / computed      format = 6.2 'height*(meter)';
    define BMI      / computed      format = 6.2 'body mass*(kg/m²)';

    * initialize the sum and counter *;
    compute before sex;
        sumBMI = 0;
        count = 0;
    endcomp;

    compute m;
        m = height.mean * .02540;
    endcomp;
    compute kg;
        kg = weight.mean * 0.45359237;
    endcomp;
    compute BMI;
        if name eq '' then do;
            name = 'mean';

            * use the sum and counter *;
            BMI = sumBMI / count;
        end;
        else do;
            BMI = kg/m/m;

            * increase the sum and counter *
            sumBMI = sumBMI + BMI;
            count = count + 1;
        end;
    endcomp;
    break after sex /summarize;
run;

有没有办法让proc report本身正确地进行平均?

您可以说我想对计算列进行分析,但是您只能在输入数据集上定义一个列作为分析列。

1 个答案:

答案 0 :(得分:0)

  • 在现有数据集列中创建一个别名列。
  • 为别名列重做BMI计算。
  • 在摘要行中,将别名列均值应用于BMI列

在此示例中,使用列别名weight=bmiX

proc report data=sasHelp.class nowindows headline headskip split='*'
    style(summary) = {font_style=italic foreground=blue};

    where Name contains 'J'; * reduce the size to facilitate manual calculation ;
    columns sex name age height m weight kg weight=bmiX BMI ;
    define sex    / group ;
    define age    / analysis mean format = 6.2;
    define height / analysis mean ;
    define weight / analysis mean ;
    define kg     / computed      format = 6.2 'Weight*(kg)';
    define m      / computed      format = 6.2 'Height*(meter)';
    define BMI    / computed      format = 6.2 'BMI*(kg/m²)';

*   define bmiX   / noprint;

    compute m;
        m = height.mean * .02540;
    endcomp;
    compute kg;
        kg = weight.mean * 0.45359237;
    endcomp;
    compute BMI;
        BMI = kg/m/m;
        if name eq '' then do;
          name = 'mean';
          BMI = bmiX;
        end;
    endcomp;

    compute bmiX;
      bmiX = kg/m/m;
    endcomp;

    break after sex /summarize;
run;