DATA Step, Macro, Functions and more

Sas current year-month, previous year-month

Accepted Solution Solved
Reply
Contributor
Posts: 54
Accepted Solution

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

Accepted Solutions
Solution
‎02-06-2018 12:05 PM
Super User
Posts: 24,003

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

Posted in reply to sayanapex06

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 

View solution in original post


All Replies
Super User
Posts: 24,003

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

Posted in reply to sayanapex06

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

 

Contributor
Posts: 54

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

Posted in reply to sayanapex06
Hi Reeza,

No I didnt.

Could you please help me with the intx function over this issue?
Frequent Contributor
Posts: 105

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

Posted in reply to sayanapex06

@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

Contributor
Posts: 54

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

Posted in reply to sayanapex06
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
Posts: 24,003

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

Posted in reply to sayanapex06

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
Posts: 329

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
Contributor
Posts: 54

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 

 

 

Please help me with the above

Solution
‎02-06-2018 12:05 PM
Super User
Posts: 24,003

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

Posted in reply to sayanapex06

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 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

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