Skip to content

Instantly share code, notes, and snippets.

@statgeek
Last active July 10, 2020 03:50
Show Gist options
  • Save statgeek/264fe76bdccb6b137215 to your computer and use it in GitHub Desktop.
Save statgeek/264fe76bdccb6b137215 to your computer and use it in GitHub Desktop.
Using SAS to Append PDF files together
******************************************************************
This requires Adobe Acrobat Professional
List files to append in order
******************************************************************;
Data Files2Append;
length file $256.;
file="C:\Test\Sample_out.PDF"; output;
file="C:\Test\Sample_out 2.PDF"; output;
run;
*Get number of files to process;
proc sql noprint;
select count(*) into :num_append from Files2Append;
quit;
%put Number of files to Append: &num_append;
Data Dim_Declare;
do i=1 to &num_append;
row=put(i, 2. -l);
Code=catt("Dim Doc", row);
output;
end;
do i=1 to &num_append;
row=put(i, 2. -l);
Code=catt("Set Doc", row, '=CreateObject("AcroExch.PDDoc")');
output;
end;
keep Code;
run;
Data Append_Files;
length code $2000.;
set Files2Append;
row=put(_n_, 2. -r);
Code=cat(compress('file'|| row),compress(" = Doc"|| row), '.Open(', quote(trim(file)), ")");
Keep Code;
run;
Data Stack_Files;
do i=2 to &num_append;
doc_id=compress("Doc"||put(i, 2. -l));
doc_pages=compress(doc_id||".GetNumPages");
Code=cat("Stack = Doc1.InsertPages(Doc1.GetNumPages - 1, ", doc_id, ", 0, ", doc_pages, " ,0 )");
output;
end;
******************************************************************
Change the final output file name/location here
******************************************************************;
Code=cat("SaveStack=Doc1.Save(1, ", quote("C:\Test\Stacked_Files.PDF"), ")"); output;
run;
*OUTPUT VBS CODE;
filename temp "C:\Test\Append_Files.vbs";
DATA allcode;
set Dim_Declare Append_Files Stack_Files;
file temp;
put code;
RUN;
*RUN VBS PROGRAM;
%sysexec "C:\Test\Append_Files.vbs";
@statgeek
Copy link
Author

statgeek commented Jul 15, 2019 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment