BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
UcheOkoro
Lapis Lazuli | Level 10

Hello,

 

Please, I have date and time in character form and I need help converting it to SAS datetime format. 

UcheOkoro_0-1602183240390.png

I am looking for this format

UcheOkoro_2-1602183391476.png

Thank you.

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

You can do that in one step, and it needs small corrections.

Data _data7f;
Set  _data7e;
abx_date=input(scan(abx_datetime,1,' '),mmddyy10.); /* 1 instead of -1 */
abx_time=input(scan(abx_datetime,2,' '),time8.); /* 2 instead if -1 */
abx_datetime_num = dhms(abx_date,0,0,abx_time);
format
  abx_date mmddyy10.
  abx_time time8.
  abx_datetime_new e8601dt19.
;
run;

View solution in original post

9 REPLIES 9
Reeza
Super User
FYI - I updated your subject line to be more descriptive. Please try and use a more relevant subject line in future posts.
UcheOkoro
Lapis Lazuli | Level 10

Thank you for the update

UcheOkoro
Lapis Lazuli | Level 10

Please, I used the following codes that was marked as the solution to a similar question asked in the past and marked as the solution but it does not seem to work for me. I guess I may be doing something wrong. I sincerely appreciate your assistance. 

Data data7f;
Set  data7e;
abx_datetime2=DHMS(Input(Scan(abx_datetime,1,' '),MMDDYY10.),Scan(Scan(abx_datetime,2,' '),1,':'),Scan(Scan(abx_datetime,2,' '),2,':'),Scan(Scan(abx_datetime,2,' '),3,':'));
Format abx_datetime2 DateTime19.;
Run;

Thank you in advance

Kurt_Bremser
Super User

Convert date and time separately. Use SCAN() to extract the first and second "word", use INPUT() to convert (the date with MMDDYY10., the time with TIME8.), and then the DHMS() function to combine everything into the new variable, to which you assign the DATETIME19. format. Use the time as the "seconds" parameter of DHMS().

UcheOkoro
Lapis Lazuli | Level 10

Thank you for your response.Please,  I am not sure I understand clearly how to use the scan statement but I used the following codes for extract data and time but it is not working. I would sincerely appreciate some help writing the code. Thank you in advance.

 

Data _data7f;
Set  _data7e;
abx_date=input(scan(abx_datetime,-1,' '),mmddyy10.);
format abx_date mmddyy10.;
run;

Data _data7f;
Set  _data7f;
abx_time=input(scan(abx_datetime,-1,' '),time8.);
format abx_date time8.;
run;
Kurt_Bremser
Super User

You can do that in one step, and it needs small corrections.

Data _data7f;
Set  _data7e;
abx_date=input(scan(abx_datetime,1,' '),mmddyy10.); /* 1 instead of -1 */
abx_time=input(scan(abx_datetime,2,' '),time8.); /* 2 instead if -1 */
abx_datetime_num = dhms(abx_date,0,0,abx_time);
format
  abx_date mmddyy10.
  abx_time time8.
  abx_datetime_new e8601dt19.
;
run;
UcheOkoro
Lapis Lazuli | Level 10

Thank you so much Kurt for the assistance. I am grateful.

I changed the format for the datetime from e8601dt19. to Datetime19. and it worked just fine.

 

Thank you again.

jimbarbour
Meteorite | Level 14

@UcheOkoro,

 

If the advice that @Kurt_Bremser has given you takes care of everything, well and good.

 

However, if things are still not working, would you please run the following code and post the results here?  I'm interested in the results not the log.

 

Jim

 

Proc Contents Data=_data7e;
RUN;
jimbarbour
Meteorite | Level 14

@UcheOkoro,

 

I just tried some code, see below, and I was able to read the data using an INformat (anydtdtm16.) and then format the value using Format datetime17.  If this is already solved, then do not worry about it, but if not, perhaps it's worth experimenting with the below code.  My results are below the code.

 

Jim

 

DATA	_data7e;
	FORMAT	Date_Value			DATETIME17.;
	INPUT	Date_Value	&	:	ANYDTDTM16.;
DATALINES;
11/8/2016 22:49
11/14/2016 10:00
11/14/2016 10:04
2/5/2017 16:01
2/5/2017 18:34
;
RUN;

 

jimbarbour_0-1602197913836.png

 

 

SAS Innovate 2025: Register Now

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!

How to Concatenate Values

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 9 replies
  • 6947 views
  • 4 likes
  • 4 in conversation