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;

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

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