Use the IN operator in macro

The IN operator is one convenient checking tool in SAS which can be used in place of lots of OR statements. And it’s quite common in data step.

data test;
  if 3 in (1, 2, 3, 4, 5, 6) then
    put "Value found within list.";
  else put "Value not in list.";
run;

Sometimes, you will come across a situation where you have to write a macro program where a macro variable has more than one value. You may think to use multiple OR operators as below…

* Prior to SAS9.2, the following syntax was used;
%macro test(value); 
  %if &value=1 or &value=2 or &value=3 or &value=4 or
      &value=5 or &value=6 %then %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Starting SAS 9.2, there is an IN operator for the macro language. The IN operator can now be used on the %IF statement when the MINOPERATOR option is set on the %MACRO statement or as a SAS system option.

%macro test(value)/minoperator;
  %if &value in 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

Or you can use character # (new binary comparison operator) as an alternate operator to mnemonic IN operator.

%macro test(value)/minoperator;
  %if &value # 1 2 3 4 5 6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

They both work fine. MINOPERATOR option tells SAS to recognize the word ‘IN’ or # by the SAS macro facility as a logical operator in expressions.

And there is another way of writing code which is more close to the way we use IN operator in data step. By default, a space is used as the delimiter for the operator, but can be changed by using the MINDELIMITER= %MACRO statement option.

options minoperator;

%macro test(value)/mindelimiter=',';
  %if &value in 1,2,3,4,5,6 %then
    %put Value found within list.;
  %else %put Value not in list.;
%mend;

%test(3)

 

Reference: 

http://support.sas.com/kb/35/591.html

http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a003092012.htm

Use the superassignment operator

One of the most important functional programming principle is that functions do not change non-local variables; that is, generally speaking, the code in a function only has read access to its non-local variables.  This is a quite important feature which can protect the higher-level variable from being changed by local functions. See the example below.

> x <- 10
> test <- function(x) {
+   x <- x - 5
+   print(x)
+ }
> test(x)
[1] 5
> x
[1] 10

However, sometimes you may wish to write to a global variable or any variable higher than the level at which your write statement exists. The superassignment operator, <<-, or the assign() function is what you want. Let’s look at the superassignment operator first.Continue reading

get() function in R

The get() function might be one of the most useful utilities in R. However I’ve never use this function before I write this page. Shame on me.

Well, let’s get to the point. The job of the get() function is actually quite simple: given the name of an object, it fetches the object itself. See the example below:

> x <- c(1:3)
> x
[1] 1 2 3
> get("x")
[1] 1 2 3

It’s easy to imagine how useful this function is.

 

Reference: The Art of R Programming by Norman Matloff

Use SAS system options to suppress Log output

For Windows SAS system, the Output, Log, and Program Editor windows can display approximately 99,999 lines each. Since the number is limited, sometimes the SAS Log windows will be full and the system will show a messages ‘SAS log window is full’. The running program will pause and wait for your action. It’s quite annoying, especially when you run a program with hundreds or thousands of loops.

I run a simulation program with a loop that went 100,000 times recently and it took more than 4 hours to run, even on the sever. And I want to suppress the log output because I don’t want that message to interrupt the program running.

I read the SAS documents carefully, and there are several option available to prevent the log window from filling up.

Solution 1: System options

There are four system options can be used to suppress SAS statements, system messages, and error message, respectively.Continue reading

The difference between using subset() function and ordinary filtering

Well at first, I thought there is no difference between this two methods. And I normally use  these two methods interchangeable when I wrote the R code.

And actually there  is a small difference in how NA values are handled.

> x <- c(6, 1, NA, 10)
> x
[1]  6  1 NA 10
> x[x > 5]
[1]  6 NA 10
> subset(x, x > 5)
[1]  6 10

So when your data have some missing values, for example survey data, choose subset() or filtering method carefully. This tiny difference may cause unpredictable mistake which normally takes you a lot of time to debug the program.

 

Reference: The Art of R Programming by Norman Matloff

Subscribe to Blog via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recommended Blogs