Thursday, May 31, 2012

SAS Macro: Top 10 Interview Questions

1. Can I use SAS functions within the macro facility?
Answer:
Yes, by using the %SYSFUNC macro
function.

Example: %put %sysfunc(date(),worddate20.);

2. What quoting function should I use to mask special characters?

Depends. Use compile-time or execution-time quoting functions for your situation.

Compile-time quoting functions:

 %STR–masks commas, mnemonics, and unmatched quotation marks and parentheses.
 %NRSTR–masks percent signs and ampersands in addition to the other special characters.

Execution-time quoting functions:


 %BQUOTE–masks special characters and mnemonics in resolved values during macro execution.
 %SUPERQ–masks special characters and mnemonics during macro execution. However, it also prevents further resolution of any macros or macro variables.

Example: Unmatched Quotation Mark (‘)
%let singleq=O'neill;
%put &singleq;

Solution: %STR Function
%let singleq=%str(O%'neill)
%put &singleq;

Example: Percent Sign (%)
%let ex=This macro is called %showme;
%put ex=&ex;

log message

20 %let ex= This macro is called %showme;
WARNING: Apparent invocation of macro SHOWME not resolved.
21 %put ex=&ex;
WARNING: Apparent invocation of macro SHOWME not resolved.

Solution: %NRSTR Function
%let ex=%nrstr(This macro is called %showme);
%put ex=&ex;


3. How do I resolve a macro variable within single quotation marks?

Use the %STR and %UNQUOTE
functions.


4. How do I resolve error messages when output that was generated with the MPRINT system option looks fine?


Use the %UNQUOTE function


5. What are the differences between the autocall facility and the stored compiled macro facility?

Autocall Facility


Macro source code is implicitly included and compiled to the WORK.SASMACR catalog.
To use autocall macros, you have to set the MAUTOSOURCE and the SASAUTOS= system options.
The MAUTOSOURCE option activates the autocall facility.
The SASAUTOS= option specifies the autocall library or libraries

Stored Compiled Macro Facility

The stored compiled macro facility provides access to permanent SAS catalogs from which you can invoke compiled macros directly.
To use these stored compiled macros, you have to set the MSTORED and the SASMSTORE= system options.
The MSTORED option searches for the compiled macros in a SAS catalog that you reference with the SASMSTORE= option.
The SASMSTORE= option specifies the libref for the SAS library that contains the stored compiled macros.

6. How do I conditionally execute a macro from within a DATA step?

Use the CALL EXECUTE routine

Example: CALL EXECUTE routine

/* Compile the macro BREAK. The BYVAL */
/* parameter is generated in the CALL */
/* EXECUTE routine. */
%macro break(byval);
data age_&byval;
set sorted(where=(age=&byval));
run;
%mend;
proc sort data=sashelp.class out=sorted;
by age;
run;
options mprint;
data _null_;
set sorted;
by age;
if first.age then call execute(‘%break(‘||age||’)’);
run;


7. Why does my macro variable not resolve?

The macro variable might not resolve for a few reasons:
 macro variable scope: Local macro variables versus global macro variables.
 no step boundary:The DATA step does not have an ending RUN statement before a CALL SYMPUT or a CALL SYMPUTX routine.

Example: No RUN statement before %PUT statement:
data test;
x="MYVALUE";
call symputx('macvar',x);
%put WILL I RESOLVE &macvar?;


 timing issues: a result of using the CALL EXECUTE and CALL SYMPUT routines together.



8. How can I use macros to loop through all files in a directory?

Use the %DO statement


9. Can I use DATA step variables in a %IF-%THEN statement?

No, DATA step variables cannot be used
in %IF-%THEN statements.


10. Why am I getting an error that states that a character operand was found in the %EVAL function?


Use the %SYSEVALF function

No comments:

Post a Comment

Programming blog Top  blogs