DATA Step, Macro, Functions and more

Changing variable based on current month

Occasional Contributor
Posts: 14

Changing variable based on current month

I have an equation that regresses the current front month price of a commodity against each subsequent month after that (i.e. Oct = Nov, Oct = Dec, Oct=Jan15 ect.) My current equations looks like this

'11/15/14'n:model lncornY0M10 = lncornY0M11/noprint;

'12/15/14'n:model lncornY0M10 = lncornY0M12/noprint;

'01/15/15'n:model lncornY0M10 = lncornY1M01/noprint;

'02/15/15'n:model lncornY0M10 = lncornY1M02/noprint;

'03/15/15'n:model lncornY0M10 = lncornY1M03/noprint;

'04/15/15'n:model lncornY0M10 = lncornY1M04/noprint;

'05/15/15'n:model lncornY0M10 = lncornY1M05/noprint;

'06/15/15'n:model lncornY0M10 = lncornY1M06/noprint;

'07/15/15'n:model lncornY0M10 = lncornY1M07/noprint;

'08/15/15'n:model lncornY0M10 = lncornY1M08/noprint;

'09/15/15'n:model lncornY0M10 = lncornY1M09/noprint;

'10/15/15'n:model lncornY0M10 = lncornY1M10/noprint;

'11/15/15'n:model lncornY0M10 = lncornY1M11/noprint;

'12/15/15'n:model lncornY0M10 = lncornY1M12/noprint;

'01/15/15'n:model lncornY0M10 = lncornY2M01/noprint;

'03/15/15'n:model lncornY0M10 = lncornY2M03/noprint;

'04/15/15'n:model lncornY0M10 = lncornY2M04/noprint;

'05/15/15'n:model lncornY0M10 = lncornY2M05/noprint;

'06/15/15'n:model lncornY0M10 = lncornY2M06/noprint;

'07/15/15'n:model lncornY0M10 = lncornY2M07/noprint;

With lncornY0M10 being the natural log of the daily change in corn for the current year, 10th month (October). I was wondering if it would be possible to write a macro that could replace the dependent variable with the current front month when the data switches months. Said differently, Is there an easy way to have the dependent variable switch from lncornY0M10 on october 31st to lncornY0M11 on November 1st? Also, On January 1st the dependent variable will become lncornY0M01 instead of lncornY1M01.


Valued Guide
Posts: 3,208

Re: Changing variable based on current month

Use a dedicated variable simulation date as origin.

When not set the value should be set with the system date.

With this you can simulate time travelling and the macro can be developed and tested.

Using the real system date makes this process impossible. Just good coding practice to have it verifiable allowing alternate dates.

The best thing having it as global standard

---->-- ja karman --<-----
Occasional Contributor
Posts: 14

Re: Changing variable based on current month

I'm able to pull in past datasets to be able to change the current front month. The issue is developing a macro that accomplishes that.

Super User
Posts: 10,500

Re: Changing variable based on current month

WHAT is the rule for Y2 vs Y1 vs Y0? Does your data have any actual dates involved? The values of date variables if containing the correct information could be used to determine start and end dates available and likely create needed code but a description of your base data is needed for that approach.

Occasional Contributor
Posts: 14

Re: Changing variable based on current month

variables are automatically created nightly from futures exchange prices from the previous day. The system we have then takes those prices and arranges them in the Y#M## format. If the current month is october the data will come in ending in Y0M10. If you wanted to look at the price in June of 2016 youd look for the variable ending in Y2M06. I have 90 days worth of price history to run a regression off of with the most current date coming in as 15OCT14. So I guess it would be possible to link the current month to the most recent date being pulled in. An example of my data is below (note that this is from a data set on September 22nd so in this example Y0M09 is the most current month) The date variable ends with 22SEP14.

sas data example.jpg

Ask a Question
Discussion stats
  • 4 replies
  • 3 in conversation