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

?

 

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: 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-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 6 replies
  • 816 views
  • 1 like
  • 4 in conversation