I've got a datetime variable called admit that is in the datetime16. format. Example: 31JUL20:23:59:35
But I hate that formatting when I'm trying to read the results of a PROC PRINT.
Here's how I'd like the above value to appear in my output: JUL/31/2020_23:59:35
Is there a way I can write a customized datetime format to do that?
Thanks!
Andrew
@DocMartin wrote:
I've got a datetime variable called admit that is in the datetime16. format. Example: 31JUL20:23:59:35
But I hate that formatting when I'm trying to read the results of a PROC PRINT.
Here's how I'd like the above value to appear in my output: JUL/31/2020_23:59:35
Is there a way I can write a customized datetime format to do that?
Thanks!
Andrew
If your really want that appearance:
proc format; picture mydatetime low-high='%3B/%0d/%Y_%H:%0m:%0s' (datatype=datetime) ; run; data example; x= "31JUL20:23:59:35"dt; put x=mydatetime.; run;
You will have to make sure the format is available in any session you want to use it.
The Picture statement is one of the few where single and double quotes make a difference. If you use double quotes in the picture then SAS will think the %(value) are macro related and not Picture statement date, datetime or time directives.
You can create a custom PICTURE format that will look exactly the way you want it to look. Here's the documentation: https://documentation.sas.com/?cdcId=pgmsascdc&cdcVersion=9.4_3.4&docsetId=proc&docsetTarget=p0n990v...
And a small example: https://sasnrd.com/sas-date-datetime-proc-format-example/
@DocMartin wrote:
I've got a datetime variable called admit that is in the datetime16. format. Example: 31JUL20:23:59:35
But I hate that formatting when I'm trying to read the results of a PROC PRINT.
Here's how I'd like the above value to appear in my output: JUL/31/2020_23:59:35
Is there a way I can write a customized datetime format to do that?
Thanks!
Andrew
If your really want that appearance:
proc format; picture mydatetime low-high='%3B/%0d/%Y_%H:%0m:%0s' (datatype=datetime) ; run; data example; x= "31JUL20:23:59:35"dt; put x=mydatetime.; run;
You will have to make sure the format is available in any session you want to use it.
The Picture statement is one of the few where single and double quotes make a difference. If you use double quotes in the picture then SAS will think the %(value) are macro related and not Picture statement date, datetime or time directives.
If you want something "quick and dirty," you can always do something like:
%LET Dt = %SYSFUNC(PUTN('31JUL20:23:59:35'dt, datetime19.));
%LET Dt = %QSUBSTR(&DT,3,3)/%QSUBSTR(&DT,1,2)/%QSUBSTR(&Dt,6,4)_%QSUBSTR(&Dt,11,2):%QSUBSTR(&Dt,14,2):%QSUBSTR(&Dt,17,2);
%PUT NOTE: &=Dt;
I some times will use something like that for a Title statement when it's a custom request and it's not something I see a lot of repeated use for.
Jim
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.