0
votes

I have a question about INTNX function:

data have;
date = '11Mar2016'd;
DO tenuremonth = 0 to 12;
    Emth=INTNX('Month', date, tenuremonth); 
    Mthstart=Emth-1;  
    Mthend=INTNX('Month',Emth, 1)-1;
end;

format emth date mth: date9.;

run;

My question is:
I know Mthend is the last day of month. However, I don't know the difference between Mthstart and Mthend.
ex. When tenuremonth = 1
Emth should be equal to 01Apr2016, and Mthend is equal to 31Mar2016.
It seems that Mthstart is the same as Mthend. But, they are different actually.
Can anyone explain it?
Appreciate.

1
Are you asking what the code is doing or are you trying to accomplish something that isn't working the way you expect? You should look up the fourth parameter for the INTNX function which would solve this question and avoid these issues. - Reeza
@Reeza i want to know the result Mthstart & Mthend, I think they are the same but actually different. Why? - user7877169

1 Answers

2
votes

First of all, you're missing an explicit output statement inside the do loop in order to output every iteration of the loop. I assume this is just a typo as you refer to the value from tenuremonth=1 in your question.

The intnx function is working as I would expect, based on your code, maybe you're not fully understanding what it is doing.

As @Reeza mentions, there is a 4th (optional) parameter for intx that enables you to return a date at different times in the month, beginning, mid, end and the same as the input date. If nothing is specified then it defaults to the beginning.

Emth will therefore always returns the 1st of the month, so for tenuremonth=0 this will be 01MAR2016.

Mthstart just takes Emth and subtracts 1 day from it, so will return 29FEB2016.

Mthend takes Emth, advances it by 1 month, then subtracts 1 day, so will retun 31MAR2016.

An easier way to get the month end is to add end to intnx, like this

Mthend=INTNX('Month',date, tenuremonth, 'end');