I have some dates in character format:
UN/UNK/2020
14/JUN/2021
UN/APR/2019
I would need to convert them to the format yyyy-mm-dd. If day and month missing, output will be yyyy only, if day missing, output will be yyyy-mm.
Thanks
By "output" do you mean that you want an actual SAS date that displays that way?
Or another character variable that can't use any of the date functions?
Or a numeric value that is not in any way a date except possibly in the readers mind?
SAS date values must have month and day of month component. And a variable can only have one format in effect at any given time. What you request for "g, output will be yyyy only, if day missing, output will be yyyy-mm" is not possible with actual SAS dates.
If you want to use any of the functions such as Intck or Intnx for returning date intervals or adjusting dates, then you will have to provide a day of month and month when missing.
The informats that read yyyymm type generally assume the day of the month to be 1.
You would have to decide what makes sense for your purpose what date of the year just the year value would mean.
Question about these type of "dates" where either the day or the month (or both) has been asked and answered many times:
https://communities.sas.com/t5/SAS-Programming/convert-to-iso8601-format/m-p/613845
https://communities.sas.com/t5/SAS-Programming/Re-Partial-date-conversion/m-p/575008
and many other times. Lots of ideas, lots of answers.
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!
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.
Ready to level-up your skills? Choose your own adventure.