SAS convert string into datetime - datetime

I have a column in a table of string representing datetime like "01-Oct-2012 12:23:43.324" how can I cast this in a SAS datetime ?

The DATETIME informat will read that string
new_var=input(datestring,datetime24.);
format new_var datetime24.;

I don't think there is a single informat which will read that format of datetime... so split it into the date & time components then use the dhms function to create a datetime value.
data have ;
datestring = "01-Oct-2012 12:23:43.324" ;
run ;
data want ;
set have ;
dt = input(scan(datestring,1,' '),??date11.) ;
tm = input(scan(datestring,2,' '),??time14.) ;
dttm = dhms(dt,0,0,tm) ;
format dt date9. tm time14.3 dttm datetime24.3 ;
run ;

Related

How to convert string column to time column type?

I have a column in this format
when I ingest the data, it is in a string format.
When I tried to convert it to timespan, the values disappear
How can I convert string column to timespan?
You could try using parse and then make_timespan(), or timespan arithmetics:
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/make-timespanfunction
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/datetime-timespan-arithmetic
https://learn.microsoft.com/en-us/azure/data-explorer/kusto/query/parseoperator
For example:
T
| parse event_time with minutes:int ":" seconds:int "." ms:int
| project result = minutes * 1m + seconds * 1s + ms * 100 * 1ms
Use the format_timespan Kusto function with fffffff formatter.
Example
format_timespan(time(29.09:00:05.12345), 'ddd.h:mm:ss [fffffff]')
Result: 029.9:00:05 [1234500]

Hive: Convert string datetime with missing seconds in "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"

I'm using the following code to convert a string datetime variable to datetime, but the converted string is missing SSS part.
Code used:
cast(FROM_UNIXTIME(UNIX_TIMESTAMP(oldtime, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss.SSS") as timestamp) as newtime
The outcome:
2019-03-08T18:28:36.901Z is converted to 08MAR2019:18:28:36.000000
Some other oldtimes in string:
2020-03-09T16:05:06:827Z
2020-03-09T16:03:19:354Z
2020-03-11T16:03:57:280Z
2020-03-10T16:02:57:642Z
2020-03-10T16:04:07:455Z
2020-03-10T16:04:09:737Z
2020-03-10T16:03:57:280Z
2020-03-10T16:02:46:816Z
The SSS part '901' is missing in the converted time. Would like help on keeping the SSS part since I need to sort the records by their exact time.
Thank you!
from_unixtime is always until minutes(yyyy-MM-dd HH:mm:ss) to get millisecs we need to do some workarounds.
we will extract the millisecs from the old_time using regexp_extract then concat that to from_unixtime result and finally cast to timestamp.
Example:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\.(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41.335Z")old_time)e
+------------------------+-----------------------+
|old_time |newtime |
+------------------------+-----------------------+
|2020-03-11T21:14:41.335Z|2020-03-11 21:14:41.335|
+------------------------+-----------------------+
UPDATE:
Your sample data have : before milliseconds, Try with below query:
select old_time,
timestamp(concat_ws(".", --concat_ws with . and cast
FROM_UNIXTIME(UNIX_TIMESTAMP(old_time, "yyyy-MM-dd'T'HH:mm:ss:SSS'Z'"),"yyyy-MM-dd HH:mm:ss"), -- from_unixtime and unix_timestamp to convert without millisecs
regexp_extract(string(old_time),".+\\:(.*)(?i)z",1))) as newtime from --regexp_extract to extract last 3 digits before z then concat
(select string("2020-03-11T21:14:41:335Z")old_time)e
Simply replace 'T' with space ' ' remove 'Z' and replace last ':' with dot, like this :
select regexp_replace('2020-03-09T16:05:06:827Z','(.*?)T(.*?):([^:]*?)Z$','$1 $2\\.$3');
Result:
2020-03-09 16:05:06.827
Read also this answer if you need to convert to different format, preserving milliseconds: https://stackoverflow.com/a/59645846/2700344

Unable to do conversion of datatype in U-sql

I am facing issue doing some transformations using U-sql one of the issue is while changing the Date format, Only when I skip the First rows(1) I am able to convert the date format. But I do need the column names so I cannot Skip the first row. Also I need to do some other transformations like data type conversion and simple concatenations.Below is my sample code.Kindly help.
DECLARE #dir string = "/storefolder/Sourcefile/dwfile3.csv";
DECLARE #file_set_path string = "/BCBSvermot/Sample_output.csv";
#data =
EXTRACT
CHECK_DATE string,
FROM #dir
USING Extractors.Csv(skipFirstNRows:1);
#result = SELECT
Convert.ToDateTime(CHECK_DATE).ToString("dd-MM-yyyy") AS CHECK_DATE
FROM #data;
OUTPUT #result
TO #file_set_path
USING Outputters.Csv();
Thanks,
Rav
You can declare a function similar to this:
DECLARE #func Func<string,string> =
(s) =>{
DateTime i;
var x = DateTime.TryParse(s, out i);
return x?((DateTime)i).ToString("dd-MM-yyyy",CultureInfo.CurrentCulture) : s;
};
Then you can use it on your queries
#result =
SELECT #func(CHECK_DATE) AS CHECK_DATE
FROM #data;

Format Date Column in USQL

I currently have the following date format:
"2017-Jan-30 12:45:02:345 EST"
But I need the format to be "MM/dd/yyyy HH:mm:ss"
Does anyone know how to do this? The issue is in the Date string below
#input=
EXTRACT
Date string,
Name string,
Location string
FROM #in
USING Extractor.Csv();
OUTPUT #input
TO #out
USING Outputters.Csv();
You can do something like this:
#input=EXTRACT
[Date] String,
[Name] string,
[Location] string
FROM #in
USING Extractors.Csv();
and then in SELECT statement you format your Date column:
#result = SELECT
DateTime.ParseExact([Date],"yyyy-MMM-dd hh:mm:ss:fff EST",null).ToString("MM/dd/yyyy HH:mm:ss") AS Date
Location,
Name
FROM #input
For more DateTime formats in c# you can check this
Maybe there are null values in your dataset so you can do something like this:
string.IsNullOrEmpty([Date]) ? null : DateTime.ParseExact(Date, "yyyy-MMM-dd HH:mm:ss:fff EST", null).ToString("MM/dd/yyyy HH:mm:ss") AS Date
Instead null you can return maybe default(DateTime), depends what you want.

SAS date formatting hours / minutes / seconds

Because my time data has some messy characters in it ( *, #, char, etc) I'm inputting the data in best.32 format and then using compress to remove the irrelevant char - time_1 = compress(Tim_original,'*#','l');
However, my data takes the form of mm:ss and hh:mm:ss and for some reason when I use time_1=input(time_1,time8.) to convert from the string to a num, it makes my mm into hours...! How do I covert my string to time/minutes and not have the minutes turned into hours with ":00" added at the end?
If your text only has one : then the informat TIME will take that to mean hh:mm and not mm:ss. You could just test your string and divide the result of the INPUT() function call by 60 to convert it.
data test;
input #1 timestr $8. ;
time1=input(timestr,time8.);
time2=input(timestr,time8.);
if countw(timestr,':') < 3 then time2=time2/60 ;
format time1 time2 time8.;
cards;
12:34
0:12:34
;

Resources