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

Hello all.  I have a question about using %let statements as a shortcut for full filenames.  Could someone tell me why this works:

%let file2005 = "/datapath/2005/filename2005";

But this doesn't:

%macro yrloop(firstyr,lastyr);

%do yr=&firstyr. %to &lastyr;

%let file&yr.= "/datapath/&yr./filename&yr.";

%end; %mend;

%yrloop(2005,2005);

How do I get the &yr. macro to resolve in the let statement?  Thanks!

1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

It works fine....what do you mean it doesn't work? I suspect you're running into a scope issue, the macro variable only exists within that macro and doesn't exist outside unless you create it as a global macro variable.


%macro yrloop(firstyr,lastyr);

%do yr=&firstyr. %to &lastyr;
%global file&yr.;

%let file&yr.= "/datapath/&yr./filename&yr.";

%put &&file&yr.;

%end; 

%mend;

%yrloop(2000,2005);

%put &file2000.;

%put &file2005.;

@JenniferBernard wrote:

Hello all.  I have a question about using %let statements as a shortcut for full filenames.  Could someone tell me why this works:

%let file2005 = "/datapath/2005/filename2005";

But this doesn't:

%macro yrloop(firstyr,lastyr);

%do yr=&firstyr. %to &lastyr;

%let file&yr.= "/datapath/&yr./filename&yr.";

%end; %mend;

%yrloop(2005,2005);

How do I get the &yr. macro to resolve in the let statement?  Thanks!




View solution in original post

3 REPLIES 3
Reeza
Super User

It works fine....what do you mean it doesn't work? I suspect you're running into a scope issue, the macro variable only exists within that macro and doesn't exist outside unless you create it as a global macro variable.


%macro yrloop(firstyr,lastyr);

%do yr=&firstyr. %to &lastyr;
%global file&yr.;

%let file&yr.= "/datapath/&yr./filename&yr.";

%put &&file&yr.;

%end; 

%mend;

%yrloop(2000,2005);

%put &file2000.;

%put &file2005.;

@JenniferBernard wrote:

Hello all.  I have a question about using %let statements as a shortcut for full filenames.  Could someone tell me why this works:

%let file2005 = "/datapath/2005/filename2005";

But this doesn't:

%macro yrloop(firstyr,lastyr);

%do yr=&firstyr. %to &lastyr;

%let file&yr.= "/datapath/&yr./filename&yr.";

%end; %mend;

%yrloop(2005,2005);

How do I get the &yr. macro to resolve in the let statement?  Thanks!




JenniferBernard
Obsidian | Level 7

Thank you so much!  This was the issue exactly.  I needed to call them outside the macro.

mklangley
Lapis Lazuli | Level 10

Try defining the macro variable as global inside the macro:

%macro yrloop(firstyr,lastyr);
    %do yr=&firstyr. %to &lastyr;
        %global file&yr.;
        %let file&yr.= "/datapath/&yr./filename&yr.";
    %end; 
%mend;
%yrloop(2005,2005);

%put &=file2005;

SAS Innovate 2025: Call for Content

Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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.

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
  • 3 replies
  • 1142 views
  • 3 likes
  • 3 in conversation