Write Picture Formats For Character Variables in SAS

Recently, I came across a question on the SAS Community where the author of the question wanted to mask the last 4 characters in a character variable with ‘XXXX’. My first thought was to use a picture format. However, picture formats are for numeric variables only. Instead, I used the technique presented in my previous post Pass Function Logic to PROC FORMAT in SAS, which solved the problem.

The Approach

First, I have to write a function that does exactly what I want. The function can accept only 1 argument, which is the string to be masked. use the Substr Function to set the last 4 characters in the input string to ‘XXXX’. Then i return the string again.

proc fcmp outlib=work.functions.fun; 
   function mask(string $) $;
      substr(string,max(1, length(string)-3),4)='XXXX';
      return (string);
   endsub;
run;

Next, I use the written mask function directly in PROC FORMAT and apply its logic to all values using the other keyword.

options cmplib=work.functions;
proc format;
    value $ mask (default=200) other=[mask()];
run;

Now, lets us put the format to the test. Below, I write a few danish Social Security Numbers (CPR). Here, the first 6 digits represent only the date of birth of an individual. The last 4 identifies exactly who you are. Therefore these are good candidates for our new format.

data test;
   cpr='2305842018'; output;
   cpr='1411701943'; output;
   cpr='1807994624'; output;
   cpr='0908454712'; output;
   cpr='0203047264'; output;
 
   format cpr $mask.;
run;
 
proc print data=test;
run;

As you can see, the SAS format worked exactly as intended. The last four characters are masked. Naturally, we could have just altered the actual value of the string. However, in some cases this is not the way to go. Maybe you do not want to alter the actual value because you need it for come comparison. You may even be restricted from altering values such as Social Security Numbers. In these cases, this approach comes in handy.

SAS PROC FCMP FORMAT Character Variable Picture

Summary

In this post, I have demonstrated a real life example of the PROC FCMP/PROC FORMAT technique described in a previous post. We have seen how to mask and alter the appearance of character values without touching the actual value. Naturally, this technique does not limit itself to masking characters. You can control all aspects of the appearance of a character value within PROC FCMP. Remember though, that the function to be used in PROC FORMAT can accept one and only one argument.

You can download the entire code from this post here.