Use the ANYDTDTM informat to read it. Then attach whatever format you want to display the values. Like DATETIME20.;
data test;
length dt 8;
informat dt anydtdtm.;
format dt datetime20.;
infile cards dsd truncover;
input dt;
put dt=;
cards;
12/30/2019 12:30:12 AM
12/30/2019 12:30:12 PM
;
Results:
dt=30DEC2019:00:30:12 dt=30DEC2019:12:30:12
Note care must by used when using ANYDTxxx informats to read dates in MDY or DMY order as how it interprets ambiguous values like 06/05/2019 will change based on your LOCALE settings.
@sanjaymane7 wrote:
Hi, I want to create a new dataset wherein date format is in mmddyyyy hr mm ss am/pm. Pls share me informat and format code for date. I already tried datetime18. And datetime19. But not worked.
Thanks in advance
You can't display dates that way, you can however display date/time values that way. Is your variable actually a date/time value?
The format is DATEAMPMw.d
Also, no need to create a new data set, just change the format in the existing data set.
@sanjaymane7 wrote:
Hi, I want to create a new dataset wherein date format is in mmddyyyy hr mm ss am/pm. Pls share me informat and format code for date. I already tried datetime18. And datetime19. But not worked.
Thanks in advance
Why does it matter what display format is attached to the DATETIME variable?
Is your problem with creating the datetime value from a string that looks like your pattern? (an INFORMAT)
Or is your problem with producing a report that writes the datetime values like your pattern? (a FORMAT)
Would it be ok for either if you split the text into into pieces so you could read/write the date and the time components separately?
@sanjaymane7 wrote:
Hi, Not working this. I am trying to import csv file through infile statement. In file, date is available in mm/dd/yyyy hr:mm:ss AM/PM. File gets imported but date is appearing blank due to incorrect date format. I need correct informat and format date format. I tried DATEAMPMw.d but error is showing that "Invalid variable specification, DATEAMPMw.d. Variable name of the form X.X must be either First.X or Last X. The name DAT
Show use the data step code used to read the data set. Best would be to include a log with all of the messages from running the data step. Copy the text from the log and paste the copied text into a code box opened on the forum using the forum's {I} icon to preserve code and message layout.
Use the ANYDTDTM informat to read it. Then attach whatever format you want to display the values. Like DATETIME20.;
data test;
length dt 8;
informat dt anydtdtm.;
format dt datetime20.;
infile cards dsd truncover;
input dt;
put dt=;
cards;
12/30/2019 12:30:12 AM
12/30/2019 12:30:12 PM
;
Results:
dt=30DEC2019:00:30:12 dt=30DEC2019:12:30:12
Note care must by used when using ANYDTxxx informats to read dates in MDY or DMY order as how it interprets ambiguous values like 06/05/2019 will change based on your LOCALE settings.
Hi, It's correct code but didn't work on my data. Then, I used mdyampm25. and it's worked.
Thanks for help
@sanjaymane7 wrote:
Hi, It's correct code but didn't work on my data. Then, I used mdyampm25. and it's worked.
Thanks for help
Using the more specific informat of MDYAMPM. will avoid the risk of mis-understood month/day order that could happen with the ANYDTDTM informat. What format you attach doesn't matter since it does not change the datetime values, just how they are printed.
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9. Sign up by March 14 for just $795.
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.