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

I all. I have a couple of questions about macros.

Does this

%let year3 = 2019;                 

%let year1 = %eval(&year3-2);

 

Do the same as this?

%let year1 = 2017;                 

%let year3 = %eval(&year1+2);

 

Another let statement in the program is this

%let folderyear = 20172019;

 

How to change the "20172019" to macro

I’d like to change this to macro, using year1year3 instead?

 

and the program also has these lines

 

libname pop "\\datafolder\PopulationData";

set pop.pop2009_2018_agelevel (where=(&year1 le year le &year3) );

 

How do i change "pop2009_2018_agelevel" to also use macros, so it will use year3 and year3-9, so in this case it would use a file named pop2010_2019_agelevel?

I think i'd need another let statement at the top of the program, like 

%let year9 = %eval(&year3-9);

 

thanks!

 

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

You don't seem to be calling any macros in that code.  A call to a macro would look like:

%mymacro(parm1=1,parm2=abc)

You are just working with macro variables.  They are two different things.

 

It looks like for your application you just want to extract parts of the macro variable value. 

 

%SUBSTR() is a good function to use for that.

%let folderyear = 20172019;
%let year1=%substr(&folderyear,1,4);
%let year2=%substr(&folderyear,5);

 

Now that you have the two 4 digit strings in two macro variables you can use them to build your dataset name and WHERE= condition.

set pop.pop&year1._&year2._agelevel(where=(year between &year1. and &year2.));

Notice the periods after the macro variable names in building the dataset name.  That is critical for this to work.

Without them you would be looking for macro variables named YEAR1_ and YEAR2_AGELEVEL instead of YEAR1 and YEAR2.

 

View solution in original post

2 REPLIES 2
Tom
Super User Tom
Super User

You don't seem to be calling any macros in that code.  A call to a macro would look like:

%mymacro(parm1=1,parm2=abc)

You are just working with macro variables.  They are two different things.

 

It looks like for your application you just want to extract parts of the macro variable value. 

 

%SUBSTR() is a good function to use for that.

%let folderyear = 20172019;
%let year1=%substr(&folderyear,1,4);
%let year2=%substr(&folderyear,5);

 

Now that you have the two 4 digit strings in two macro variables you can use them to build your dataset name and WHERE= condition.

set pop.pop&year1._&year2._agelevel(where=(year between &year1. and &year2.));

Notice the periods after the macro variable names in building the dataset name.  That is critical for this to work.

Without them you would be looking for macro variables named YEAR1_ and YEAR2_AGELEVEL instead of YEAR1 and YEAR2.

 

geneshackman
Pyrite | Level 9
Thanks, I think this works.

Gene

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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
  • 2 replies
  • 846 views
  • 1 like
  • 2 in conversation