SAS DATE issue - calculating "NEAREST MONTH" -


I need to calculate a figure that is equal to the 'nearest' number of months between two dates. However, the standard SAS function (INTCK) is not ready to consider the day of its date parameter (for example, the code below needs to be solved for 0 when I need it 1).

What is the 'fastest' way to solve this problem?

  data_null_; X = "01APR08" d; Y = "28APR08" d; Z = intck ('month', x, y); Put z =; Run;  

EDIT: Martins replies to comments.

I will last up to 0 months - I do not think the range is relevant. I am trying to repeat the function (NEAREST_MONTHS) comes from DCS (Sungard Nabi Application).

Help file includes the following: Category Date

Description

The difference between the two dates is closest Returns the number if the second date is greater than the first date, then 0 is returned

SYNETS

NEAREST_MONTHS (Later_Date, earlier_Date)

return type integer < / P>

Example

NEAREST_MONTHS (date1, date2) return 8 if date is 1 20/3/1997 and dated 2 is 23/7/1996

NEAREST_MONTHS (Date 1, Date 2) Return 26 if date 1 20/3/1997 and date 2 1/2/2005

I wrote it in the form of a function which I think is calculating the same as the DCS application. It uses some features which are new to SAS version 9.2, which includes continuous alignment in the date. It works toward the front or the back of the time (i.e., subsequently afterwards gives a negative integer). I used to spend more than 15 days in the form of cutoffs in the next month, but if you wish, you can tweak it.

  proc fcmp outlib = work.myfuncs.dates; Function nearest _mon (later, first_date); / * If unavailable, return missing if missing / / (previous_date eq.) Or (later_date eq.) Then nearest_month =.; Do more; / * Use the 'cont' logic for consecutive dates * / months = intck ('MONTH', earlier, later, 'cont'); If months and lieutenants; 0 then month = month + 1; Days = later_date - intnx ('month', pre-date, months, 'same'); / * Negative handle (earlier date) * / if months and lieutenants; 0 then do it; If the day & lt; -15th month = months-1; Nearest_month = months; End; Do more; If the day & gt; 15th month + 1; Nearest_month = months; End; End; Return (near_month); end Sub; Run; Option cmplib = work.myfuncs; Data_null_; X = closest months ('20 March 1997, '23 July 196); Put x =; X = closest months ('20 March 1997, '01FEB1995 D); Put x =; Run;  

This is similar to your reference:

  x = 8x = 26  

Comments

Popular posts from this blog

c++ - Linux and clipboard -

What is expire header and how to achive them in ASP.NET and PHP? -

sql server - How can I determine which of my SQL 2005 statistics are unused? -