SAS Proc Format - Format a character based on a substring

217 views Asked by At

I want to know if it is possible to create a custom format that will work on a range of character values by taking a substring and formatting it base on that.

e.g. A001, B001, C001 are all formatted to 'JAN' A002, B002, C002 are all formatted to 'FEB'

Is this possible?

value $custom_format'A001', 'B001', 'C001' = 'JAN'
'A002', 'B002', 'C002' = 'FEB';
etc.

I know you can just list specific character values you want formatted (as above), but I would like this to work for any letter at the begining and only read the last 3 characters to determin the format, without having to list all possible iterations.

1

There are 1 answers

0
Stu Sztukowski On BEST ANSWER

Use substr to only pull the last 3 values and create a format based on that.

proc format;
    value $custom_format
        '001' = 'JAN'
        '002' = 'FEB'
    ;
run;

/* substr(var, 2) will start at the second character and read until the end */
data test;
    var = 'A001';
    month = put(substr(var, 2), $custom_format.);
    output;

    var = 'B002';
    month = put(substr(var, 2), $custom_format.);
    output;
run;

Output:

var     month
A001    JAN
B002    FEB

If you need something more complex, you can supply regex to formats in SAS.