Search code examples
sas

Convert yyyy/mm into mmyyyy SAS format


maybe asked many times..but still struggled with it. How to convert the following date format: 2020/11 (yyyy/mm) into NOV2020? I have no day.

Thank you in advance.

Best.


Solution

  • If you have no day of the month then you must have the value as character string.

    data have;
      charvar = '2020/11';
    run;
    

    If you want to create a date value you will have to pick a day of the month to use. Usually the first day of the month is used. You can then display the resulting date with whatever date style format you want.

    data want;
      set have;
      datevar = input(cats(charvar,'/01'),yymmdd10.);
      format datevar monyy7.;
    run;
    

    Result:

    OBS    charvar    datevar
    
     1     2020/11    NOV2020
    

    If you don't actually need a date value then you could just replace the original string with the result of using the PUT() function to apply the MONYY7. format to the date value created by the INPUT() function.

    data want;
      set have;
      charvar = put(input(cats(charvar,'/01'),yymmdd10.),monyy7.);
    run;