BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
imath
Calcite | Level 5

Hello,

 

I have a problem that I can't solve properly if anyone can help.

 

I'm working on automatizing pdf outputs using ods pdf and proc report.

 

I have two data sets Data1 and Data2. I want to produce pdf files using a table from Data1 and another table from Data2. I use the option 'newfile=bygroup' as the same groups are in both datasets. I would like to put both tables in the same page (pdf).

 

Here's the code of what i do:

 

options nodate nonumber;
ods pdf file="...../MyPDF1.pdf" style=sasref newfile=bygroup;
proc report data=Data1 nowd;
    by myGroup;

     ........
proc report data=Data2 nowd;
    by myGroup;

     .......
run;

ods pdf close;
run;

 

By doing this, i produce pdf files by group but separately from each data set. I would like to put for each group the two tables together in one page.

 

It would be helpful if anyone has an isea or a hint.

 

Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
howarder
Obsidian | Level 7

Below is a macro that should help with your request of getting both Proc Report outputs on one PDF file and also renames the PDF output based on the group number. You don't need the Proc SQL code if you already know what the first and last group numbers are. But you can use it if you want SAS to automate the values for the do loop if this is always changing. "Startpage = Never" option on the ods pdf statement is used to not create a page break between the output. Hopefully this works for you.

  

options nodate nonumber;
%macro reporting;
proc sql;
	select min(mygroup),max(mygroup) into :firstgrp, :lastgrp from Data1;
quit;

	%do i = &firstgrp. %to &lastgrp.;
	    ods pdf file="...../MyPDF1 - Group &i..pdf" style=sasref startpage=never;

		proc report data=Data1 nowd;
		    where myGroup = &i.;
		    by myGroup;
		     ........
run;
proc report data=Data2 nowd; where myGroup = &i.; by myGroup; ....... run; ods pdf close; %end; %mend reporting; %reporting;

 

View solution in original post

10 REPLIES 10
Reeza
Super User

Unfortunately that won't work with the newfile option and you'll have to write a macro 😞

 

 

howarder
Obsidian | Level 7

Below is a macro that should help with your request of getting both Proc Report outputs on one PDF file and also renames the PDF output based on the group number. You don't need the Proc SQL code if you already know what the first and last group numbers are. But you can use it if you want SAS to automate the values for the do loop if this is always changing. "Startpage = Never" option on the ods pdf statement is used to not create a page break between the output. Hopefully this works for you.

  

options nodate nonumber;
%macro reporting;
proc sql;
	select min(mygroup),max(mygroup) into :firstgrp, :lastgrp from Data1;
quit;

	%do i = &firstgrp. %to &lastgrp.;
	    ods pdf file="...../MyPDF1 - Group &i..pdf" style=sasref startpage=never;

		proc report data=Data1 nowd;
		    where myGroup = &i.;
		    by myGroup;
		     ........
run;
proc report data=Data2 nowd; where myGroup = &i.; by myGroup; ....... run; ods pdf close; %end; %mend reporting; %reporting;

 

Reeza
Super User

I think you'd only need the WHERE clause, not both the WHERE and BY.

 

		    where myGroup = &i.;
		    by myGroup;
Reeza
Super User

I think you'd only need the WHERE clause, not both the WHERE and BY.

 

		    where myGroup = &i.;
		    by myGroup;
howarder
Obsidian | Level 7

That is correct. Thanks for pointing that out. I guess I am used to sorting by another variable. 

imath
Calcite | Level 5

It works, thank you!

 

Basically, 'myGroup' is a character type (a name), so i just introduced a variable of counting fllowing myGroup and used it instead of myGroup. Unfotunately, the name of PDF is a number now. It would be perfect if i can name the file with the name of myGroup. Any idea?

 

Thank you again!!

howarder
Obsidian | Level 7

The easiest way to get the name of the group in the pdf filename is to incorporate a proc sql to create a macro variable based on the group name in the do loop. That way you can use the group name macro variable in the ods pdf file statement.

 

  %do i = &firstgrp. %to &lastgrp.;

        proc sql;
	   select distinct(mygroup) into :grpname from Data1 where mygroup = &i.;
        quit;
	    ods pdf file="...../MyPDF1 - &grpname..pdf" style=sasref startpage=never;
imath
Calcite | Level 5

Perfect! Thank you so much!

Spoiler
 

Smiley Happy

imath
Calcite | Level 5

Hello,

 

I use a proc template (that i use in the code : style = sasref) to manipulate the output of my tables, but it seems that i can't use different templates for my two tables; is there a possibility to use different templates for different tables created in one pdf page?

 

Thank you.

Reeza
Super User

@imathThis is a new question, I would recommend a new thread.

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 10 replies
  • 4296 views
  • 2 likes
  • 3 in conversation