BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
sanjaymane7
Obsidian | Level 7
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
1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

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.  

View solution in original post

7 REPLIES 7
PaigeMiller
Diamond | Level 26

@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

https://documentation.sas.com/?cdcId=pgmmvacdc&cdcVersion=9.4&docsetId=fedsqlref&docsetTarget=n10z3a...


Also, no need to create a new data set, just change the format in the existing data set.

--
Paige Miller
Tom
Super User Tom
Super User

@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
Obsidian | Level 7
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
ballardw
Super User

@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.

Tom
Super User Tom
Super User

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
Obsidian | Level 7

Hi, It's correct code but didn't work on my data. Then, I used mdyampm25. and it's worked.

 

Thanks for help

Tom
Super User Tom
Super User

@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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

Mastering the WHERE Clause in PROC SQL

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.

Discussion stats
  • 7 replies
  • 1602 views
  • 1 like
  • 4 in conversation