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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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;

View solution in original post

3 REPLIES 3
Patrick
Opal | Level 21

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;
alecav76
Calcite | Level 5
Thanks! 🙂
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

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!

Register Now

Creating Custom Steps in SAS Studio

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 984 views
  • 1 like
  • 3 in conversation