【问题标题】:Saving results from SAS proc freq with multiple tables使用多个表保存来自 SAS proc freq 的结果
【发布时间】:2015-03-15 13:30:29
【问题描述】:

我是 SAS 初学者,遇到以下问题。

我需要从一个数据集中计算几个变量 (A B C) 的计数和百分比,并将结果保存到另一个数据集中。 我的代码是:

proc 频率数据=mydata; 表 A B C / out=data_out ;运行;

每个变量的过程结果显示在 SAS 输出窗口中,但 data_out 仅包含最后一个变量的结果。如何将它们全部保存在 data_out 中? 任何帮助表示赞赏。

【问题讨论】:

    标签: sas sas-ods


    【解决方案1】:

    ODS OUTPUT 是您的答案。不能直接使用OUT=输出,但是可以这样输出:

    ods output OneWayFreqs=freqs;
    proc freq data=sashelp.class;
      tables age height weight;
    run;
    ods output close;
    

    OneWayFreqs 是单向表,(n>1)-way 表是 CrossTabFreqs:

    ods output CrossTabFreqs=freqs;
    ods trace on;
    proc freq data=sashelp.class;
      tables age*height*weight;
    run;
    ods output close;
    

    您可以通过运行ods trace on; 找到正确的名称,然后运行您的初始过程(到屏幕上);它会告诉你日志中输出的名称。 (ods trace off; 当你看腻了。)

    【讨论】:

    • 我在运行您的第一个示例时收到警告 - 使用此方法时,SAS 无法将所有 3 个表变量保留在同一输出数据集中。根据我的回答,我认为 proc summary 在这里做得更好。
    【解决方案2】:

    在这里学习很多好的基本 sas 东西

    1) 使用不同的输出数据集名称运行三个 proc freq 语句(每个变量 a b c 一个),这样数据集就不会被覆盖。

    2) 在 out = 语句中使用重命名选项来更改合并数据集时的计数和百分比变量

    3) 按类别排序,将所有数据集合并在一起

    (我假设多个变量中出现了一些值,如果没有,您可以堆叠数据集)

    data mydata;
        input a $ b $ c$;
        datalines;
    r r g
    g r b
    b b r
    r r r
    g g b
    b r r
    ;
    run;
    
    proc freq noprint data = mydata; 
        tables a / out = data_a 
        (rename = (a = category count = count_a percent = percent_a)); 
    run;
    proc freq noprint data = mydata; 
        tables b / out = data_b 
        (rename = (b = category count = count_b percent = percent_b)); 
    run;
    proc freq noprint data = mydata; 
        tables c / out = data_c 
        (rename = (c = category count = count_c percent = percent_c)); 
    run;
    
    proc sort data = data_a; by category; run;
    proc sort data = data_b; by category; run;
    proc sort data = data_c; by category; run;
    
    data data_out;
        merge data_a data_b data_c;
        by category;
    run;
    

    【讨论】:

    • 如果变量是数字/字符的混合怎么办?还是应用了格式?
    【解决方案3】:

    这个问题我已经处理过很多次了,我希望 SAS 有更好的方法来解决这个问题。

    我的解决方案是一个泛化的宏,提供您的输入数据、变量列表和输出数据集的名称。我考虑了你必须做的变量的格式/类型/标签

    希望对你有帮助:

    https://gist.github.com/statgeek/c099e294e2a8c8b5580a

    /* 描述:创建一个包含百分比/计数的变量的单向频率表 参数: dsetin - 输入数据集 varlist - 要分析的变量列表,用空格分隔 dsetout - 要创建的数据集的名称

    作者:F.Khurshed 日期:2011 年 11 月

    */

    %macro one_way_summary(dsetin, varlist, dsetout);
    
    proc datasets nodetails nolist;
        delete &dsetout;
    quit;
    
    *loop through variable list;
    %let i=1;
    %do %while (%scan(&varlist, &i, " ") ^=%str());
    %let var=%scan(&varlist, &i, " ");  
    
    %put &i &var; 
    
        *Cross tab;
        proc freq data=&dsetin noprint;
        table &var/ out=temp1;
        run;
    
        *Get variable label as name;
        data _null_;
            set &dsetin (obs=1);
            call symput('var_name', vlabel(&var.));
        run;
        %put &var_name;
    
        *Add in Variable name and store the levels as a text field;
        data temp2;
            keep variable value count percent;
            Variable = "&var_name";
            set temp1;
            value=input(&var, $50.);
            percent=percent/100; * I like to store these as decimals instead of numbers;
            format percent percent8.1;
            drop &var.;
        run;
    
        %put &var_name;
        *Append datasets;
        proc append data=temp2 base=&dsetout force;
        run;
    
        /*drop temp tables so theres no accidents*/
        proc datasets nodetails nolist;
            delete temp1 temp2;
        quit;
    
    *Increment counter;
    %let i=%eval(&i+1);
    %end;
    
    %mend;
    
    %one_way_summary(sashelp.class, sex age, summary1);
    
    proc report data=summary1 nowd;
        column variable value count percent;
        define variable/ order 'Variable';
        define value / format=$8. 'Value';
        define count/'N';
        define percent/'Percentage %';
    run;
    

    【讨论】:

      【解决方案4】:

      与以往一样,在 SAS 中有很多不同的方法来做这种事情。以下是其他几个选项:

      1。使用 proc summary 而不是 proc freq:

      proc summary data = sashelp.class;
          class age height weight;
          ways 1;
          output out = freqs;
      run;
      

      2。在单个proc freq 中使用多个表语句

      这比运行 3 个单独的 proc freq 语句更有效,因为 SAS 只需读取输入数据集一次而不是 3 次:

      proc freq data = sashelp.class noprint;
          table age       /out = freq_age;
          table height    /out = freq_height;
          table weight    /out = freq_weight;
      run;
      
      data freqs;
          informat age height weight count percent;
          set freq_age freq_height freq_weight;
      run;
      

      【讨论】:

        【解决方案5】:

        9.3 中添加到 PROC MEANS 的选项 STACKODS(OUTPUT) 使这项任务变得更加简单。

        proc means data=have n nmiss stackods;
          ods output summary=want;
        run;
        
        | Variable | N     | NMiss |
        | ------   | ----- | ----- |
        |        a |     4 |     3 |
        |        b |     7 |     0 |
        |        c |     6 |     1 |
        

        【讨论】:

        • 我还应该指出,这仅适用于数字变量
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-03
        • 1970-01-01
        • 1970-01-01
        • 2018-04-07
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多