Search code examples
sas

name prefix and suffix removal SAS


I am looking for a way to remove a list of prefix and suffixes from the name variable

For example

under name I have

Mr. Walter White Jr.

I wish to keep just Walter White

I have list of prefixes and suffixes that I can use a reference

thanks in advance


Solution

  • Use regular expressions with your list to replace them with blank values.

    data have;
        infile datalines dlm='|';
        length name $20.;
        input name$;
        datalines;
    Mr. Walter White Jr.
    Mrs. Skyler White
    Dr. Saul Goodman
    Mr. Jesse Pinkman
    Mr. Gus Fring
    ;
    run;
    
    data want;
        set have;
        
        name = strip(prxchange('s/(Mr.|Jr|Dr)\.?//', -1, name) );
    run;
    

    Output:

    name
    Walter White
    Skyler White
    Saul Goodman
    Jesse Pinkman
    Gus Fring