EZ Study

Actuarial Biology Chemistry Economics Calculators Confucius Engineer
Physics
C.S.


SAS Interview Frequently Asked Macro Questions -3

19. Tell me about call symput?
CALL SYMPUT takes a value from a data step and assigns it to a macro variable. I can then use this macro variable in later steps. To assign a value to a single macro variable, I use CALL SYMPUT with this general form:
CALL SYMPUT ("macro-variable-name", value);

Where macro-variable-name, enclosed in quotation marks, is the name of a macro variable, and value is the value I want to assign to that macro variable. Value can be the name of a variable whose value SAS will use, or it can be a constant value enclosed quotation marks.

CALL SYMPUT is often used in if-then statements such as this:
If age>=18 then call symput ("status","adult");
else call symput ("status","minor");


These statements create a macro variable named &status and assign to it a value of either adult or minor depending on the variable age.Caution: We cannot create a macro variable with CALL SYMPUT and use it in the same data step because SAS does not assign a value to the macro variable until the data step executes. Data steps executes when SAS encounters a step boundary such as a subsequent data, proc, or run statement.

20. Tell me about % include and % eval?
The %include statement, despite its percent sign, is not a macro statement and is always executed in SAS, though it can be conditionally executed in a macro.It can be used to setting up a macro library. But this is a least approach.

The use of %include does not actually set up a library. The %include statement points to a file and when it executed the indicated file (be it a full program, macro definition, or a statement fragment) is inserted into the calling program at the location of the call.

When using the %include building a macro library, the included file will usually contain one or more macro definitions.%EVAL is a widely used yet frequently misunderstood SAS(r) macro language function due to its seemingly simple form.

However, when its actual argument is a complex macro expression interlaced with special characters, mixed arithmetic and logical operators, or macro quotation functions, its usage and result become elusive and problematic. %IF condition in macro is evaluated by %eval, to reduce it to true or false.

21. Describe the ways in which you can create macro variables?
There are the 5 ways to create macro variables:

%Let %Global Call Symput Proc SQl into clause Macro Parameters.


22. Tell me more about the parameters in macro?
Parameters are macro variables whose value you set when you invoke a macro. To add the parameters to a macro, you simply name the macro vars names in parenthesis in the %macro statement.

Syntax: %MACRO macro-name (parameter-1= , parameter-2= , ...parameter-n = );
macro-text%;
%MEND macro-name;
%macro_name(par1,par2,....parn);


23. What is the maximum length of macro variable? => 32 characters long.

24. Automatic variables for macro?
Every time we invoke SAS, the macro processor automatically creates certain macro var. eg: &sysdate &sysday.

25. What system options would you use to help debug a macro?
The SAS System offers users a number of useful system options to help debug macro issues and problems. The results associated with using macro options are automatically displayed on the SAS Log. Specific options related to macro debugging appear in alphabetical order in the table below:

MEMRPT: Specifies that memory usage statistics be displayed on the SAS Log.
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 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.

26. How to know how &&var&i or &&dsn&i resolves?
It is very confusing some times to tell rightaway how &&var&i or &&dsn&i get resolved... but here is the simple technique you can use to know....

ex: We generally use &&var&i or &&dsn&i these macro variables when we are using a %do loop... to execute same code n number of times. You have a dataset and it has 5 variables ... Patid sex age ethnic race wt ht;

%macro doit;
%do i=1 %to &nvars %by (integer only);
&&var&i
%end;
%mend doit;


So if the nvars value is 7, then the loop creates a macro variable list of
&var1   &var2   &var3   &var4   &var5   &var6   &var7
which further get resolved to
patid sex age ethnic race wt ht
You can always use Macro debugging option SYMBOLGEN to see how each macro variable got resolved. If it's not by integer, then you need to do some extra tricky step like: %eval(&i/10).

Related links:

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