DATA Step, Macro, Functions and more

Converting custom informat datetime to DATETIMEn. format

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 9
Accepted Solution

Converting custom informat datetime to DATETIMEn. format

I've been searching function or a way to convert datetime DDD MON DD HH:MMSmiley FrustratedS YYYY (e.g Tue Oct 31 01:30:31 2017) I received from datalist to DATETIME19. At the moment I use SQL CASE format, FIND function and temporary field to get day, month, year, hour, minute and second. 

 

I'm using SAS 9.2


Accepted Solutions
Solution
‎10-31-2017 02:22 AM
PROC Star
Posts: 2,318

Re: Converting custom informat datetime to DATETIMEn. format

Your date is weird enough that you must parse the string.

 

Here is one way:

data _null_;
  A='Tue Oct 31 01:30:31 2017';
  B=input(scan(A,3)||scan(A,2)||scan(A,5,' ')||':'||scan(A,4,' '),datetime.);
run;

 

 

View solution in original post


All Replies
Solution
‎10-31-2017 02:22 AM
PROC Star
Posts: 2,318

Re: Converting custom informat datetime to DATETIMEn. format

Your date is weird enough that you must parse the string.

 

Here is one way:

data _null_;
  A='Tue Oct 31 01:30:31 2017';
  B=input(scan(A,3)||scan(A,2)||scan(A,5,' ')||':'||scan(A,4,' '),datetime.);
run;

 

 

Occasional Contributor
Posts: 9

Re: Converting custom informat datetime to DATETIMEn. format

Thank you Smiley Happy
Frequent Contributor
Posts: 103

Re: Converting custom informat datetime to DATETIMEn. format

If the variable is a datetime number (the number of seconds from midnight 1 January 1960), then you can apply any of a range of datetime formats to display the data as you wish.

 

If the variable is not a datetime number, then you have to extract the components and re-assemble them. If you do this correctly, you can convert the result into a SAS datetime number, then apply a format as described above.

 

Some advice on coding can be found here:

 

https://blogs.sas.com/content/sasdummy/2011/11/15/do-you-mind-if-we-dance-with-your-dates/

Norman.
SAS 9.4 (TS1M4) X64_7PRO WIN 6.1.7601 Service Pack 1 Workstation

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 3 replies
  • 118 views
  • 2 likes
  • 3 in conversation