Search code examples
sasdate-format

SAS date format - Struggling to get a neater solution


I am currently not at my workplace so I will update this later on. Essentially what I want is to have a date column transform into 2 Januari 2023 (the dutch month names)

I have build a date table and just added an extra column using substring and replace JAN with Januari, FEB with Februari, MAR with Maart, etc. From there I just left join it on every date column in my final table (there are 8 date columns).

Now a more neater solution would be to create a FORMAT for this, but I haven't been able to make this work. Any reference material or code you may have would be very useful.

Personally I have tried this, reading some documentation and chatgpt, but I haven't managed to make it work.

    proc format;
  value $dutch_month_abbrev (default = 20)
  'JAN' = 'Januari'
  'FEB' = 'Februari'
  'MAR' = 'Maart'
  'APR' = 'April'
  'MAY' = 'Mei'
  'JUN' = 'Juni'
  'JUL' = 'Juli'
  'AUG' = 'Augustus'
  'SEP' = 'September'
  'OCT' = 'Oktober'
  'NOV' = 'November'
  'DEC' = 'December';
  
  value mydate (date)
  low-< '01JAN2023'd = ddmmyy.-[$dutch_month_abbrev.]-yyyy4.
  '01JAN2023'd - high = ddmmyy.-[$dutch_month_abbrev.]-yyyy4.;
run;

Solution

  • I believe SAS comes with pre-built formats for this purpose:

    DATA output;
        SET input;
    
        new_date_col = put(old_date_col, NLDDFWDX29.);
    
    RUN;
    

    Here is documentation showing how this is implemented in other languages as well:

    https://www.sfu.ca/sasdoc/sashtml/lgref/z0205146.htm

    Note that the 29. length can be reduced but will need to consider potential truncating depending on length of month name + day and year digits.

    Alternatively, you could retain the date value/functionality and just over lay the format:

    DATA output;
        SET input;
    
        format old_date_col NLDDFWDX29.;
    
    RUN;