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

SAS Innovate 2025: Call for Content

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!

Submit your idea!

SAS Enterprise Guide vs. SAS Studio

What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.

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
  • 3 replies
  • 486 views
  • 1 like
  • 3 in conversation