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

I'm trying to generate PDF files with diferent namen in each iteration. 

 

My code I need is:

 

%macro x1(i);

ODS PDF FILE='myfolders/newfile_&i';

ODS PDF CLOSE;

%mend;

 

%macro x2(init,end);

%do i=&init %to &end;

x1(&i)

%end

%mend;

 

%x2(1,2)

 

My problem is that I dont know how to update in the name of the new file the number i ( newfile_&i'; )

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

What do you want the file name to be? 

 

As is it will be 

 

new_file1

new_file2

 

Your also missing the extension which make cause issues. 

Oh...and double quotes not single quotes. Macro variables don't resolve in single quotes. 

And your second macro is pointless. Loop in your main macro. 

And you need a period to let SAS know where the end of the macro variable is. 

 


%macro x1(init, end);
 
%do I =&unit %to &bend;
ODS PDF FILE="myfolders/newfile_&i..pdf";
 
Title "This is report #&i.";
Proc print data = sashelp.class;
run;
 
ODS PDF CLOSE;
%end;
%mend;
 
 
%x1(1,2)

 








View solution in original post

6 REPLIES 6
Reeza
Super User

What do you want the file name to be? 

 

As is it will be 

 

new_file1

new_file2

 

Your also missing the extension which make cause issues. 

Oh...and double quotes not single quotes. Macro variables don't resolve in single quotes. 

And your second macro is pointless. Loop in your main macro. 

And you need a period to let SAS know where the end of the macro variable is. 

 


%macro x1(init, end);
 
%do I =&unit %to &bend;
ODS PDF FILE="myfolders/newfile_&i..pdf";
 
Title "This is report #&i.";
Proc print data = sashelp.class;
run;
 
ODS PDF CLOSE;
%end;
%mend;
 
 
%x1(1,2)

 








mariange8282
Obsidian | Level 7

thanks it works perfect! 

 

Kurt_Bremser
Super User

@mariange8282 wrote:

thanks but &i in 

ODS PDF FILE="myfolders/newfile_&i..pdf";

 not change the name, because the program assume that all in "....." is a string and not avaluate the variables. 

 


This is plain wrong.

If i is present as a macro variable, and has different values, this will work.

Did you run @Reeza's code as presented, to test the principle?

Reeza
Super User

@mariange8282 wrote:

thanks but &i in 

ODS PDF FILE="myfolders/newfile_&i..pdf";

 not change the name, because the program assume that all in "....." is a string and not avaluate the variables. 

 


I don't understand what your saying here. 

1. Does the code run as posted and generate two files?

2. How does it not meet your requirements. Not a mind reader here. Be explicit. 

Astounding
PROC Star

Your original post used single quotes, which prevent resolution of macro variables within.  The sample solutions use double quotes, which allow that resolution.  So that's part of the change you will need to make, switching from single to double quotes.

ballardw
Super User

In addition to the issue with single quotes:

 

%do i=&init %to &end;  

x1(&i)       <you do not call the X1 macro correctly inside %x2 should be %x1

%end       < No ; to complete the %end;

 

 

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 2141 views
  • 0 likes
  • 5 in conversation