how to combine date and time to make it datetime

  • Follow


Hi,

I would like to combine mydate and mytime to create mydatetime.
I would appreciate it if anyone would offer some help.



***  In excel:
mydate                         mytime
1/2/2009  12:00:00 AM          7:45:00 AM


*** After reading the data into SAS
mydate: Number 8  date9.
mytime: Number 8  time8.


mydate                    mytime
02JAN2009                 7:45:00



*** Results I would like:
I would like to combine mydate and mytime to create mydatetime.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      02JAN2009:07:45:00



*** codes I tried and the (wrong) result I got.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      01JAN60:07:45:00




Options validVarName=any ;

PROC IMPORT OUT= WORK.one
            DATAFILE= "c:\Service"
            DBMS=EXCEL REPLACE;
     SHEET="Service$";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;


data two;
set one;
myDateTime = datepart(mydate) + mytime;
run;
0
Reply hummingbird10111 (35) 12/18/2009 6:56:47 AM

Hi,

You can use dhms with hour, minute and second functions.

------Original Message------
From: Annie Lee
Sender: SAS(r) Discussion
To: SAS-L@LISTSERV.UGA.EDU
ReplyTo: Annie Lee
Subject: how to combine date and time to make it datetime
Sent: Dec 18, 2009 2:56 PM

Hi,

I would like to combine mydate and mytime to create mydatetime.
I would appreciate it if anyone would offer some help.



***  In excel:
mydate                         mytime
1/2/2009  12:00:00 AM          7:45:00 AM


*** After reading the data into SAS
mydate: Number 8  date9.
mytime: Number 8  time8.


mydate                    mytime
02JAN2009                 7:45:00



*** Results I would like:
I would like to combine mydate and mytime to create mydatetime.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      02JAN2009:07:45:00



*** codes I tried and the (wrong) result I got.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      01JAN60:07:45:00




Options validVarName=any ;

PROC IMPORT OUT= WORK.one
            DATAFILE= "c:\Service"
            DBMS=EXCEL REPLACE;
     SHEET="Service$";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;


data two;
set one;
myDateTime = datepart(mydate) + mytime;
run;


Sent from my BlackBerry Wireless Handheld

--
Regards,
Murphy Choy

Certified Advanced Programmer for SAS V9
Certified Basic Programmer for SAS V9
0
Reply goladin (478) 12/18/2009 7:08:13 AM


HI,

USE DHMS function....


data datetime;
input mydate mytime;
informat mydate date9. mytime time8.;
format mydate date9. mytime time8. dt1 datetime32.;
dt1=DHMS(mydate,0,0,mytime);
cards;
02JAN2009 7:45:00
03JAN2009 8:45:00
04JAN2009 9:45:00
;
run;
proc print;
run;





On Fri, Dec 18, 2009 at 12:26 PM, Annie Lee <hummingbird10111@hotmail.com>wrote:

> Hi,
>
> I would like to combine mydate and mytime to create mydatetime.
> I would appreciate it if anyone would offer some help.
>
>
>
> ***  In excel:
> mydate                         mytime
> 1/2/2009  12:00:00 AM          7:45:00 AM
>
>
> *** After reading the data into SAS
> mydate: Number 8  date9.
> mytime: Number 8  time8.
>
>
> mydate                    mytime
> 02JAN2009                 7:45:00
>
>
>
> *** Results I would like:
> I would like to combine mydate and mytime to create mydatetime.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      02JAN2009:07:45:00
>
>
>
> *** codes I tried and the (wrong) result I got.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      01JAN60:07:45:00
>
>
>
>
> Options validVarName=any ;
>
> PROC IMPORT OUT= WORK.one
>            DATAFILE= "c:\Service"
>            DBMS=EXCEL REPLACE;
>     SHEET="Service$";
>     GETNAMES=YES;
>     MIXED=NO;
>     SCANTEXT=YES;
>     USEDATE=YES;
>     SCANTIME=YES;
> RUN;
>
>
> data two;
> set one;
> myDateTime = datepart(mydate) + mytime;
> run;
>



--
Thanks,
Mahesh P

***   FAILING TO PLAN IS PLANNING TO FAIL  ***
0
Reply peesari.mahesh (114) 12/18/2009 7:39:05 AM

Suppose the time was a duration. Would we create a datetime value as
the OP wants?  If we would then the way the DHMS function is called
will yield different results.   For example...

3077  data _null_;
3078     date = today();
3079     time = '25:30:30't;
3080     put time=time8.;
3081     h = hour(time);
3082     m = minute(time);
3083     s = second(time);
3084     put (h m s)(=);
3085     dt1 = dhms(date,h,m,s);
3086     dt2 = dhms(date,0,0,time);
3087     put (dt1 dt2) (=datetime.);
3088     run;

time=25:30:30
h=1 m=30 s=30
dt1=18DEC09:01:30:30 dt2=19DEC09:01:30:30


Does creating a datetime from a date and a duration make sense?



On 12/18/09, Murphy Choy <goladin@gmail.com> wrote:
> Hi,
>
> You can use dhms with hour, minute and second functions.
>
> ------Original Message------
> From: Annie Lee
> Sender: SAS(r) Discussion
> To: SAS-L@LISTSERV.UGA.EDU
> ReplyTo: Annie Lee
> Subject: how to combine date and time to make it datetime
> Sent: Dec 18, 2009 2:56 PM
>
> Hi,
>
> I would like to combine mydate and mytime to create mydatetime.
> I would appreciate it if anyone would offer some help.
>
>
>
> ***  In excel:
> mydate                         mytime
> 1/2/2009  12:00:00 AM          7:45:00 AM
>
>
> *** After reading the data into SAS
> mydate: Number 8  date9.
> mytime: Number 8  time8.
>
>
> mydate                    mytime
> 02JAN2009                 7:45:00
>
>
>
> *** Results I would like:
> I would like to combine mydate and mytime to create mydatetime.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      02JAN2009:07:45:00
>
>
>
> *** codes I tried and the (wrong) result I got.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      01JAN60:07:45:00
>
>
>
>
> Options validVarName=any ;
>
> PROC IMPORT OUT= WORK.one
>            DATAFILE= "c:\Service"
>            DBMS=EXCEL REPLACE;
>     SHEET="Service$";
>     GETNAMES=YES;
>     MIXED=NO;
>     SCANTEXT=YES;
>     USEDATE=YES;
>     SCANTIME=YES;
> RUN;
>
>
> data two;
> set one;
> myDateTime = datepart(mydate) + mytime;
> run;
>
>
> Sent from my BlackBerry Wireless Handheld
>
> --
> Regards,
> Murphy Choy
>
> Certified Advanced Programmer for SAS V9
> Certified Basic Programmer for SAS V9
>
0
Reply iebupdte 12/18/2009 12:31:33 PM

Annie

As others have told you, the DHMS function will create a DateTime value but first, you need Hour, Minute, and Second:


 Data Annie;

 Time = '7:45:00't;
 Date = '02JAN2009'd;

format date date. time time.;
Drop Hour Min Sec;

** first extract hour, minute, and second;
Hour = Hour  ( time );
Min  = Minute( time );
Sec  = Second( time );


* Now, combine the pieces. Of course, I am using DateTime as both a variable name and a format;
DateTime = DHMS( Date , Hour , Min , Sec );
format DateTime Datetime20.;
 run;

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-L@LISTSERV.UGA.EDU] On Behalf Of Annie Lee
Sent: Friday, December 18, 2009 1:57 AM
To: SAS-L@LISTSERV.UGA.EDU
Subject: how to combine date and time to make it datetime

Hi,

I would like to combine mydate and mytime to create mydatetime.
I would appreciate it if anyone would offer some help.



***  In excel:
mydate                         mytime
1/2/2009  12:00:00 AM          7:45:00 AM


*** After reading the data into SAS
mydate: Number 8  date9.
mytime: Number 8  time8.


mydate                    mytime
02JAN2009                 7:45:00



*** Results I would like:
I would like to combine mydate and mytime to create mydatetime.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      02JAN2009:07:45:00



*** codes I tried and the (wrong) result I got.

mydate                    mytime       mydatetime
02JAN2009                 7:45:00      01JAN60:07:45:00




Options validVarName=any ;

PROC IMPORT OUT= WORK.one
            DATAFILE= "c:\Service"
            DBMS=EXCEL REPLACE;
     SHEET="Service$";
     GETNAMES=YES;
     MIXED=NO;
     SCANTEXT=YES;
     USEDATE=YES;
     SCANTIME=YES;
RUN;


data two;
set one;
myDateTime = datepart(mydate) + mytime;
run;
CONFIDENTIALITY NOTICE:  This electronic message contains
information which may be legally confidential and or privileged and
does not in any case represent a firm ENERGY COMMODITY bid or offer
relating thereto which binds the sender without an additional
express written confirmation to that effect.  The information is
intended solely for the individual or entity named above and access
by anyone else is unauthorized.  If you are not the intended
recipient, any disclosure, copying, distribution, or use of the
contents of this information is prohibited and may be unlawful.  If
you have received this electronic transmission in error, please
reply immediately to the sender that you have received the message
in error, and delete it.  Thank you.
0
Reply nathaniel.wooding (1453) 12/18/2009 12:38:29 PM

Hi,

Wouldn't we use a kind of modulus function to extract the proper time?

On Dec 18, 2009 8:31pm, "Data _null_;" <iebupdte@gmail.com> wrote:
> Suppose the time was a duration. Would we create a datetime value as

> the OP wants? If we would then the way the DHMS function is called

> will yield different results. For example...



> 3077 data _null_;

> 3078 date = today();

> 3079 time = '25:30:30't;

> 3080 put time=time8.;

> 3081 h = hour(time);

> 3082 m = minute(time);

> 3083 s = second(time);

> 3084 put (hms)(=);

> 3085 dt1 = dhms(date,h,m,s);

> 3086 dt2 = dhms(date,0,0,time);

> 3087 put (dt1 dt2) (=datetime.);

> 3088 run;



> time=25:30:30

> h=1 m=30 s=30

> dt1=18DEC09:01:30:30 dt2=19DEC09:01:30:30





> Does creating a datetime from a date and a duration make sense?







> On 12/18/09, Murphy Choy goladin@gmail.com> wrote:

> > Hi,

> >

> > You can use dhms with hour, minute and second functions.

> >

> > ------Original Message------

> > From: Annie Lee

> > Sender: SAS(r) Discussion

> > To: SAS-L@LISTSERV.UGA.EDU

> > ReplyTo: Annie Lee

> > Subject: how to combine date and time to make it datetime

> > Sent: Dec 18, 2009 2:56 PM

> >

> > Hi,

> >

> > I would like to combine mydate and mytime to create mydatetime.

> > I would appreciate it if anyone would offer some help.

> >

> >

> >

> > *** In excel:

> > mydate mytime

> > 1/2/2009 12:00:00 AM 7:45:00 AM

> >

> >

> > *** After reading the data into SAS

> > mydate: Number 8 date9.

> > mytime: Number 8 time8.

> >

> >

> > mydate mytime

> > 02JAN2009 7:45:00

> >

> >

> >

> > *** Results I would like:

> > I would like to combine mydate and mytime to create mydatetime.

> >

> > mydate mytime mydatetime

> > 02JAN2009 7:45:00 02JAN2009:07:45:00

> >

> >

> >

> > *** codes I tried and the (wrong) result I got.

> >

> > mydate mytime mydatetime

> > 02JAN2009 7:45:00 01JAN60:07:45:00

> >

> >

> >

> >

> > Options validVarName=any ;

> >

> > PROC IMPORT OUT= WORK.one

> > DATAFILE= "c:\Service"

> > DBMS=EXCEL REPLACE;

> > SHEET="Service$";

> > GETNAMES=YES;

> > MIXED=NO;

> > SCANTEXT=YES;

> > USEDATE=YES;

> > SCANTIME=YES;

> > RUN;

> >

> >

> > data two;

> > set one;

> > myDateTime = datepart(mydate) + mytime;

> > run;

> >

> >

> > Sent from my BlackBerry Wireless Handheld

> >

> > --

> > Regards,

> > Murphy Choy

> >

> > Certified Advanced Programmer for SAS V9

> > Certified Basic Programmer for SAS V9

> >
0
Reply goladin (478) 12/18/2009 12:52:34 PM

I don't follow.  What is "proper time"?  Do you have example?

On 12/18/09, goladin@gmail.com <goladin@gmail.com> wrote:
> Hi,
>
> Wouldn't we use a kind of modulus function to extract the proper time?
>
>
> On Dec 18, 2009 8:31pm, "Data _null_;" <iebupdte@gmail.com> wrote:
> > Suppose the time was a duration. Would we create a datetime value as
> >
> > the OP wants?  If we would then the way the DHMS function is called
> >
> > will yield different results.   For example...
> >
> >
> >
> > 3077  data _null_;
> >
> > 3078     date = today();
> >
> > 3079     time = '25:30:30't;
> >
> > 3080     put time=time8.;
> >
> > 3081     h = hour(time);
> >
> > 3082     m = minute(time);
> >
> > 3083     s = second(time);
> >
> > 3084     put (h m s)(=);
> >
> > 3085     dt1 = dhms(date,h,m,s);
> >
> > 3086     dt2 = dhms(date,0,0,time);
> >
> > 3087     put (dt1 dt2) (=datetime.);
> >
> > 3088     run;
> >
> >
> >
> > time=25:30:30
> >
> > h=1 m=30 s=30
> >
> > dt1=18DEC09:01:30:30 dt2=19DEC09:01:30:30
> >
> >
> >
> >
> >
> > Does creating a datetime from a date and a duration make sense?
> >
> >
> >
> >
> >
> >
> >
> > On 12/18/09, Murphy Choy goladin@gmail.com> wrote:
> >
> > > Hi,
> >
> > >
> >
> > > You can use dhms with hour, minute and second functions.
> >
> > >
> >
> > > ------Original Message------
> >
> > > From: Annie Lee
> >
> > > Sender: SAS(r) Discussion
> >
> > > To: SAS-L@LISTSERV.UGA.EDU
> >
> > > ReplyTo: Annie Lee
> >
> > > Subject: how to combine date and time to make it datetime
> >
> > > Sent: Dec 18, 2009 2:56 PM
> >
> > >
> >
> > > Hi,
> >
> > >
> >
> > > I would like to combine mydate and mytime to create mydatetime.
> >
> > > I would appreciate it if anyone would offer some help.
> >
> > >
> >
> > >
> >
> > >
> >
> > > ***  In excel:
> >
> > > mydate                         mytime
> >
> > > 1/2/2009  12:00:00 AM          7:45:00 AM
> >
> > >
> >
> > >
> >
> > > *** After reading the data into SAS
> >
> > > mydate: Number 8  date9.
> >
> > > mytime: Number 8  time8.
> >
> > >
> >
> > >
> >
> > > mydate                    mytime
> >
> > > 02JAN2009                 7:45:00
> >
> > >
> >
> > >
> >
> > >
> >
> > > *** Results I would like:
> >
> > > I would like to combine mydate and mytime to create mydatetime.
> >
> > >
> >
> > > mydate                    mytime       mydatetime
> >
> > > 02JAN2009                 7:45:00      02JAN2009:07:45:00
> >
> > >
> >
> > >
> >
> > >
> >
> > > *** codes I tried and the (wrong) result I got.
> >
> > >
> >
> > > mydate                    mytime       mydatetime
> >
> > > 02JAN2009                 7:45:00      01JAN60:07:45:00
> >
> > >
> >
> > >
> >
> > >
> >
> > >
> >
> > > Options validVarName=any ;
> >
> > >
> >
> > > PROC IMPORT OUT= WORK.one
> >
> > >            DATAFILE= "c:\Service"
> >
> > >            DBMS=EXCEL REPLACE;
> >
> > >     SHEET="Service$";
> >
> > >     GETNAMES=YES;
> >
> > >     MIXED=NO;
> >
> > >     SCANTEXT=YES;
> >
> > >     USEDATE=YES;
> >
> > >     SCANTIME=YES;
> >
> > > RUN;
> >
> > >
> >
> > >
> >
> > > data two;
> >
> > > set one;
> >
> > > myDateTime = datepart(mydate) + mytime;
> >
> > > run;
> >
> > >
> >
> > >
> >
> > > Sent from my BlackBerry Wireless Handheld
> >
> > >
> >
> > > --
> >
> > > Regards,
> >
> > > Murphy Choy
> >
> > >
> >
> > > Certified Advanced Programmer for SAS V9
> >
> > > Certified Basic Programmer for SAS V9
> >
> > >
> >
0
Reply iebupdte 12/18/2009 1:14:08 PM

SAS date values are integers - the number of days since 01-January-1960.
All days are 86,400 seconds long.  SAS time values are also integers - the
number of seconds since midnight.  SAS date-time values are the number of
seconds since midnight, 01-January-1960.

Depending on the release of SAS you're using, you may be able to use the
DHMS function, but if that's not available to you, the following formula
will convert date and time values to date-time values:

SAS-date-time-value = (SAS-date-value * 86,400) + SAS-time-value

"Annie Lee" <hummingbird10111@HOTMAIL.COM> wrote in message
news:200912180656.nBHJ1Ti0009874@malibu.cc.uga.edu...
> Hi,
>
> I would like to combine mydate and mytime to create mydatetime.
> I would appreciate it if anyone would offer some help.
>
>
>
> ***  In excel:
> mydate                         mytime
> 1/2/2009  12:00:00 AM          7:45:00 AM
>
>
> *** After reading the data into SAS
> mydate: Number 8  date9.
> mytime: Number 8  time8.
>
>
> mydate                    mytime
> 02JAN2009                 7:45:00
>
>
>
> *** Results I would like:
> I would like to combine mydate and mytime to create mydatetime.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      02JAN2009:07:45:00
>
>
>
> *** codes I tried and the (wrong) result I got.
>
> mydate                    mytime       mydatetime
> 02JAN2009                 7:45:00      01JAN60:07:45:00
>
>
>
>
> Options validVarName=any ;
>
> PROC IMPORT OUT= WORK.one
>             DATAFILE= "c:\Service"
>             DBMS=EXCEL REPLACE;
>      SHEET="Service$";
>      GETNAMES=YES;
>      MIXED=NO;
>      SCANTEXT=YES;
>      USEDATE=YES;
>      SCANTIME=YES;
> RUN;
>
>
> data two;
> set one;
> myDateTime = datepart(mydate) + mytime;
> run;


0
Reply Lou 12/18/2009 3:32:57 PM

the DHMS function is available for sure! I'm not sure about V5, but in V6
and higher it is!

use it like:

  dtm = dhms(mydate,0,0,mytime);

you can also extract the hour, minute and second with the functions of
exactly that names, but that's not necessary.

Don't forget to format

  format dtm datetime20.;

to make it more readayble.

Gerhard





On Fri, 18 Dec 2009 10:32:57 -0500, Lou <lpogoda@HOTMAIL.COM> wrote:

>SAS date values are integers - the number of days since 01-January-1960.
>All days are 86,400 seconds long.  SAS time values are also integers - the
>number of seconds since midnight.  SAS date-time values are the number of
>seconds since midnight, 01-January-1960.
>
>Depending on the release of SAS you're using, you may be able to use the
>DHMS function, but if that's not available to you, the following formula
>will convert date and time values to date-time values:
>
>SAS-date-time-value = (SAS-date-value * 86,400) + SAS-time-value
>
>"Annie Lee" <hummingbird10111@HOTMAIL.COM> wrote in message
>news:200912180656.nBHJ1Ti0009874@malibu.cc.uga.edu...
>> Hi,
>>
>> I would like to combine mydate and mytime to create mydatetime.
>> I would appreciate it if anyone would offer some help.
>>
>>
>>
>> ***  In excel:
>> mydate                         mytime
>> 1/2/2009  12:00:00 AM          7:45:00 AM
>>
>>
>> *** After reading the data into SAS
>> mydate: Number 8  date9.
>> mytime: Number 8  time8.
>>
>>
>> mydate                    mytime
>> 02JAN2009                 7:45:00
>>
>>
>>
>> *** Results I would like:
>> I would like to combine mydate and mytime to create mydatetime.
>>
>> mydate                    mytime       mydatetime
>> 02JAN2009                 7:45:00      02JAN2009:07:45:00
>>
>>
>>
>> *** codes I tried and the (wrong) result I got.
>>
>> mydate                    mytime       mydatetime
>> 02JAN2009                 7:45:00      01JAN60:07:45:00
>>
>>
>>
>>
>> Options validVarName=any ;
>>
>> PROC IMPORT OUT= WORK.one
>>             DATAFILE= "c:\Service"
>>             DBMS=EXCEL REPLACE;
>>      SHEET="Service$";
>>      GETNAMES=YES;
>>      MIXED=NO;
>>      SCANTEXT=YES;
>>      USEDATE=YES;
>>      SCANTIME=YES;
>> RUN;
>>
>>
>> data two;
>> set one;
>> myDateTime = datepart(mydate) + mytime;
>> run;
0
Reply gerhard.hellriegel (2531) 12/18/2009 4:52:17 PM

8 Replies
362 Views

(page loaded in 0.201 seconds)

Similiar Articles:













7/22/2012 4:14:37 AM


Reply: