Hello,
I need to create a series of macro variables to assign each year between two given years.
For instance, without using any loop I have to assign them one by one:
%let yr1= 2020;
%let yr2= 2021;
%let yr3= 2022;
However, this is not very efficient , if range becomes wider.
My idea is :
%let start = 2010;
%let end = 2012;
.............................
.............................
Thank you.
As always, context and purpose of these macro variables is important, and you don't tell us what the larger problem is or why you need these macro variables. I get the feeling that maybe there are easier ways where you won't need this sequence of macro variables, so please please please tell us what the larger problem is and why you need these macro variables.
Anyway, this is one way to do this.
%let start_yr=2010;
%let end_yr=2022;
data _null_;
do i=1 to &end_yr-&start_yr+1;
call symputx(cats("year",i),&start_yr+i-1);
end;
run;
%put &=year1;
%put &=year2;
%put &=year13;
As always, context and purpose of these macro variables is important, and you don't tell us what the larger problem is or why you need these macro variables. I get the feeling that maybe there are easier ways where you won't need this sequence of macro variables, so please please please tell us what the larger problem is and why you need these macro variables.
Anyway, this is one way to do this.
%let start_yr=2010;
%let end_yr=2022;
data _null_;
do i=1 to &end_yr-&start_yr+1;
call symputx(cats("year",i),&start_yr+i-1);
end;
run;
%put &=year1;
%put &=year2;
%put &=year13;
Do you mean something like:
%macro libname_yr(start_yr=,end_yr=);
%local i ;
%do i = &start_yr %to &end_yr;
/* libname path&i " c:\a\i"; */
%local year%eval(&i-&start_yr+1) ; %*make it local or global ;
%let year%eval(&i-&start_yr+1)=&i ;
%end ;
%put _local_ ;
%mend libname_yr;
%libname_yr(start_yr=2010,end_yr=2022)
Which will create macro variables like:
14 %libname_yr(start_yr=2010,end_yr=2022) LIBNAME_YR END_YR 2022 LIBNAME_YR I 2023 LIBNAME_YR START_YR 2010 LIBNAME_YR YEAR1 2010 LIBNAME_YR YEAR10 2019 LIBNAME_YR YEAR11 2020 LIBNAME_YR YEAR12 2021 LIBNAME_YR YEAR13 2022 LIBNAME_YR YEAR2 2011 LIBNAME_YR YEAR3 2012 LIBNAME_YR YEAR4 2013 LIBNAME_YR YEAR5 2014 LIBNAME_YR YEAR6 2015 LIBNAME_YR YEAR7 2016 LIBNAME_YR YEAR8 2017 LIBNAME_YR YEAR9 2018
?
@sascode wrote:
Hello,
It worked perfectly. However, one more thing:
Is any way we can include your solution inside of this macro:
macro libname_yr;
%do i = &start_yr %to &end_yr;
libname path&i " c:\a\i";
/*To add your piece here*/
.............................................
%mend libname_yr;
This is why you should include the context of the problem in your original problem statement. Then I won't waste my time or your time on a DATA step solution.
@sascode wrote:
Hello,
It worked perfectly. However, one more thing:
Is any way we can include your solution inside of this macro:
macro libname_yr;
%do i = &start_yr %to &end_yr;
libname path&i " c:\a\i";
/*To add your piece here*/
.............................................
%mend libname_yr;
Why would you need a SERIES of macro variables if that is your code?
What do you need besides the current YEAR which you for some reason called I instead of YEAR or YR in your code?
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.