Saturday 24 December 2016

SAS Macro: Top 10 Interview Questions

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?






Basic SAS Interview Questions

1. Steps from raw data to output ….
2. Have you used macros n how are you with them?
3. Can you give me some system option for debugging macro error?
4. Why do you use Macros?
5. How do u create a macro variable?
6. Difference between Local and Global macros
7. Just list various Procs you have used….
8. What is the use of Proc corr?
9. Can you explain Proc Compare?
10. Can you explain Proc Report?
11. Use of Put statement?
12. Say u have x=a+b; and x = sum (a,b); what result is expected?
13. Where do you use proc Formats and label?
14. What do you mean by calculated / derived variable?
15. Getting data from databases such as oracle
16. Which functions are used to convert Numeric to character variables and vice-versa?
17. Proc Univariate advantages.
18. How do you use your statistical skills in your last projects?
19. What's the difference between proc means and function means?
20. How do you use ODS?
21. How do you use MACRO?
22. What's the difference between SQL and MERGE?
23. What would you do with data validation? What's your process?
24. How would you transport data?
25. How would you rank your expertise in SAS Base in a scale of 1 to 10?
26. How would you rank your expertise in using MACRO in a scale of 1 to 10?

3 comments:

  1. Thanks for sharing this interview questions with answers.
    It is really helpful.
    SAS Training in Chennai | SAS Institutes in Chennai

    ReplyDelete
  2. Thanks for sharing the descriptive information on SAS Programming. It’s really helpful to me since I'm taking SAS Programming training. Keep doing the good work and if you are interested to know more on SAS Programming Tutorial, do check this SAS Programming.:-https://www.youtube.com/watch?v=4PtMFE4IjLg

    ReplyDelete