SAS® and R

Best of Both Worlds

More about PRXCHANGE function

with one comment

In a way, CALL PRXCHANGE is very similar to TRANWRD, it searches for regular expression that matches and replaces them with another character string.  However, it has the advantage of performing searches with wildcards, thus giving it more flexibility.

Let’s take a look at the example from post The flexibility of PRX functions in SAS:

prxchange('s/(observation|treatment)//io', -1, visit)

Starting from the first parameter, “s/” in the beginning tells us we are doing substitution.  Inside the parentheses are the words were are looking to replace.  So in this case, we are looking for either “observation” or “treatment”.  What PRX function does here is it searches for these words, and put them in a buffer, and replace them with the words specified after the ‘/‘ sign.  In our case, we are trying to remove these words, so we leave substitution as empty.

“/i” is useful because it specifies that the search is case insensitive.  It’s basically an “ignore case option”.  The regular expression is recompiled on every loop of the data step.  In our situation, we do not need that so we can add the “o” option to the end of the regular expression to tell it to just compile it once.

The second parameter to the prxchange() function is -1 and just tells the function to keep searching the source, finding and replacing every occurrence till you get to the end of source.

The third parameter “visit” just tells the function which source variable to search.

So here you have it, a simple solution once you understand what’s behind that “expression.”

Advertisements

Written by sasandr

May 7, 2012 at 9:34 pm

Posted in SAS

Tagged with ,

One Response

Subscribe to comments with RSS.

  1. Now it is more clear. Duo Xie!

    Xiao

    May 16, 2012 at 2:46 pm


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: