问题
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;
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.
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.
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?
I'd like the resulting ods dataset above to have 10 rows, each corresponding to one of %DO loop's iterations.
回答1:
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;
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;
EDIT: you can use ods output or ods table, but I used both in this example and modified it to be consistent.
回答2:
If you want to store the output from each run; you will have to save them to the same data set by appending your results. So, use PROC APPEND.
Note: I moved Your ODS Output and Proc append inside your loop, so the ods table will be appended with each loop:
Code:
/*Create an empty base table*/
proc sql;
create table WORK.ods_output
(
score1_N num format=BEST1. label='N',
score1_Mean num format=BEST12. label='Mean',
score1_StdDev num format=BEST12. label='Std Dev',
score1_Min num format=BEST12. label='Minimum',
score1_Max num format=BEST12. label='Maximum'
);
quit;
%MACRO DO_MEAN;
%DO I = 1 %TO 10;
PROC MEANS data = scores;
VAR score1;
TITLE "Average math score";
RUN;
ods output Summary = Summary;
proc append base=ods_output data=summary; quit;
%END;
%MEND DO_MEAN;
%DO_MEAN;
Output table with 10 rows: (ignore the log warnings)
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
score1_N=2 score1_Mean=1073.5 score1_StdDev=82.731493399 score1_Min=1015 score1_Max=1132
来源:https://stackoverflow.com/questions/49523197/sas-how-to-save-multiple-ods-output-from-a-do-loop