The SAS Output Delivery System and reporting techniques

Need Help With Date Functions

Reply
N/A
Posts: 0

Need Help With Date Functions

Hi gyes

1. If I want to get previous month, I do like this:
month(intnx('month', "&theDate."d, -1))
So, how can I get previous month if instead theDate I have month number (value from 1 to 12)?
I made in this way:
month(intnx('month', mdy(&theMonth, 1, &theYear), -1))
Is it right or there is special function to do this?

2. Also I need to get previous year if it is January.
How can I do this?

Thanks.
SAS Super FREQ
Posts: 8,868

Re: Need Help With Date Functions

Posted in reply to deleted_user
Hi,
If you already have a number from 1-12 as a month value, then simple subtraction would work for your programming problem without using INTNX. Consider the following code:
[pre]
options nodate nonumber;
data testmon;
infile datalines;
input name $ bthmm bthyy;

prevmon = bthmm - 1;
adjyr = bthyy;
if prevmon = 0 then do;
prevmon = 12;
adjyr = bthyy - 1;
end;
newdate = mdy(prevmon,1,adjyr);

** if you wanted to use intnx;
intmon = month(intnx('month', mdy(bthmm, 1, bthyy), -1));
intyy = year(intnx('month',mdy(bthmm,1,bthyy),-1));
return;
datalines;
alice 11 1950
bill 08 1951
carol 01 2000
dave 01 1986
edna 12 2000
;
run;

proc print data=testmon;
title 'started with month as a number';
format newdate mmddyy10.;
run;
** program results are shown at the end of the posting;
[/pre]
In this data, every person has a birth month (bthmm) and a birth year (bthyy), So the statement:
prevmon = bthmm - 1;
will net me the previous month, but, as you noted, someone born in January (1) will now have a previous month value of 0. So I can take care of both month and year in an IF statement:
[pre]
if prevmon = 0 then do;
prevmon = 12;
adjyr = bthyy - 1;
end;
newdate = mdy(prevmon,1,adjyr);
[/pre]

The advantage of INTNX is that it will do the adjustment for you. So if you wanted to stick with an INTNX method, you could do this:
[pre]
intmon = month(intnx('month', mdy(bthmm, 1, bthyy), -1));
intyy = year(intnx('month',mdy(bthmm,1,bthyy),-1));
[/pre]
The first statement assigns a value to the INTMON variable based on using the MONTH function on the nested INTNX function. The second statement assigns a value to the INTYY variable based on using the YEAR function on the returned value from the INTNX function. Both the month AND the year are adjusted by the INTNX function.

But, if you are starting with a month value and a year value and not a SAS date value, then you are forcing the use of an extra function (the MDY inside the INTNX) in order to be able to use the YEAR or MONTH function on what was returned. In this instance, I would probably pick subtraction and an IF statement over INTNX because
1) it's simpler
2) it's easier to understand by a non-SAS programmer (like an auditor)
3) it's easier to maintain.

However, it is less elegant and takes more than one statement to put into effect. So if you needed to accomplish your setting of month in just one statement and your setting of year in just one statement, then INTNX would be the method, even if you do have some overhead in all the nested functions.

Good luck!
cynthia
Results of above program:
[pre]
started with month as a number

Obs name bthmm bthyy prevmon adjyr newdate intmon intyy
1 alice 11 1950 10 1950 10/01/1950 10 1950
2 bill 8 1951 7 1951 07/01/1951 7 1951
3 carol 1 2000 12 1999 12/01/1999 12 1999
4 dave 1 1986 12 1985 12/01/1985 12 1985
5 edna 12 2000 11 2000 11/01/2000 11 2000
[/pre]
Ask a Question
Discussion stats
  • 1 reply
  • 118 views
  • 0 likes
  • 2 in conversation