## Automatically filling out control table with numeric values for loop-job (user written code)

Solved
Frequent Contributor
Posts: 89

# Automatically filling out control table with numeric values for loop-job (user written code)

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.

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;

Accepted Solutions
Solution
‎11-01-2012 07:43 AM
Super Contributor
Posts: 644

## Re: Automatically filling out control table with numeric values for loop-job (user written code)

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

All Replies
Solution
‎11-01-2012 07:43 AM
Super Contributor
Posts: 644

## Re: Automatically filling out control table with numeric values for loop-job (user written code)

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

Frequent Contributor
Posts: 89

## Re: Automatically filling out control table with numeric values for loop-job (user written code)

Brilliant, thanks.

🔒 This topic is solved and locked.

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