Date: Thu, 18 Mar 1999 16:44:56 -0500 Bernard Tremblay "SAS(r) Discussion" Bernard Tremblay Re: Question of Date and Time Value To: c674010@yahoo.com cc: bernard@pollux.capitale.qc.ca

Hi Jie,

Since you dates are in character, you have to bring them into SAS date. Use the input function for that:

t1=input(date1,date9.); /* if you date is in date9 format */ t1=input(date1,yymmdd6.);/* If you date is YYMMDD6. */ and so on.

Then you may use the intck function:

nmonth=intck('month',from,to);

but be aware that intck count the number of interval based on the number of time you cross a boundary. For the month, its the first day of the month. The following would be returned by intcd:

intck('month', '01jan99'd, '01feb99'd) returns: 1 ; intck('month', '31jan99'd, '01feb99'd) returns: 1 ; intck('month', '01jan99'd, '28feb99'd) returns: 1 ;

Not very accurate! Well it depends of your needs. If you want to be more accurate, then how many day make a month ? Depending if you cross a february, an august or a september would you say that 28, 31 or 30 days is a month ?

What we could do to avoid this problem is to make a translation. We move the dates to insure the first date is the first of a month and we move the ending period the same number of days. Then the intck function give a good number except if the last date (after the shift) is the last day of the month. In that case we do not cross the boundary of a month. To correct for this we only have to add 1 day to the last date and we avoid the problem.

See the sample code here:

data _null_; format t1 t2 x1 x2 date9. d 6.; input @1 t1 date9. @11 t2 date9. ; shift=day(t1)-1; /** Calculate number of days to shift **/ x1=t1-shift; /** Shift the dates **/ x2=t2-shift+1; /** Correct the last date by +1 day **/ d=intck('month',x1,x2); /** Obtain number of month crossed **/ put _all_; cards; 01jan1999 01feb1999 31jan1999 01feb1999 01jan1999 28feb1999 15jan1999 15aug1999 15jan1999 12aug1999 ;;;; run;

This gives the following output:

T1=01JAN1999 T2=01FEB1999 X1=01JAN1999 X2=02FEB1999 D=1 SHIFT=0 T1=31JAN1999 T2=01FEB1999 X1=01JAN1999 X2=03JAN1999 D=0 SHIFT=30 T1=01JAN1999 T2=28FEB1999 X1=01JAN1999 X2=01MAR1999 D=2 SHIFT=0 T1=15JAN1999 T2=15AUG1999 X1=01JAN1999 X2=02AUG1999 D=7 SHIFT=14 T1=15JAN1999 T2=12AUG1999 X1=01JAN1999 X2=30JUL1999 D=6 SHIFT=14

NOTE: DATA statement used: real time 2.250 seconds cpu time 0.146 seconds

As you can see the variable D contains the number of completed months since between the two dates T1 and T2. And one have to be very carefull with dates manipulations and SAS functions.

Have fun with SAS, Bernard Tremblay \\\|/// \\ - - // ( @ @ ) +-----oOOo-(_)-oOOo-------+--------------------------------------+ | Bernard Tremblay | | | La Capitale | Tel: (418) 646-2401 | | | Fax: (418) 646-5960 | | | Int: Bernard.Tremblay@capitale.qc.ca | +-------------------------+--------------------------------------+ | Imaginasys enr | Res: (418) 658-1411 | | | Int: bertrem@quebectel.com | +--------------Oooo-------+--------------------------------------+ oooO ( ) ( ) ) / \ ( (_/ \_) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

>>>From owner-sas-l@vm.marist.edu Thu Mar 18 15:50 EST 1999 >>>MIME-Version: 1.0 >>>Date: Thu, 18 Mar 1999 12:49:23 -0800 >>>Sender: "SAS(r) Discussion" <SAS-L@vm.marist.edu> >>>From: Jie Qin <c674010@yahoo.com> >>>Subject: Question of Date and Time Value >>>To: SAS-L@vm.marist.edu >>>Content-Type: text/plain; charset="us-ascii" >>>Content-Length: 523 >>> >>>I have a dataset which contains a time variable. The values of this >>>variable are 01Jan70, 28APR89, 14MAY97, ..., etc. I want to know the >>>month duration between specified in this variable and Aug 30, 1996. >>>Say, if this variable is 01Sep95, then the duration should be 12; if >>>it is 01JUL96, then the duration should be 1. ..... Are there anybody >>>know how I can do that? >>> >>>Thanks in advance. >>> >>> >>> >>> >>>_________________________________________________________ >>>DO YOU YAHOO!? >>>Get your free @yahoo.com address at http://mail.yahoo.com

Back to: Top of message | Previous page | Main SAS-L page