Thursday, April 30, 2009

Substring in SAS

In many programming languages, there is a substr() function which will extract a portion of a string. SAS also has such a function. But, in SAS the substr() function is a little bit complicated.

In SAS version-8, if you put the substr() function on the left of the = sign, the substr() is actually a procedure to substitute string. It will replace a portion of the argument string with values at the right of the = sign. This link shows the version-8 syntax and here is the version-9 syntax.

If, however, the substr() function is put at the right of the = sign, the behavior of the substr() function is the normal sub-string function as expected by many ones. It will return a portion of the argument string. This link is the version-8 syntax and here is the version-9 syntax.

In SAS version-8, there is another function called kstrstrb(). Its function is to extract a substring in terms of byte. The DBCS version of this function is called ksubstr(). SAS version-9 also has these two functions.

In short, putting the substr() function on the LHS or RHS of the = sign will have very different behaviours. Putting it on the LHS will alter the argument string, just like passing parameter by reference. Putting it on the RHS is a function call with parameters passed by value and the argument string will NOT be altered.

By the way, when writing SAS macro, there are %substr() and %qsubstr() macro functions doing similar substring extraction processing.




No comments:

Duplicate Open Current Folder in a New Window

Sometimes after I opened a folder in Win7, I would like to duplicate open the same folder again in another explorer window. Then, I can ope...