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;
Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.
Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.
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.
Ready to level-up your skills? Choose your own adventure.