2 users online | 2 Guests and 0 Registered

Can I write SAS dates in bespoke formats?


There are a series of Date-Time directives (listed elsewhere on the site) which can be used to build bespoke formats using the PICTURE statement and these will give you a wide range of options.  For example this code:

proc format ;
  picture wordydt (default = 200)
  low - high = '%A the %D day of %B in the year of our Lord %Y' (datatype = date) ;
run ;

data test ;
  format dt wordydt. ;
  dt = today() ;
run ;

will give you a date formatted to look like Thursday the 24 day of August in the year of our Lord 2023 but you are still limited by the available directives.

While not 'formatting' data, it is possible to construct a character string which will render the value in any style desired.  Using the following code gives complete control:

 
proc format ;
  value wordth
   1 = 'First'
   2 = 'Second'
   3 = 'Third'
   4 = 'Fourth'
   5 = 'Fifth'
   6 = 'Sixth'
   7 = 'Seventh'
   8 = 'Eighth'
   9 = 'Ninth'
  10 = 'Tenth'
  11 = 'Eleventh'
  12 = 'Twelfth'
  13 = 'Thirteenth'
  14 = 'Fourteenth'
  15 = 'Fifteenth'
  16 = 'Sixteenth'
  17 = 'Seventeenth'
  18 = 'Eighteenth'
  19 = 'Nineteenth'
  20 = 'Twentieth'
  21 = 'Twenty-First'
  22 = 'Twenty-Second'
  23 = 'Twenty-Third'
  24 = 'Twenty-Fourth'
  25 = 'Twenty-Fifth'
  26 = 'Twenty-Sixth'
  27 = 'Twenty-Seventh'
  28 = 'Twenty-Eighth'
  29 = 'Twenty-Ninth'
  30 = 'Thirtieth'
  31 = 'Thirty-First'
  ;
run ;

data test2 ;
  format dt date9. ;
  dt = '1jan1906'd ;
  dt_str = catx( ' '
                ,put(weekday(dt),downame.)
                ,'the'
                ,put(day(dt),wordth.)
                ,'of'
                ,put(dt,monname.)
                ,'in the year of our Lord'
                ,catx( ' '
                      ,propcase(put(floor(year(dt)/100)*100,words40.))
                      ,'and'
                      ,propcase(put(mod(year(dt),100),words40.))
                     )
               ) ;
run ;

returning a date in the form Sunday the First of January in the year of our Lord One Thousand Nine Hundred and Six.

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

You cannot comment on this entry

Chuck Norris has counted to infinity. Twice.

Records in this category

Tags