EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer

SAS Interview Frequently Asked Macro Questions -2

1. Have you used macros? For what purpose you have used?
Yes I have, I used macros in creating analysis datasets and tables where it is necessary to make a small change through out the program and where it is necessary to use the code again and again.

2. How would you invoke a macro?
After I have defined a macro I can invoke it by adding the percent sign prefix to its name like this: % macro name a semicolon is not required when invoking a macro, though adding one generally does no harm.

3. How can you create a macro variable with in data step? => with CALL SYMPUT

4. How would you identify a macro variable? ==> with Ampersand (&) sign

5. How would you define the end of a macro?
The end of the macro is defined by %Mend Statement

6. For what purposes have you used SAS macros?
If we want use a program step for executing to execute the same Proc step on multiple data sets. We can accomplish repetitive tasks quickly and efficiently. A macro program can be reused many times. Parameters passed to the macro program customize the results without having to change the code within the macro program. Macros in SAS make a small change in the program and have SAS echo that change thought that program.

7. What is the difference between %LOCAL and %GLOBAL?

% Local is a macro variable defined inside a macro.%Global is a macro variable defined in open code (outside the macro or can use anywhere).

8. How long can a macro variable be? A token?
A 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.

9. If you use a SYMPUT in a DATA step, when and where can you use the macro variable?
The macro variable created by the CALL SYMPUT routine cannot be used in the same datastep in which it got created. Other than that we can use the macro variable at any time..

10. What do you code to create a macro?
We create macro with %MACRO statement and end macro with %MEND statemnt.

11. What is the difference between %PUT and SYMBOLGEN?
%PUT is used to display user defined messages on log window after execution of a program where as % SYMBOLGEN is used to print the value of a macro variable resolved, in log window.

12. How do you add a number to a macro variable?
Using %eval function or %sysevalf function if the number is a floating number.

13. Can you execute a macro within a macro? Describe.
Yes, Such macros are called nested macros. They can be obtained by using symget and call symput macros.

14. If you need the value of a variable rather than the variable itself what would you use to load the value to a macro variable?
If we need a value of a macro variable then we must define it in such terms so that we can call them everywhere in the program. Define it as Global. There are different ways of assigning a global variable. Simplest method is %LET.

Ex: A, is macro variable. Use following statement to assign the value of a rather than the variable itself %Let A=xyz; %put x="&A";

This will assign "xyz" to x, not the variable xyz to x.

15. Can you execute macro within another macro? If so, how would SAS know where the current macro ended and the new one began?
Yes, I can execute macro within a macro, we call it as nesting of macros, which is allowed. Every macro's beginning is identified the keyword %macro and end with %mend.

16. 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;

17. How would you code a macro statement to produce information on the SAS log?
This statement can be coded anywhere?

18. How we can call macros with in data step?
We can call the macro with CALL SYMPUT, Proc SQL , %LET statement. and macro parameters.

Related links:

Continue SAS Interview Frequently Asked Macro Questions -3   SAS Tutorial
SAS Interview Questions -1: Top 10 SAS Macro Interview Questions   SAS Interview