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-white.png

Our biggest data and AI event of the year.

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.

 

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