SAS date format - Struggling to get a neater solution

85 Views Asked by At

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;
3

There are 3 best solutions below

0
Stickleback On BEST ANSWER

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;
0
data _null_ On

From SAS Documentation

options dflang=dutch;
    data _null_;
    input date;
    put date eurdfwdx29.;
    put date nlddfwdx29.
    put date itadfwdx17.;
    datalines;
    15342
    ;
0
Therkel On

Inspired by FORMAT Procedure on the SAS Help Center, it looks like you want to use a picture format.

PROC FORMAT;
   PICTURE dmy(DEFAULT = 8) OTHER = '%0d%0b%Y' (DATATYPE = date);
RUN;

OPTION LOCALE = nl_NL;

DATA _NULL_ ;
    DO m = 1 to 12;
        date = MDY(m,1,2023);
        PUT date = DMY.;
    END;
RUN;

This writes to the log

date=01jan202
date=01feb202
date=01mrt202
date=01apr202
date=01mei202
date=01jun202
date=01jul202
date=01aug202
date=01sep202
date=01okt202
date=01nov202
date=01dec202