DATA Step, Macro, Functions and more

Multiple Do Loops, but Single RTF?

Reply
Contributor
Posts: 57

Multiple Do Loops, but Single RTF?

Hi Everybody,

I have the following program that runs a macro for two different periods and two different models. The issue is that each time the loop runs through the macro, a new RTF output gets generated. I was wondering if there was a way to generate a single RTF document with titles for each output (for each per and m pairs). I was suggested earlier that I put "ods rtf close" after the second %end, but SAS got stuck (runs forever) after this change. I was also suggested to add "ODS RTF startpage=now" where I identify the location of the RFT output. When I do that, SAS keeps crashing.

I attached the sample sas dataset to this inquiry. Thank you for your time and help in advance.

%macro twostep;

    %do per=1 %to 2;

        %do m=1 %to 2;

%let model1= vret_msa mktrf100;    *model specification;

%let model2= vret_msa mktrf100 ind_ret;

data perret&per; set a14_samplesas; if period=&per;

proc sort data=perret&per; by permno myear; run;

ods listing close;

ods output parameterestimates=pe&per&m;

proc reg data=perret&per;

by permno;

    model ereturn_i =&&model&m;

run;

quit;

ods listing;

ods rtf file="C:\SAS\twostep\period&per model&m.rtf" style=journal;

proc means data=pe&per&m mean std t probt;

var estimate; class variable;

run;

*By variable by variable, it calculates the mean, standard deviation, t-statistics and p-value of the

coefficient estimates;

proc sort data=pe&per&m; by variable; run;

ods rtf close;

%end;

    %end;

%mend;

%twostep;

Attachment
Super User
Posts: 11,343

Re: Multiple Do Loops, but Single RTF?

Posted in reply to finans_sas

You just need to add the appropriate TITLE statements in the code body. For example just before the Proc Reg or Means something like:

title "This is the title for PER= &Per M= &m";

To create a single RTF or other ODS output file type put all of the output generating code within a single ODS "sandwich".

Since you are running multiple nested loops, probably what you want is to move ODS RTF file = to before the %do Per = loop and have the ODS RTF close after the associated %end. Since the ODS RTF precedes the macor then you'd likely want a fixed file name.


Contributor
Posts: 57

Re: Multiple Do Loops, but Single RTF?

Thank you, ballardw. I followed your suggestions, and the macro ended up generating one single output. However, in the original code that I posted, ODS output is whatever comes out of proc means statement. On the other hand, after the changes that I made, ODS output gives me a output for each permno (regression ids) in addition to the result of proc means operation, which is in hundreds of pages. I am not sure how to go from here. I would deeply appreciate your input.

Super User
Posts: 7,811

Re: Multiple Do Loops, but Single RTF?

Posted in reply to finans_sas

Use NOPRINT in the PROC REG statement.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Super User
Posts: 11,343

Re: Multiple Do Loops, but Single RTF?

Posted in reply to finans_sas

I was afraid that would happen. I generally build all datasets outside of output and then have another loop or call to create the output file..

So take the ODS RTF and the Proc means out side of the current loops you have.

Then add the ODS RTF sandwich around a second loop of similar structure that just runs the proc means part.

ods rtf file="your name here";

%do per=1 %to 2; 

        %do m=1 %to 2;

          title "Output for Per=&per and M=&m";

          proc means data=pe&per&m mean std t probt;

          var estimate; class variable;

          run;

     %end;

%end;

ods rtf close;

Trusted Advisor
Posts: 3,214

Re: Multiple Do Loops, but Single RTF?

Posted in reply to finans_sas

use of proc document? SAS(R) 9.3 Output Delivery System: User's Guide, Second Edition

---->-- ja karman --<-----
Ask a Question
Discussion stats
  • 5 replies
  • 625 views
  • 3 likes
  • 4 in conversation