BookmarkSubscribeRSS Feed
daniel23523523
Calcite | Level 5

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
		do;
			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||'))');
		end;
	else
		do;
			*Append to previously opened PDF. Create new page;
			ods pdf startpage=now;
			call execute('%nrstr(%print_data('||column1||','||column2||'))');
		end;
run;

 

 

%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.;
run;

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

proc print data=prep;
run;
%mend;
2 REPLIES 2
ballardw
Super User

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 ;
quit; 
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;');
   end;
	call execute('%nrstr(%print_data('||column1||','||column2||'))');
   if last.id_name then call execute('ods pdf close:');
run;
Cynthia_sas
SAS Super FREQ
Hi:
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.

cynthia

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 2 replies
  • 3021 views
  • 0 likes
  • 3 in conversation