本文介绍了SAS:如何从 %DO 循环保存多个 ODS 输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

data scores;
   length name $ 12;
   input name $ score1 score2;
   datalines;
Riley 1132 1187
Henderson 1015 1102
;

%MACRO DO_MEAN;
%DO I = 1 %TO 10;
 PROC MEANS data = scores;
 VAR score1;
 TITLE "Average math score";
 RUN;
%END;
%MEND DO_MEAN;

ods output Summary = Summary;
%DO_MEAN;

我有一个名为 %DO_MEAN 的简单宏,用于计算数据集中的平均值 score1.我希望宏从 i = 1 运行到 10 次.

I have a simple macro called %DO_MEAN that calculates the mean score1 in my dataset. I want the macro to run from i = 1 to 10 times.

调用宏会输出 MEANS 过程的结果 10 次.是的,我知道输出完全相同,但我只是想使用这个可重现的示例来进行说明.

Invoking the macro outputs the results of the MEANS procedure 10 times. And yes, I know the output is exactly the same, but I just wanted to use this reproducible example for the sake of illustration.

在调用宏来存储我的 10 个输出之前,我尝试使用 ods output Summary = Summary;.但是,生成的 Summary 数据集只有 1 行.如何调整我的代码以便我可以存储所有 10 个输出?

I tried using ods output Summary = Summary; before invoking the macro to store my 10 outputs. However, the resulting Summary dataset only has 1 row. How can I adjust my code so that I can store all 10 outputs?

我希望上面生成的 ods 数据集有 10 行,每行对应于 %DO 循环的迭代之一.

I'd like the resulting ods dataset above to have 10 rows, each corresponding to one of %DO loop's iterations.

推荐答案

您可以在 ODS OUTPUT 语句中使用 PERSIST 选项,记录在此处:http://documentation.sas.com/?docsetId=odsug&docsetTarget=p0oxrbinw6fjuwn1x23qam6dntyd.htm&docsetVersion=9.4&locale=en

You can use the PERSIST option on the ODS OUTPUT statement, documented here:http://documentation.sas.com/?docsetId=odsug&docsetTarget=p0oxrbinw6fjuwn1x23qam6dntyd.htm&docsetVersion=9.4&locale=en

ods output parameterestimates (persist=proc) = want;
proc reg data=sashelp.class;
where sex='F';
model height = weight;
run;

proc reg data=sashelp.class;
where sex='M';
model height = weight;
run;

quit;

ods output clear;

但最好的答案是首先不要这样做,而是使用 BY 组.

But the best answer is don't do this in the first place, use BY groups instead.

proc sort data=sashelp.class out=class; by sex; run;

ods output ParameterEstimates = want2;
proc reg data=class;
by sex;
model height = weight;
run;

您可以使用 ods 输出ods 表,但我在本示例中同时使用了两者并对其进行了修改以保持一致.

you can use ods output or ods table, but I used both in this example and modified it to be consistent.

这篇关于SAS:如何从 %DO 循环保存多个 ODS 输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 04:08