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!
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!
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!
Thank you so much! This was the issue exactly. I needed to call them outside the macro.
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;
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!
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.