SAS Programming

DATA Step, Macro, Functions and more
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

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