BookmarkSubscribeRSS Feed
deleted_user
Not applicable
I have a dataset with a string variable that has this appearance : "10/16/2007 22:37:30". I would like to convert this string to become a datetime value. I read that the datetime format is this 16Oct2007:22:37:30 but I was trying to create a date and a time variable from the string using the substrn function and then join them to create my datetime variable, all that in a data step

Im unable to do it, I though it would be a great idea to ask here about the pertinence of my technique before trying to make it work that way, do you guys have any tips on the best way to do this ?

cheers
Yan
6 REPLIES 6
Tim_SAS
Barite | Level 11
Here's my solution. Use substr to extract the date part of the string and the time part of the string into two variables. Then use the input function with the mmddyy10. informat to convert the date string to a SAS date value, and with the time8. informat to convert the time string to a SAS time value.

Given a SAS date value and a SAS time value, the SAS documentation for the DHMS function explains how to combine them into a SAS datetime value.

Here's a data step to test my algorithm:

[pre]
data _null_;
input str $20.;
dtstr = substr(str, 1, 10);
tmstr = substr(str, 12, 8);

dt = input(dtstr, mmddyy10.);
tm = input(tmstr, time8.);

dttm = dhms(dt, 0, 0, tm);
put dttm datetime19.;
datalines;
10/16/2007 22:37:30
05/01/1953 11:15:27
07/04/1976 23:59:59
12/31/2000 00:00:00
run;[/pre]

Please test this for yourself to make sure it works.
deleted_user
Not applicable
Thank you Tim

meanwhile someone also provided me this way of acheiving the desired result using the scan function instead of substr :

This works :
Data _null_ ;
%let entry_date_time_orig = "10/13/2007 01:14:35" ;
entry_date_time=sum((input(scan(&entry_date_time_orig.,1,' '),mmddyy10.)*60*60*24),input(scan(&entry_date_time_orig.,2,' '),time8.));
put entry_date_time=datetime.;
run;
Peter_C
Rhodochrosite | Level 12
try informat mdyampm.
like

%put %sysfunc( inputn( 10/13/2007 01:14:35, mdyampm), datetime);

PeterC
deleted_user
Not applicable
hi guys..

how about if the input is not a string. but date9. values.
i have a variable contain a date type : yymmdd10.
and i want to make a type datetime19.
ex : position_date is =


==================================================
data _null_;
%put &position_date;
myDate = input(symget('position_date'),yymmdd10.);
format myDate datetime19.;
put myDate=;
run;

the log result :

994 data _null_;
995 %put &position_date;
2008-03-31
996 myDate = input(symget('position_date'),yymmdd10.);
997 format myDate datetime19.;
998 put myDate=;
999 run;

myDate=01JAN1960:04:53:42
NOTE: DATA statement used (Total process time):
real time 0.03 seconds
cpu time 0.01 seconds

==============================================

why myDate became : 01JAN1960:04:53:42 not 31MAR2008:00:00:00 ?
how to make a date like that?

thank you.
regards


capricornday
Peter_C
Rhodochrosite | Level 12
sas datetime formats are suitable for datetime values, not date values.
Convert from a numeric date value to datetime value with function dhms(), like[pre] myDateTime = DHMS( myDate, 0,0,0 ) ; [/pre] Before reading the doc for the datetime() function http://support.sas.com/documentation/cdl/en/lrdict/61724/HTML/default/a000179419.htm , I think you should read up on how SAS manages dates and times in the Concepts section at http://support.sas.com/documentation/cdl/en/lrcon/61722/HTML/default/a002200738.htm.

Good Luck

PeterC
deleted_user
Not applicable
Thanks Peter.C
your links also usefull for me.


regards'
capricornday

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
  • 6 replies
  • 1022 views
  • 0 likes
  • 3 in conversation