Hello Everyone,
I am new to sas and I am trying to convert a character string which is in ISO8601 to datetime format in sas.
My code looks like this
proc sql;
Select appnum
,input(submitdate,datetime22.9) as Submitdate
from test
;quit;
submitdate is being pulled from an XML data source so its currently in varchar format
Could any help me with converting the submitdate(which is in iso8601) to datetime? I am new and I only use proc sql; since I have a sql background and still very new to SAS.
Have you tried this link yet?
http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003169817.htm
You may want to apply one of these informat from the above link:
Datetime | YYYY-MM-DDThh:mm:ss.nnnnnn | 2008-09-15T15:53:00 | E8601DTw.d |
Datetime with time zone | YYYY-MM-DDThh:mm:ss.nnnnnn+|-hh:mm | 2008-09-15T15:53:00+05:00 | E8601DZw.d |
Read date from a datetime | YYYY-MM-DD | 2008-09-15 | E8601DNw. |
hey,
I found that page on a google search, but don't quite understand what I would need to do to convert from ISO8601 varchar to datetime22.9 format in SAS. using the above code.
The idea is to apply informat from the page and the informat should convert iso date to a datetime value of sas. Then you apply a datetime format on the numeric datetime value that you derived by applying the informat
Input ( your_variable, <informat - what variable currently looks like>) as new_variable format=<format - what you want variable to look like>
The INFORMAT is likely one from @novinosrin post and the format is DATETIME20.
The decimal portion of 9 is likely not what you actually want, so I would try datetime20 first.
would this be correct I still get an error.
input(date, informat=e8601dzw.d) as Date format=datetime20
input(date, informat=e8601dzw.d) as Date format=datetime20input(date, informat=e8601dzw.d) as Date format=datetime20
e8601dzw.d specify and remove informat=
specifies the width of the input field.
Range | 1–32 |
specifies the power of 10 by which to divide the value. If the data contain decimal points, the d value is ignored. This argument is optional.
Range | 0–31 |
Specify the informat just like you did earlier with a datetime example
Not quite, the INPUT statement doesn't require the INFORMAT keyword.
input(date, e8601dz.) as Date format=datetime20.
hey reeza,
example of the date i am trying to convert '2015-12-14T12:00:29.3387979-05:00'
input(submitdate, E8601Dz.) as date format=datetime20.
i tried the above code and i get blanks
Informats will have a default width if not supplied. In the case of E8601DZ that default is 26. Which for your example is in the middle of the decimal portion of the seconds which means the required part of -05:00 is not seen by the informat. Which will require using the W part of the format.
data junk; x= '2015-12-14T12:00:29.3387979-05:00'; y=input(x,e8601dz33.); format y datetime20.; put y datetime20.; run;
Informats, other thant when used in fixed column input statements, seldom have problems when W is larger than the input string's length.
If you have more decimals in the seconds you may want to use the maximum of W=35 in the informat.
Is your clock actually accurate to the millionth of a second???
Thank you that worked! many thanks!
Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.
Register today!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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: