BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
sascode
Quartz | Level 8

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.

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller

View solution in original post

6 REPLIES 6
PaigeMiller
Diamond | Level 26

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;
--
Paige Miller
sascode
Quartz | Level 8
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;

Quentin
Super User

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

?

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
sascode
Quartz | Level 8
It worked perfectly.
Thank you for your help.
PaigeMiller
Diamond | Level 26

@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.

--
Paige Miller
Tom
Super User Tom
Super User

@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: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 6 replies
  • 1190 views
  • 1 like
  • 4 in conversation