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;
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;
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.