Wednesday, February 20, 2013

One shot to macro interview question

Image and video hosting by TinyPic
Certainly macro is an important part of SAS programming, with various ways for designing interview questions. But almost the only reason interviewers ask about macro is to determine whether you are clear about the difference between preprocessing and processing.

1. Definition
Let's take a look at "Preprocessor", which does the work of preprocessing.
In Wikipedia, "Preprocessor" is:
"In computer science, a preprocessor is a program that processes its input data to produce output that is used as input to another program. The output is said to be a preprocessed form of the input data, which is often used by some subsequent programs like compilers. "
As for "Macro":
"A macro in computer science is a rule or pattern that specifies how a certain input sequence (often a sequence of characters) should be mapped to a replacement input sequence (also often a sequence of characters) according to a defined procedure."

In other words, raw source code is first met with the language scanner (word scanner), where sections of it are syntactically translated by preprocessor and hanged in there for action, rather than to the language compiler.

In SAS, the preprocessor component is called the macro processor. The "macro facility" refers to the macro processor together with the coding language supplied for the preprocessor and the features for using preprocessor variables.

This figure illustrates it well, from SAS Macro: Beyond the Basics :
Image and video hosting by TinyPic
The macro preprocessor will kick in once the word scanner detects the trigger "%" or "&". After the macro preprocessor resolves the macro variables, the normal process will continue.
But what's the details about this interchange between "Preprocessing" and "Process"?

2. Details
The following figures can be found from SAS website, How SAS Processes Statements with Macro Activity 
Image and video hosting by TinyPic
The macro processor starts to work once the word scanner found the % and writes an entry in the symbol table.
Image and video hosting by TinyPic
Then the word scanner will continue to work after the macro processor stops.
During macro compilation, the macro processor does the following:
  • creates an entry in the session catalog
  • complies and stores all macro program statements for that macro as macro instructions
  • stores all noncomplied items in the macro as text

3. Question
Sounds easy, right?
Here is a question for you:
What is the difference between symbol table and PDV (program data vector) ?
From Bancova.com

Tuesday, February 19, 2013

Test your macro

Macro programming is generally considered an advanced topic. But during the training at Bancova I've found it easy to learn the basic concepts, as well as simple implementation for daily task.
Here is a short presentation I gave in the summer, a quick test of your macro skills:


You can also watch my video as you go through the ppt above.
Now I want to show you two examples of using the macro.
The first "setr" macro will generate a column of random number in the datasets.
https://gist.github.com/4083348
With the first "setr", the second "setcat" macro can create a categorical variable with the assigned number of observations for each group in random sequence. This is good for data simulation.
https://gist.github.com/4083405
For instance, if in the dataset "all" with 10 observations, two variables "Subjid" and "Treatment", we want to create a new dataset "dis" with a new variable "Reasons" based on the "all".

We can simply use the following code:
%setcat(indsn=all, outdsn=dis, rn=0, varname=Reasons, len=40,
cat=Disease Progression|Adverse event|Subject Choice|Investigator discretion,
ncat=2|2|3|3);
Then we will get the new dataset "dis" we want like this:

Easy enough, right? Thanks for coming to my blog! See you next time!