Hello SAS folks,
I am trying to automate a list of dates, having as input the starting year and the ending year.
I have this:
%LET ain=2007;
%LET afi=2022; (every year I want to update this year)
I would like to create a list to insert it in the "retain" command:
retain _name_ _2004 _2005 _2006 _2007 _2004_2007 _2009 _2010 _2011 _2012 _2013 _2014 _2015 _2016 _2017 _2018 _2019 _2020;
So the ideal thing would be to have something like this:
retain _name_ &listofyears;
I am not able to create that macrovariable %listofyears.
Thanks in advance fot your help.
Ale
Something like below should do.
%LET ain=2007;
%LET afi=2022;
%macro yr_list(start,stop);
%global listofyears;
%let listofyears=;
%do i=&start %to &stop;
%let listofyears=&listofyears _&i;
%end;
%mend;
%yr_list(&ain,&afi);
data sample;
retain _name_ &listofyears;
run;
Something like below should do.
%LET ain=2007;
%LET afi=2022;
%macro yr_list(start,stop);
%global listofyears;
%let listofyears=;
%do i=&start %to &stop;
%let listofyears=&listofyears _&i;
%end;
%mend;
%yr_list(&ain,&afi);
data sample;
retain _name_ &listofyears;
run;
There are better ways to organize data than putting year values into variable names. A long data set will be much easier to work with in the long run, as almost all SAS PROCs are designed to work with long data sets.
Please tell us the goal of doing this, what analysis or report will you be doing with this data?
This seems like a great example of the XY problem, never a good place to be.
Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!
Check out this tutorial series to learn how to build your own steps in SAS Studio.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.