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

I would like a macro variable that is referenced to change -1 and +1 with each iteration. For example in the following loop, each iteration from the start year of 2011 to the end year of 2013 should give the year minus 1 (YM1), the year (Y), and the year plus 1 (YP1).

 

 

%macro Loop_year(startyear=, endyear=);
   %DO I = &startyear %TO &endyear;
      %put YM1 is &I-1;   
	  %put Y   is &I;
	  %put YP1 is &I+1;
   %end;
%mend Loop_year;

%Loop_year(startyear=2011, endyear=2013);

This is what I get:

 

YM1 is 2011-1
Y is 2011
YP1 is 2011+1
YM1 is 2012-1
Y is 2012
YP1 is 2012+1
YM1 is 2013-1
Y is 2013
YP1 is 2013+1

 

But this is what I want:

YM1 is 2010
Y is 2011
YP1 is 2012
YM1 is 2011
Y is 2012
YP1 is 2013
YM1 is 2012
Y is 2013
YP1 is 2014

 

Would this be possible? Thanks in advance

1 ACCEPTED SOLUTION

Accepted Solutions
PeterClemmensen
Tourmaline | Level 20

Like this?

 

%macro Loop_year(startyear=, endyear=);
   %DO I = &startyear %TO &endyear;
      %put YM1 is %eval(&I-1);   
	   %put Y   is &I;
	   %put YP1 is %eval(&I+1);
   %end;
%mend Loop_year;

%Loop_year(startyear=2011, endyear=2013);

 

Result:

 

YM1 is 2010
Y   is 2011
YP1 is 2012
YM1 is 2011
Y   is 2012
YP1 is 2013
YM1 is 2012
Y   is 2013
YP1 is 2014

View solution in original post

2 REPLIES 2
PeterClemmensen
Tourmaline | Level 20

Like this?

 

%macro Loop_year(startyear=, endyear=);
   %DO I = &startyear %TO &endyear;
      %put YM1 is %eval(&I-1);   
	   %put Y   is &I;
	   %put YP1 is %eval(&I+1);
   %end;
%mend Loop_year;

%Loop_year(startyear=2011, endyear=2013);

 

Result:

 

YM1 is 2010
Y   is 2011
YP1 is 2012
YM1 is 2011
Y   is 2012
YP1 is 2013
YM1 is 2012
Y   is 2013
YP1 is 2014
DougHold
Obsidian | Level 7
Exactly, thank you! The %eval() is what I was looking for.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 2 replies
  • 292 views
  • 0 likes
  • 2 in conversation