BookmarkSubscribeRSS Feed
somebody
Lapis Lazuli | Level 10

I would like to output multiple graphs to PDF. For now, I draw my graphs using a macro that outputs 3 graphs (this number can be changed to 5 or 6). I run this macro several times for different datasets. 

What I would like to achieve is to have each PDF page contain 3 graphs from the macro.

So far, I have tried this:

data ibm intel microsoft; set sashelp.stocks;
	if stock='IBM' then output IBM;
	if stock='Intel' then output Intel;
	if stock='Microsoft' then output Microsoft;
run;
%macro graph(data);
	proc sgplot data=&data.;title "&data. : open";series x=date y=open;run;
	proc sgplot data=&data.;title "&data. : close";series x=date y=close;run;
	proc sgplot data=&data.;title "&data. : volume";series x=date y=volume;run;
%mend;
ods _all_ close; 

ods pdf file = "test.pdf" startpage=never ;

ods layout gridded rows=2 columns=2 column_widths=(3.75in 3.75in);
	ods region;
	ods graphics / height=3in width=3.5in;;
	%graph(ibm);

	ods region;
	ods graphics / height=3in width=3.5in;;
	%graph(intel);

	ods region;
	ods graphics / height=3in width=3.5in;;
	%graph(microsoft);

	ods region;
	ods graphics / height=3in width=3.5in;;
	%graph(ibm);

ods layout end;
ods pdf close;

Which is not really what I want because it puts the 3 graphs from the macro on 1 side of the page, and other 3 from running the macro again 

Also, I don't know why there is a title on each page.

What have I done wrong? and what if the macro returns 5 graphs? In this case, I would still want to have 4 per page, and the next page would contain only the fifth graph. graphs from running the macro will start on a new page.

 

2 REPLIES 2
ballardw
Super User

The Title output you get multiple times because EACH proc call will display the current title statements in effect at that time.

So if you only want a title to appear before one plot then (dummy code)

Title "Title statement";

proc sgplot data= ..
   <plot statements>
run;

Title; /* this turns of the title for the next plot call*/

proc sgplot data= ..
   <plot statements>
run;

proc sgplot data= ..
   <plot statements>
run;

You may also want to investigate the ODS PDF option Nogtitle to place the title into the PDF portion of the output instead of in the graphs.

Your demonstrated code will generate 12 plots but you talk about wanting each page to contain 3 graphs but do not tell us which 3 graphs.

 

It sounds like you may want to insert ODS PDF Startpage=NOW statements at some point, either between macro calls or between the SGPLOT statements but I'm not sure.

 

Again dummy code to create a new page before each macro plot call. I remove the layout stuff as I am not sure what you actually are attempting. Your layout settings may be part of your issue with the pagination desire.

ods pdf file = "test.pdf" startpage=never ;

	ods graphics / height=3in width=3.5in;;
	%graph(ibm);
ODS PDF Startpage=now;
	
	ods graphics / height=3in width=3.5in;;
	%graph(intel);

ODS PDF Startpage=now;

	
	ods graphics / height=3in width=3.5in;;
	%graph(microsoft);

ODS PDF Startpage=now;
	
	ods graphics / height=3in width=3.5in;;
	%graph(ibm);

ods pdf close;

 

somebody
Lapis Lazuli | Level 10

Thank you for your reply. I tried your suggestions and it works with starting a new PDF page for each macro graph. However, it does not stack the graphs together to have 2x2 graphs per pdf page. It returns 4 graphs (1x4). Would you have any idea on how to spread these 4 graphs into 2 columns?

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 2 replies
  • 648 views
  • 0 likes
  • 2 in conversation