Obsidian | Level 7

## Sas current year-month, previous year-month

Hi everyone,

I need to find the syntax for sas (year-month)

Suppose I code :

Curr = put(today, mmyyd.) ;
I will be getting
Curr = 02-2018

Now my question is,

I need from the sas date function
Curr01 = 12-2017
Curr02 = 11-2017

Curr01 being the previous month and year
Curr02 beung the previous of the previous month

Could you please help me with this function, so that even if I am in the middle of the year, I can get a month - 1 and month - 2 with a single code?

Thanks,
Sayan
1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: Sas current year-month, previous year-month

1. You need SAS dates for using INTNX. Don’t separate it to year and Month components.

2. You don’t need SYSFUNC within a data step

3. For previous month and year, that’s 13 months ago. Try using Month and -13 in INTNX.

4. Please format and comment your code. Formatting makes it easier to read, commenting shows your logic or what you expect so we can help.

5. Use the format YYMMN6 to get the dates you want

8 REPLIES 8
Super User

## Re: Sas current year-month, previous year-month

Did you try INTNX()?

@sayanapex06 wrote:
Hi everyone,

I need to find the syntax for sas (year-month)

Suppose I code :

Curr = put(today, mmyyd.) ;
I will be getting
Curr = 02-2018

Now my question is,

I need from the sas date function
Curr01 = 12-2017
Curr02 = 11-2017

Curr01 being the previous month and year
Curr02 beung the previous of the previous month

Could you please help me with this function, so that even if I am in the middle of the year, I can get a month - 1 and month - 2 with a single code?

Thanks,
Sayan

Obsidian | Level 7

Hi Reeza,

No I didnt.

Quartz | Level 8

## Re: Sas current year-month, previous year-month

@sayanapex06 a simple google search will give you tons of syntax, examples, scenarios. Why be so dependent to ask for syntax help and so on. Personally, I think it is not fair. Anyway, up to you!

http://www.sascommunity.org/wiki/INTNX_Function_examples

Obsidian | Level 7

## Re: Sas current year-month, previous year-month

So the current date is 05FEB2018

what will happen if I use this

Curr02 = intx('month', currd, -2)

What will be the value of curr02?
Super User

## Re: Sas current year-month, previous year-month

@sayanapex06 wrote:
So the current date is 05FEB2018

what will happen if I use this

Curr02 = intx('month', currd, -2)

What will be the value of curr02?

Run it and see....

Moderator

## Re: Sas current year-month, previous year-month

Don't forget the optional 4th INTNX() parmeter:
"B" - beggining of period
"M" - middle of period
"E" - end of period
"S" - same place in period
Obsidian | Level 7

## Re: Sas current year-month, previous year-month

I coded the following :

DATA TEST ;

C_MONTH = MONTH(TODAY()) ;
C_YEAR = YEAR(TODAY()) ;

P_MONTH =%SYSFUNC(MONTH(%SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY()), -1))));

P_YEAR =%SYSFUNC(YEAR(%SYSFUNC(INTNX(MONTH, %SYSFUNC(TODAY()), -1))));

PROC PRINT DATA = TEST ;

got the output as :

C_MONTH C_YEAR P_MONTH P_YEAR

2                  2018               1              2018

Now what to do if I want a P_P_MONTH (previous of the previous month) and P_P_YEAR

P_P_MONTH   has to be  12

P_P_YEAR    has to be 2017

Super User

## Re: Sas current year-month, previous year-month

1. You need SAS dates for using INTNX. Don’t separate it to year and Month components.

2. You don’t need SYSFUNC within a data step

3. For previous month and year, that’s 13 months ago. Try using Month and -13 in INTNX.

4. Please format and comment your code. Formatting makes it easier to read, commenting shows your logic or what you expect so we can help.

5. Use the format YYMMN6 to get the dates you want

Discussion stats
• 8 replies
• 9367 views
• 2 likes
• 4 in conversation