Years ago I saw a line of SAS code that was really puzzling. It was a statement that started with:
if 0 then … ;
What? This was a statement that would always be evaluated as false. Why would anyone write such a statement? Recently, I was discussing with a good friend of mine a macro I wrote where I used an IF statement that was always false.
In some programming languages, the "IF 0" trick ensures that the code that follows would never be executed. But with the DATA step, SAS actually does still evaluate the "zeroed-out" code during the compile phase.
Let's see why this can actually be useful.
The problem I was trying to solve was to assign the number of observations in a data set to a macro variable. First, the code, then the explanation.
if 0 then set Oscar Nobs=Number_of_Obs;
The SET option Nobs= places the number of observations in data set Oscar in a variable I called Number_of_Obs. This happens at compile time, before any observations are read from data set Oscar. The CALL SYMPUTX routine takes two arguments. The first is the name of a macro variable (Number) and the second is the name of a SAS variable (Number_of_Obs). The call routine assigns the value of the SAS variable to the Macro variable.
Most data sets stop executing when you reach the end-of-file on any data set. Because you are not reading any values from data set Oscar, you need a STOP statement to prevent the Data Step from looping. When this Data Step executes, the number of observations in data set Oscar is assigned to the macro variable called Number.
Now you, too, can write programs using this useful trick. The macro where I used this trick can be found in my book, Cody's Data Cleaning techniques, 3rd edition. You can see a copy of this macro (and all the other macros described in the book) by going to my author web site: support.sas.com/cody, scrolling down to the Data Cleaning book and clicking on the link to see example and data. One of the macros that use this trick is called HighLow.sas. As an alternative, you can buy the book!
By the way, you can download programs and data from any of my books by using this same method and it is free!Books by Ron Cody See them all!
A DATA step compiler trick to get the record count was published on SAS Users.