10 Replies Latest reply on Jul 10, 2018 5:37 PM by Paul Robins

# How to calculate previous year and previous month

I need to calculate previous system month and previous system year in format mm/yyyy.  Can I use the CALCDATE  %%MONTH -1 and \$CALCDATE %%\$YEAR -1?  Or will I have to use \$CALCDATE %%DATE -365 and -366 (for leap year)?

• ###### 1. Re: How to calculate previous year and previous month

Hi Stacy,

The only valid parameter for CALCDATE is the full date in (CC)YYMMDD depending on the function.

Can you please clarify, are you trying to go back 1 month, or 13 months, or both?

Is there a particular day of the month that you will be performing this calculation?

You can calculate the last day of the previous month by:

%%FIRSTDAY = %%\$YEAR.%%MONTH.01

%%PREVDATE = %%\$CALCDATE %%FIRSTDAY -1

Then use %%SUBSTR to grab out the month and year.

If you're not fussy about the day of the month then you could do a '-370' to land in the tail end of the previous month of the previous year.

You could also use the above method to grab the previous month, then use %%\$YEAR %%MINUS 1 to calculate the previous year.

Hope this helps.

Paul.

1 of 1 people found this helpful
• ###### 2. Re: How to calculate previous year and previous month

We want the previous month/year.  For example, the current mm/yyyy is 02/2017, but we want to pass 01/2017 to a web services job.  If the current month/year is  01/2017, previous month/year should be 12/2016.

• ###### 3. Re: How to calculate previous year and previous month

Yep. That'll work then:

%%FIRSTDAY = %%\$OYEAR.%%OMONTH.01 (20170101)

%%PREVDATE = %%\$CALCDATE %%FIRSTDAY -1 (20161231)

%%PREVYYYYMM = %%SUBSTR %%PREVDATE 1 6 (201612)

or

%%PREVYYYY = %%SUBSTR %%PREVDATE 1 4 (2016)

%%PREVMM = %%SUBSTR %%PREVDATE 5 2 (12)

%%PREVYYYYMM = %%PREVMM/%%PREVYYYY (12/2016 - I haven't checked the syntax on this concatenation)

1 of 1 people found this helpful
• ###### 4. Re: How to calculate previous year and previous month

The proper syntax for last example is %%PREVYYYYMM = %%PREVMM./%%PREVYYYY

This is perfect.  Thank you.

• ###### 5. Re: How to calculate previous year and previous month

How can I get the date to output format to be YYYYMMDD?

• ###### 6. Re: How to calculate previous year and previous month

Hi Kenneth,

Do you want the current day? The first of the month? or the last of the month?

• ###### 7. Re: How to calculate previous year and previous month

First and last of the month.

• ###### 8. Re: How to calculate previous year and previous month

First day of the current month is:

%%FIRSTDAY = %%\$OYEAR.%%OMONTH.01 (20180601)

Last day of the previous month is

%%FIRSTDAY = %%\$OYEAR.%%OMONTH.01 (20180601)

%%PREVDATE = %%\$CALCDATE %%FIRSTDAY -1 (20180531)

First day of the next month, assuming you don't know the number of days in the month is:

Calculate the first day of the current month.

%%FIRSTDAY = %%\$OYEAR.%%OMONTH.01 (20180601)

Add 31 days to move to the next month regardless of what month it is

%%NEXTMON1 = %%\$CALCDATE %%FIRSTDAY +31 (20180702)

Take the YYYYMM from the result then append 01 to it:

%%NEXTMON2 = %%\$SUBSTR %%NEXTMON1 1 6 (201807)

%%NEXTFOM = %%NEXTMON2.01 (20180701)

2 of 2 people found this helpful
• ###### 9. Re: How to calculate previous year and previous month

Is there an easy way to calculate the previous 1st day?

I was able to use PREVFIRSTDATE = %%\$CALCDATE %%FIRSTDAY -30

I am not sure -30 would work for months with 28 or 31 days.

• ###### 10. Re: How to calculate previous year and previous month

First day of the previous month would be:

Set first day of the current month

%%CURRFD = %%\$OYEAR.%%OMONTH.01 (20180701)

Subtract 1 to get the previous month and year (in case you're going back to December previous year)

%%PREVEOM = %%\$CALCDATE %%CURRFD -1 (20180630)

Extract the year and month:

%%PREVYM = %%\$SUBSTR %%PREVEOM 1 6 (201806)

Append the first onto the year and month

%%PREVFD = %%PREVYM.01 (20180601)

Sometimes if you know what day your job is running you can shortcut a step or 2 but for a job that can run on any date this is the most thorough method I can think of.

1 of 1 people found this helpful