The SAS Output Delivery System and reporting techniques

Date 20110503 09:58:47.65

Reply
Regular Contributor
Posts: 237

Date 20110503 09:58:47.65

Hello,

I have a date like this: 20110503 09:58:47.65 (String format $48).
How do I make this a Sas DateTime? ( the .65 can be dropped at the end )

Thanks! Message was edited by: Filipvdr
Occasional Contributor
Posts: 11

Re: Date 20110503 09:58:47.65

data sample_data;
infile datalines;
input original_variable $48.;
datalines;
20110503 09:58:47.65
;
run;

data modified_data;
set sample_data;

*** separate date from original string ***;
date=input(scan(original_variable,1,' '), yymmdd8.);

*** separate time from original string ***;
time=scan(original_variable,-1,' ');

*** separate out hours minutes and seconds ***;
hour=input(scan(time,1,':'),2.);
minute=input(scan(time,2,':'),2.);
second=input(scan(time,3,':'),2.);

*** create datetime variable ***;
datetime=dhms(date,hour,minute,second);

*** check work ***;
datetime2=put(datetime,datetime.);

run;
Regular Contributor
Posts: 237

Re: Date 20110503 09:58:47.65

ok thanks Smiley Happy Message was edited by: Filipvdr
Super User
Posts: 10,041

Re: Date 20110503 09:58:47.65

[pre]
options datestyle=ymd;
data sample_data;
infile datalines;
input original_variable anydtdtm22.;
format original_variable datetime20.;
datalines;
20110503 09:58:47.65
;
run;
[/pre]


Ksharp Message was edited by: Ksharp
Super Contributor
Super Contributor
Posts: 3,174

Re: Date 20110503 09:58:47.65

I found that using ANYDTDTM INFORMAT is inconsistent and sometimes unpredictable - splitting the two date-time components for INPUT and then combining them with DHMS (without manual parsing) works well.

Scott Barry
SBBWorks, Inc.


1 data _null_;
2 infile datalines;
3 length tm_char $11;
4 input dt_char $ tm_char $;
5 dt = input(dt_char,yymmdd8.);
6 tm = input(tm_char,time11.);
7 dt_tm = dhms(dt,0,0,tm);
8 format dt_tm datetime22.2;
9 list;
10 putlog _all_;
11 datalines;

tm_char=09:58:47.65 dt_char=20110503 dt=18750 tm=35927.65 dt_tm=03MAY2011:09:58:47.65 _ERROR_=0
_N_=1
RULE: ----+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+
12 20110503 09:58:47.65
NOTE: DATA statement used (Total process time):
real time 0.02 seconds
cpu time 0.00 seconds


13 run;
Regular Contributor
Posts: 237

Re: Date 20110503 09:58:47.65

thanks for your research, i'm using that one now and it goes good!
Ask a Question
Discussion stats
  • 5 replies
  • 390 views
  • 0 likes
  • 4 in conversation