BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Singham20
Obsidian | Level 7

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:

NameTime
A11/15/2012 09:16
B11/15/2012 09:01
C11/07/2012 09:51
D11/28/2012 12:16
E12/13/2012 01:31
F

12/10/2012 09:36

 

Thank you!

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Singham20
Obsidian | Level 7

So I struggled and finally got the solution:

 

Time=input(time2,anydtdtm.)

format Time datetime16.;

View solution in original post

5 REPLIES 5
Singham20
Obsidian | Level 7

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.

VDD
Ammonite | Level 13 VDD
Ammonite | Level 13

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;

 

Singham20
Obsidian | Level 7

So I struggled and finally got the solution:

 

Time=input(time2,anydtdtm.)

format Time datetime16.;

heffo
Pyrite | Level 9

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;

 

 

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 5 replies
  • 2348 views
  • 0 likes
  • 3 in conversation