BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
TurnTheBacon
Fluorite | Level 6

I've created a loop-job which runs the inner job once per month, from the previous month and back until october 2011. The job is parameterized so that -1 indicates one month back, -2 indicates two months back, etc. The control table currently contains the variable "month_back" with values from 1- to -13, so that the inner job is run from oct2012 to oct 2011.

My problem is that the values in the control table are static, so each month I have to manually add another number so that it goes all the way back to oct2011.

What I need is user written code that creates a table where month_back is automatically filled out with all the proper values according to the current month. If I run it today it should get values from -1 to -13, if I run it next month it should get values from -1 to -14, etc.

I'd appreciate any advice on how to accomplish this. Smiley Happy

Basic start:

data work.months_back;

     %let startdate = %sysfunc(intnx(month, %sysfunc(date()), -1, E), date9.); *appears as 31OCT2012 if run in November;
     month_back = -1 *one month back;

     *must get values for all months back until october 2011;

run;

data &_output;

     set months_back;

run;

1 ACCEPTED SOLUTION

Accepted Solutions
RichardinOz
Quartz | Level 8

I think this is what you are looking for.  Day of month in these dates does not matter.

%Let initmonth = '01OCT2011'D ;

Data control ;

    Thismonth = date() ;

    Intervals = 1 + intck ('MONTH', &initmonth, thismonth) ;

    Put intervals= ;

    Do month_back = -1 to -intervals by -1 ;

        Output ;

    End ;

    Keep month_back ;

Run ;

Richard in Oz

View solution in original post

2 REPLIES 2
RichardinOz
Quartz | Level 8

I think this is what you are looking for.  Day of month in these dates does not matter.

%Let initmonth = '01OCT2011'D ;

Data control ;

    Thismonth = date() ;

    Intervals = 1 + intck ('MONTH', &initmonth, thismonth) ;

    Put intervals= ;

    Do month_back = -1 to -intervals by -1 ;

        Output ;

    End ;

    Keep month_back ;

Run ;

Richard in Oz

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 connect to databases in SAS Viya

Need to connect to databases in SAS Viya? SAS’ David Ghan shows you two methods – via SAS/ACCESS LIBNAME and SAS Data Connector SASLIBS – in this video.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 2 replies
  • 732 views
  • 1 like
  • 2 in conversation