%macro yearOf(num, year=&gYear, start=&gStart, finish=&gFinish);
%local max left right;
%if &finish <= &start %then %return;
%if &year < &start or &finish < &year %then %return;
%let max = %eval(&finish - &start);
%if &num < 1 or max < &num %then %return;
%if &year = &start %then %do;
%eval(&start + &num)
%end; %else %if &year = &finish %then %do;
%eval(&finish - &num)
%end; %else %do;
%let left = %eval(&year - &start - 1);
%let right = %eval(&max - &left);
%if &num = 1 %then %do;
%eval(&start + &left)
%end; %else %if &num <= &right %then %do;
%eval(&start + &left + &num)
%end; %else %if &right < &num %then %do;
%eval(&finish - &num)
%end;
%end;
%mend yearOf;
%*-- check. how to use --*;
%let gStart = 2005;
%let gFinish = 2010;
%let gYear = 2007;
%put year: year1 year2 year3 year4 year5;
%put &gyear: %yearOf(1) %yearOf(2) %yearOf(3) %yearOf(4) %yearOf(5);
%*-- on log
year: year1 year2 year3 year4 year5
2007: 2006 2008 2009 2010 2005
--*;
%*-- another check --*;
%macro check(year, start, finish);
%local n max;
%let max = %eval(&finish - &start);
%*;&year:
%do n = 1 %to &max;
%*; %yearOf(&n, year=&year, start=&start, finish=&finish)
%end;
%mend check;
%macro checkAll(start, finish);
%local year;
%put start=&start finish=&finish;
%do year = &start %to &finish;
%put %check(&year, &start, &finish);
%end;
%mend checkAll;
%checkAll(start=2005, finish=2010)
%*-- on log
start=2005 finish=2010
2005: 2006 2007 2008 2009 2010
2006: 2005 2007 2008 2009 2010
2007: 2006 2008 2009 2010 2005
2008: 2007 2009 2010 2006 2005
2009: 2008 2010 2007 2006 2005
2010: 2009 2008 2007 2006 2005
--*;
%checkAll(start=2005, finish=2011)
%*-- on log
start=2005 finish=2011
2005: 2006 2007 2008 2009 2010 2011
2006: 2005 2007 2008 2009 2010 2011
2007: 2006 2008 2009 2010 2011 2005
2008: 2007 2009 2010 2011 2006 2005
2009: 2008 2010 2011 2007 2006 2005
2010: 2009 2011 2008 2007 2006 2005
2011: 2010 2009 2008 2007 2006 2005
--*;
%checkAll(start=2005, finish=2012)
%*-- on log
start=2005 finish=2012
2005: 2006 2007 2008 2009 2010 2011 2012
2006: 2005 2007 2008 2009 2010 2011 2012
2007: 2006 2008 2009 2010 2011 2012 2005
2008: 2007 2009 2010 2011 2012 2006 2005
2009: 2008 2010 2011 2012 2007 2006 2005
2010: 2009 2011 2012 2008 2007 2006 2005
2011: 2010 2012 2009 2008 2007 2006 2005
2012: 2011 2010 2009 2008 2007 2006 2005
--*;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.