Friday, April 28, 2017

Advanced SAS – %STR, %BQUOTE (SAS)

%STR Function – The %STR Function serves as a way to assign variables within SAS which contain token character data.

Some of these tokens include:

; + - * / , < > = blank # LT GT EQ

Let’s say that, for whatever reason, you wanted to write a very complicated block of code. Within this code block, you want to assign the value:

If var1 = var2 then varflag = 1;

To the variable progvar1.

You would not simply be able to write:

%let progvar1 = If var1 = var2 then varflag = 1;;

Doing so would cause a SAS error as you have an assignment and an "IF" statement all occurring at once. To prevent this from happening, so that you may complete the assignment, and after such, call the newly assigned &progvar1, you will need to utilize %STR.

The utilization of the %STR function for this occasion would resemble:

%let progvar1 = %STR( If var1 = var2 then varflag = 1;);

This not only makes the code appear cleaner, but also allows it to run.

%BQUOTE Function – The %BQUOTE function enables the SAS user to place token character data within a variable without having to match quotation marks.

For example, if you wanted to assign the value:

The Data Scientist’s Blog hasn’t been updated!

to a variable. The utilization of apostrophes would cause SAS to return an error. To prevent this from happening, you would utilize %BQUOTE. The variable assignment would resemble:

%let progvar2 = %BQUOTE (The Data Scientist’s Blog hasn’t been updated!);

This assignment allows progvar2 to maintain the data as if it were a quoted string without having to fuss over syntax.

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.