Hi all!
I am struggling to convert a character string into a time variable. I have a dataset that imports the Time variable like:
11/11/2011 11:20
SAS turns this into a Character in the $16. Format. However, I need the Time to display in the DATETIME16. Format.
How do I go about doing this? I have tried an input statement already.
Here is the dataset as I see it in Excel:
Name | Time |
A | 11/15/2012 09:16 |
B | 11/15/2012 09:01 |
C | 11/07/2012 09:51 |
D | 11/28/2012 12:16 |
E | 12/13/2012 01:31 |
F | 12/10/2012 09:36 |
Thank you!
So I struggled and finally got the solution:
Time=input(time2,anydtdtm.)
format Time datetime16.;
Here is a link to SAS date formats
I did look at that before posting the paper. However the formats do not seem to convert the character string to SAS time. It just comes up as a blank.
Are you sure that you looked at the link. At the bottom is an example that basically is the same as the solution you selected here.
Taken from
http://support.sas.com/documentation/cdl/en/lrcon/65287/HTML/default/viewer.htm#p1wj0wt2ebe2a0n1lv4lem9hdc0v.htm
options nodate pageno=1 linesize=80 pagesize=18;
data test;
Time1=86399;
format Time1 datetime.;
Date1=86399;
format Date1 date9.;
Time2=86399;
format Time2 timeampm.;
run;
proc print data=test;
title 'Same Number, Different SAS Values';
footnote1 'Time1 is a SAS DATETIME value';
footnote2 'Date1 is a SAS DATE value';
footnote3 'Time2 is a SAS TIME value';
run;
footnote;
So I struggled and finally got the solution:
Time=input(time2,anydtdtm.)
format Time datetime16.;
Make sure that the setting for the date is the same in each session. As in, the setting for days and month order. Sometimes it works when running code manually but then you run it on the server and it fails.
So you might want to add
options datestyle=mdy;
Another way could be to split the string into date vs time and then combine them.
data want;
datetime_s = "11/15/2011 11:20";
date_s = scan(datetime_s,1," ");
time_s = scan(datetime_s,2," ");
date_n = input(date_s,mmddyy10.);
time_n = input(time_s,hhmmss5.);
datetime_n = dhms(date_n,0,0,time_n);
format date_n yymmdd10. time_n hhmm5. datetime_n B8601DT.;
run;
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.
Find more tutorials on the SAS Users YouTube channel.