Can I write my own SAS date formats?


Yes, this is possible using the PICTURE format in conjunction with date / time directives.

For example there is a system defined format YYQw. which writes dates with a 'Q' separating the year and quarter numbers (2021Q3) and the YYQDw. which uses a dash as the separator (2021-3), but what if you wanted both a dash and Q? (2021-Q3)

First create a temporary format using the FORMAT procedure.  Using the PICTURE statement allows you to combine text and values, and the date time directives allow you to insert specific values - just be sure to include a DATATYPE definition to resolve these correctly.

proc format ;
  picture yrdq
    low = high = '%Y-Q%q' (datatype = date) ;
run ;

Applying this format:

%let runq = %sysfunc(intnx(quarter,%sysfunc(today()),-1,e),yrdq7.) ;
%put &=runq ;

will return the appropriate value in the LOG.

Examples of the date time directives are as follows:

Date Time Directives

Author:
Alan D Rudland
Revision:
1.1
Average rating:0 (0 Votes)

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.