BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sayanapex06
Obsidian | Level 7
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
Reeza
Super User

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

8 REPLIES 8
Reeza
Super User

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

 

sayanapex06
Obsidian | Level 7
Hi Reeza,

No I didnt.

Could you please help me with the intx function over this issue?
MarkWik
Quartz | Level 8

@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

sayanapex06
Obsidian | Level 7
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?
Reeza
Super User

@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....

AndrewHowell
Moderator
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
sayanapex06
Obsidian | Level 7

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

Reeza
Super User

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 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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