BookmarkSubscribeRSS Feed
Filipvdr
Pyrite | Level 9
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
5 REPLIES 5
AndyJ
Fluorite | Level 6
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;
Filipvdr
Pyrite | Level 9
ok thanks :) Message was edited by: Filipvdr
Ksharp
Super User
[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
sbb
Lapis Lazuli | Level 10 sbb
Lapis Lazuli | Level 10
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;
Filipvdr
Pyrite | Level 9
thanks for your research, i'm using that one now and it goes good!

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1181 views
  • 0 likes
  • 4 in conversation