使用多个表保存SAS proc freq的结果

时间:2015-01-16 20:48:20

标签: sas sas-ods

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

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

proc freq data = mydata; 表A B C / out = data_out;运行;

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

5 个答案:

答案 0 :(得分:2)

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

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

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

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

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

答案 1 :(得分:0)

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

1)使用不同的输出数据集名称运行三个proc freq语句(每个变量一个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;

答案 2 :(得分:0)

这是我多次处理的一个问题,我希望SAS有更好的方法来做到这一点。

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

希望有所帮助:

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

/ * 描述:创建一个包含百分比/计数的单向Freq变量表 参数: dsetin - inputdataset 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;

答案 3 :(得分:0)

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

1。使用proc摘要而不是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;

答案 4 :(得分:0)

在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 |
相关问题