The SAS Output Delivery System and reporting techniques

Creating multiple ODS PDF pages in a data step

Senior User
Posts: 1

Creating multiple ODS PDF pages in a data step

I am trying to create multiple PDF files with each one more more pages. The code that I am trying to use is shown below. If the ID in the data changes, a new PDF file should be generated, if it is the same as its lag, a new PDF page should be appended to the previously opened PDF.


Running the code, all pages seem to append to the first opened pdf file, without pagebreaks in between (even though I have specified ods startpage = now). The macro %print_data consists of some analysis and some report generating, a general structure can be seen below (there is a bit more to it, but in general that's all it does).


I hope someone can help me fix my program to get the desired outcome.



data _null_;
	set dataset;
	lag_id = lag(id);

	if lag_id ne id then
			ods pdf close;
			*Generate new PDF!;
			call symput('filename',id_name);
			ods pdf file="C:\&filename..pdf'" startpage=never;
			call execute('%nrstr(%print_data('||column1||','||column2||'))');
			*Append to previously opened PDF. Create new page;
			ods pdf startpage=now;
			call execute('%nrstr(%print_data('||column1||','||column2||'))');



%macro print_data(key1,key2);

ods graphics off; /* or use the %ODSOff macro */

ods exclude all; /* suspend all open destinations */

 data prep;
set full-database;
where id_key1 = &key1.;

ods exclude none; /* or use the %ODSOn macro */
ods graphics on / height=8cm;

proc print data=prep;
Super User
Posts: 13,583

Re: Creating multiple ODS PDF pages in a data step

Posted in reply to daniel23523523

I don't believe that SAS can "append" to an exsiting PDF. I would suggest making a control data set from your data to have one record for each set of values you want to process.


Maybe something along these lines:

proc sql;
   create table control as
   select distinct id_name,column1,column2
   from dataset
   order by id_name ;
data _null_;
	set control;
   by id_name;
   length filenamestr $ 200;
   retain filenamestr;
   if first.id_name then do;
      filenamestr = quote(cats('C:\',id_name,'.pdf'));
   	call execute ('ods pdf file='||filenamestr||' startpage=never;');
	call execute('%nrstr(%print_data('||column1||','||column2||'))');
   if last.id_name then call execute('ods pdf close:');
Posts: 9,371

Re: Creating multiple ODS PDF pages in a data step

BallardW is correct. PDF files cannot be appended. That only works for ODS HTML files.

The challenge with ODS PDF is that the PDF destination has to conform to the Adobe specification and by design, all PDF files have beginning and ending information that can ONLY appear 1 time in each PDF file. So there would NOT be a way to create a PDF file for example at 8 AM and close it, but then append to it at 10 AM. As soon as the PDF file is opened (when the ODS PDF FILE= is encountered) the beginning information is written. When the ODS PDF CLOSE is encountered, the ending information is written. And, once written, cannot be "appended to" except with a proprietary Adobe product to combine PDFs or a 3rd party product that allows you to combine PDFs together.

Ask a Question
Discussion stats
  • 2 replies
  • 3 in conversation