Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Transform a string to become a datetime value

Status
Not open for further replies.

ymercier

Technical User
Oct 16, 2007
3
CA
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
 
Hi ymercier,

Try the below:-
Code:
entry_date_time=sum((input(scan(entry_date_time_orig,1,' '),ddmmyy10.)*60*60*24),
                             input(scan(entry_date_time_orig,2,' '),time8.));

Robbie
 
without tabs as the forum dont like tabs....

Code:
entry_date_time=sum((input(scan(entry_date_time_orig,1,' '),ddmmyy10.)*60*60*24),input(scan(entry_date_time_orig,2,' '),time8.));
 
Thank you, it works fine, I didnt know about the scan function

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;

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top