01 April 2008

SAS Macros

What do you code to create a macro? End one?

Beginning of a macro is defined by %macro and ending of a macro is defined by %mend.


Describe how you would pass data to a macro.


We pass data into a macro using macro parameters.
%macro
plot(yvar= ,xvar= );
proc plot;
plot &yvar*&xvar;
run;
%mend


Can you execute a macro within a macro? Describe.


Yes, execution of macro within a macro is called nesting.They can be obtained by using symget and call symput macros.


How are parameters passed to a macro?

A macro variable defined in parentheses in a %MACRO statement is a macro parameter. Macro parameters allow you to pass information into a macro.

Here is a simple example:

%macro
plot(yvar= ,xvar= );
proc plot;
plot &yvar*&xvar;
run;
%mend plot;

Can you a macro within another macro? If so, how would SAS know where the current macro ended and the new one began?


Yes, we can execute macros in macros, what we call it as nesting of macros. Macros beginning is defined by keyword %macro and its ending is defined by %mend

Describe how you would create a macro variable

Using the %Let Statement

How do you add a number to a macro variable?

Using the %eval function

How long can a macro variable be? A token?

component of SAS known as the word scanner breaks the program text into fundamental units called tokens.· Tokens are passed on demand to the compiler.· The compiler then requests token until it receives a semicolon.· Then the compiler performs the syntax check on the statement.

How would you code a macro statement to produce information on the SAS log?

Coding the macro's using options like MPRINT MLOGIC MERROR SYMBOLGEN,we can produce the information about the macro in the SAS log.

What system options would help us debug a macro

The system options like

MERROR: SAS will issue warning if we invoke a macro that SAS didn’t find. Presents Warning Messages when there are misspellings or when an undefined macro is called.
SERROR: SAS will issue warning if we use a macro variable that SAS can’t find.
MLOGIC: SAS prints details about the execution of the macros in the log.
MPRINT: Displays SAS statements generated by macro execution are traced on the SAS Log for debugging purposes.
SYMBOLGEN: SAS prints the value of macro variables in log and also displays text from expanding macro variables to the SAS Log.

what would you use to load a value to the macro variable ?

we can load a value to a macro variable using the %let statement. For example

%let city=new york;


What is the maximum length of macro variable?

32 characters long .

What is the difference betweem automatic macro variables and user defined macro variables?

When we invoke SAS ,the macro processor creates automatic macro variables that supply information related to SAS session,these are called automatic macro variables and these are always global.Whereas user defined macro variables are created by the user which can be either local or global.


Describe the ways with which we can create macro variables?

In addition to %let statement, other features of macro language that create macro variable are

%global statement

%local statement

%macro

symput routine etc.

What is the difference between a Symbolgen and %put?

Symbologen is used to print the value of a macro variable resolved on log window whereas %put is used to display user defined messages on log window.

What is Call Symput?

Call Symput routine is used to create a macrovariable whose value is assigned during the execution of data step.

call symput (name,value)

Where call=keyword

symput =Invokes symput routine

name=name of the macrovariable

value=value to be assigned.

1 comment:

  1. These posts on macros are helping me a lot to brush up basics before leaving for interviews ........
    Great job

    ReplyDelete