| Date: | Fri, 13 Jan 2012 14:18:00 -0800 |
| Reply-To: | Mary Rosenbloom <mary.rosenbloom.sas@GMAIL.COM> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Mary Rosenbloom <mary.rosenbloom.sas@GMAIL.COM> |
| Subject: | Re: Trouble with the Datepart Function |
|
| In-Reply-To: | <0F548D585A011E4484D77C7B095EC53F0F4753CC@TAC-CMS001.prod.ds.russell.com> |
| Content-Type: | text/plain; charset=ISO-8859-1 |
|---|
Thanks, all, for your time and your very helpful ideas!
On Fri, Jan 13, 2012 at 12:18 PM, Terjeson, Mark <Mterjeson@russell.com>wrote:
> Hi Mary,
>
> Checking formats is one aspect you can entertain.
>
> Another aspect you might entertain actually covers
> two bases instead of just one.
>
> A little background: as you know already, date variables
> and and datetime variables are exactly the same kind of
> variable, a numeric number. The variable isn't different,
> but the cosmetics to apply to the final output can be
> different. Since date is the number of days since 1jan1960
> and datetime is the number of seconds since 1jan1960, then
> the only difference in the numeric variable contents is
> that date is usually a 4 or 5 digit number and a datetime
> is usually a 9 or 10 digit number. And we can manual convert
> the value-range by changing date to datetime (in other words
> days to seconds) by merely multiplying the days by the
> number seconds in a day(24*60*60 or 86400)
> e.g. datetime = date*86400;
> conversely
> date = datepart(datetime);
> or date = int(datetime/86400));
>
> Now after reminding all readers about this, we can make
> use of this information to cover two bases.
>
>
> Base#1 - ...now I know you state you just need to know if the
> variable is date or datetime and you could treat
> the entire column as one or the other... This
> can indeed be done IF the variable has a format
> applied to it, yes, you could check the format.
>
> However, what if there is NO date-format applied to
> your variable? It still could have a format applied
> which is just a number format, or there could be NO
> format at all? Sometimes people's app may not apply
> the cosmetic format until the last minute, and when
> need to do your testing and conversions may be sooner
> when there is NO format.
>
> Base#2 - not very often, but you do see externally imported
> data have varying date or datetime values in the
> same date variable you are reading in. When this
> happens you want be able to detect if it is date
> or datetime not just at the column level but at
> the row level. You want to fold the mix of date
> and datetime values to, just date only, or just
> datetime only.
>
>
> So, for BOTH scenarios of mixed-values or a variable with
> NO format applied, we can use our knowledge of dates to key
> from. With only two caveats to rule out, 1) if we don't care
> about datetimes just for the morning of one day(1jan1960),
> e.g. from 12:00am midnight to 2:12PM on just that one day,
> and 2) we assume your program doesn't need to be working
> 88 years into the future, then this trick will work for all
> SAS versions.
>
> The trick: If we take '31dec2099'd which merely converts
> to a constant integer value of 51134, that does two things
> for us. It is just the seconds in the morning of 1jan1960 up to
> 2:12PM, and it takes days up through the end of year 2099.
>
> So if we want to check our incoming variable which;
> - does not have a format
> or - has mixed date type values
>
> then, just checking our incoming variable against the value
> of 51134 or '31dec2099'd will tell us:
> < 51134 is a date value
> > 51134 is a datetime value
>
>
> So your sample code of
> <code>
> format date date9.;
> if format(datetime) ='DATETIME' then date=datepart(datetime);
> else date=datetime;
> <more code>
>
> could be
>
> <code>
> format date date9.;
> if datetime gt '31dec2099'd then date=datepart(datetime);
> else date=datetime;
> <more code>
>
>
> and that would work whether your incoming variable has a
> format or not. (and as another benefit it works for mixed
> types if you should ever need that too)
>
>
>
>
> Hope this is helpful.
>
>
> Mark Terjeson
> Investment Business Intelligence
> Investment Management & Research
> Russell Investments
> 206-505-2367
>
>
> Russell
> Global Leaders in Multi-Manager Investing
>
>
>
>
>
>
>
>
>
>
>
>
> -----Original Message-----
> From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Mary
> Rosenbloom
> Sent: Friday, January 13, 2012 11:17 AM
> To: SAS-L@LISTSERV.UGA.EDU
> Subject: Trouble with the Datepart Function
>
> Hi all,
>
> I have a macro that contains a data step, and that datastep uses the
> DATEPART function. It looks something like this:
>
> <code>
> format date date9.;
> date=datepart(datetime);
> <nore code>
>
> where DATE is a date-only variable, and DATETIME is usually a datetime
> variable.
>
> However, sometimes DATETIME is acutally just a date variable, and when it
> is, DATE takes the value of zero.
>
> QUESTION: is there a function that I can use to determine whether or not
> DATETIME is truely a datetime variable, and then conditionally process
> only those, such as:
>
> <code>
> format date date9.;
> if format(datetime) ='DATETIME' then date=datepart(datetime);
> else date=datetime;
> <more code>
>
> I'm using 9.1.3 for the next few days, then 9.3.
>
> Thanks in advance for your assistance!
>
> Cheers,
> Mary R.
>
--
Cheers,
Mary F. O. Rosenbloom
http://www.sascommunity.org/wiki/User:Otterm1
|