SAS Macros Interview Questions and Answers
This document provides a collection of interview questions and answers related to SAS Macros, which are a powerful feature in SAS for automating repetitive tasks and dynamically generating code.
1. What is a macro in SAS?
A macro in SAS is a code that generates SAS statements dynamically. Macros allow for automation, reusability, and parameterization of code, making it more flexible and efficient.
2. How do you create a simple macro in SAS?
You can create a macro in SAS using the %MACRO
and %MEND
statements. Here's an example:
%macro simple_macro;
data new_data;
set old_data;
/* Your SAS code here */
run;
%mend simple_macro;
%simple_macro;
3. What is the difference between a macro variable and a data step variable?
Macro variables are defined and used in the macro language (outside of the DATA step) and are available throughout the SAS session. Data step variables are used within a DATA step and are not accessible outside of that step unless explicitly passed to a macro.
4. How do you define a macro variable using %LET
?
A macro variable is defined using the %LET
statement in SAS.
%let varname = value;
5. How do you define a macro variable using CALL SYMPUT
?
CALL SYMPUT
is used within a DATA step to assign a value to a macro variable based on data or calculations within that step.
data _null_;
call symput('varname', value);
run;
6. What is the use of the %IF-%THEN
statement in macros?
The %IF-%THEN
statement in a macro is used to perform conditional logic based on the value of macro variables.
%macro check_value(val);
%if &val = 1 %then %do;
%put Value is 1;
%end;
%else %do;
%put Value is not 1;
%end;
%mend check_value;
%check_value(1);
7. How do you create a macro with eters?
You can create a macro with parameters by specifying the parameters in parentheses after the macro name.
%macro example_macro(test1, test2);
%put &test1 &test2;
%mend example_macro;
%example_macro(value1, value2);
8. Explain the difference between positional and keyword parameters in macros.
Positional parameters are passed in the order they are defined, while keyword parameters are specified by name and can be passed in any order. Keyword parameters can also have default values.
%macro example_macro(test1=default1, test2=default2);
%put &test1 &test2;
%mend example_macro;
%example_macro(test2=value2);
9. How do you debug a macro in SAS?
Macro debugging can be done using options such as MPRINT
, MLOGIC
, and SYMBOLGEN
. These options display macro execution details in the SAS log.
options mprint mlogic symbolgen;
%example_macro;
10. What is the purpose of the %DO-%END
statement in a macro?
The %DO-%END
statement is used to create a block of code that repeats a certain number of times or executes conditionally within a macro.
%macro loop_macro;
%do i = 1 %to 5;
%put Iteration &i;
%end;
%mend loop_macro;
%loop_macro;
11. How do you pass a macro variable to a DATA step?
You can pass a macro variable to a DATA step by referencing it with an ampersand (&
) within the DATA step.
%let threshold = 100;
data filtered_data;
set original_data;
if value > &threshold;
run;
12. What is the difference between %GLOBAL
and %LOCAL
macro variables?
%GLOBAL
macro variables are accessible throughout the entire SAS session, while %LOCAL
macro variables are only available within the macro in which they are defined.
%macro test_macro;
%local local_var;
%global global_var;
%let local_var = Local;
%let global_var = Global;
%put &local_var &global_var;
%mend test_macro;
%test_macro;
%put &global_var; /* Global variable is accessible */
%put &local_var; /* Local variable is not accessible outside the macro */
13. How do you concatenate macro variables?
Macro variables can be concatenated by placing them next to each other or by using the %STR()
function to prevent unwanted interpretation of special characters.
%let var1 = Hello;
%let var2 = World;
%put &var1 &var2; /* Outputs: Hello World */
%put %str(&var1&var2); /* Outputs: HelloWorld */
14. Explain the use of the %EVAL
function in macros.
%EVAL
is used to evaluate arithmetic and logical expressions in macro language.
%let sum = %eval(5 + 3);
%put ∑ /* Outputs: 8 */
15. How do you create a dynamic macro that adapts to different datasets?
You can create a dynamic macro by using macro parameters and conditional logic to adapt the code to different datasets or requirements.
%macro dynamic_macro(dataset, var);
proc print data=&dataset;
var &var;
run;
%mend dynamic_macro;
%dynamic_macro(sashelp.class, age);
16. What is the purpose of the %SYSFUNC
function?
%SYSFUNC
allows you to execute DATA step functions within a macro. It is useful for performing tasks like formatting dates or manipulating strings within macro code.
%let current_date = %sysfunc(today(), date9.);
%put ¤t_date; /* Outputs the current date in DATE9 format */
17. How do you create a macro that conditionally executes code?
You can create a macro that conditionally executes code using %IF-%THEN
statements within the macro definition.
%macro conditional_macro(flag);
%if &flag = Y %then %do;
%put Executing block 1;
%end;
%else %do;
%put Executing block 2;
%end;
%mend conditional_macro;
%conditional_macro(Y);
18. How do you use the %INCLUDE
statement in a macro?
The %INCLUDE
statement is used to include external SAS code files within a macro or program.
%macro include_macro;
%include 'path-to-file.sas';
%mend include_macro;
%include_macro;
19. How do you debug a complex macro?
For debugging complex macros, you can use the debugging options (MPRINT
, MLOGIC
, SYMBOLGEN
) and break the macro into smaller parts, testing each part individually.
20. What is the CALL EXECUTE
statement and how is it used with macros?
CALL EXECUTE
is used to dynamically generate SAS code that is executed after the current DATA step completes. It is often used in conjunction with macros to dynamically generate and execute code.
data _null_;
set dataset;
call execute('%my_macro(' || var || ')');
run;
21. How do you use the %DO
loop to repeat a block of code in a macro?
The %DO
loop allows you to repeat a block of code multiple times within a macro.
%macro repeat_macro;
%do i = 1 %to 5;
%put This is iteration &i;
%end;
%mend repeat_macro;
%repeat_macro;
22. How do you conditionally include or exclude code in a macro?
Conditional inclusion or exclusion of code in a macro can be done using %IF-%THEN
statements.
%macro condition_macro(flag);
%if &flag = 1 %then %do;
proc print data=sashelp.class;
run;
%end;
%else %do;
proc contents data=sashelp.class;
run;
%end;
%mend condition_macro;
%condition_macro(1);
23. How do you use the %PUT
statement in SAS macros?
The %PUT
statement is used to write text or the value of macro variables to the SAS log.
%macro log_macro;
%let var = Hello;
%put The value of var is &var;
%mend log_macro;
%log_macro;
24. How do you handle errors in SAS macros?
Error handling in SAS macros can be managed by checking the status of macro variables, using %IF-%THEN
logic, and writing appropriate messages to the SAS log using %PUT
.
%macro error_handling(flag);
%if &flag ne 1 %then %do;
%put ERROR: The flag is not set to 1;
%end;
%mend error_handling;
%error_handling(0);
25. How do you use the %SYSGET
function?
%SYSGET
retrieves the value of an operating system environment variable and assigns it to a macro variable.
%let home_dir = %sysget(HOME);
%put The home directory is &home_dir;
26. How do you use the %SUPERQ
function in SAS macros?
%SUPERQ
is used to prevent the resolution of macro variables, especially when dealing with special characters or unresolved macro variables.
%let var = %nrstr(&var);
%put %superq(var);
27. How do you use the %BQUOTE
function?
%BQUOTE
masks special characters or mnemonics in the macro variable without fully resolving the macro expression.
%let text = This is a test %bquote(%str(%)macro);
%put &text;
28. How do you ensure that a macro variable is resolved only once in a loop?
To ensure that a macro variable is resolved only once in a loop, you can store the resolved value in another macro variable before entering the loop.
%macro single_resolution;
%let initial_value = %sysfunc(today(), date9.);
%do i = 1 %to 5;
%put Iteration &i: Date is &initial_value;
%end;
%mend single_resolution;
%single_resolution;
29. How do you include comments within a macro?
Comments within a macro can be included using either macro comments (%*
) or regular SAS comments (*;
or /* */
).
%macro commented_macro;
%* This is a macro comment;
/* This is a standard comment */
data _null_;
x = 1; * Another comment;
run;
%mend commented_macro;
%commented_macro;
30. How do you dynamically create multiple datasets using a macro?
You can dynamically create multiple datasets using a macro by looping through a set of values and using them in the DATA step.
%macro create_datasets;
%do i = 1 %to 3;
data dataset&i;
x = &i;
run;
%end;
%mend create_datasets;
%create_datasets;
31. How do you pass parameters to a macro that need to be processed further?
You can pass parameters to a macro and process them using macro functions or conditional logic.
%macro process_params(test1, test2);
%let combined = %upcase(&test1)_%lowcase(&test2);
%put &combined;
%mend process_params;
%process_params(HELLO, WORLD);
32. How do you store the result of a macro execution in a dataset?
You can store the result of a macro execution in a dataset by using a DATA step and assigning the macro result to a dataset variable.
%macro store_result;
data results;
result = "&sysdate9.";
output;
run;
%mend store_result;
%store_result;
33. How do you execute a macro conditionally based on the existence of a dataset?
You can conditionally execute a macro based on the existence of a dataset using the EXIST
function in a macro.
%macro check_dataset(dsname);
%if %sysfunc(exist(&dsname)) %then %do;
%put Dataset &dsname exists.;
%end;
%else %do;
%put Dataset &dsname does not exist.;
%end;
%mend check_dataset;
%check_dataset(sashelp.class);
34. How do you create a macro that generates code for multiple procedures?
You can create a macro that generates code for multiple procedures by including each procedure within the macro and using conditional logic or loops to control their execution.
%macro multi_proc;
proc means data=sashelp.class;
var age height weight;
run;
proc freq data=sashelp.class;
tables sex;
run;
%mend multi_proc;
%multi_proc;
35. How do you reset a macro variable to its original value after modifying it?
You can reset a macro variable to its original value by storing the original value in another macro variable before modifying it, and then reassigning it.
%macro reset_var;
%let original_var = &var;
%let var = NewValue;
%put &var;
%let var = &original_var;
%put &var;
%mend reset_var;
36. How do you create a macro that generates a report based on user input?
You can create a macro that generates a report based on user input by passing the user inputs as parameters to the macro.
%macro generate_report(dataset, var);
proc print data=&dataset;
var &var;
run;
%mend generate_report;
%generate_report(sashelp.class, age height weight);
37. How do you use the %SCAN
function in a macro?
The %SCAN
function extracts words from a string based on the specified delimiter.
%let text = SAS Macro Programming;
%let first_word = %scan(&text, 1);
%let second_word = %scan(&text, 2);
%put First word: &first_word;
%put Second word: &second_word;
38. How do you create a macro that generates a summary report?
You can create a macro that generates a summary report by including a PROC MEANS or PROC SUMMARY step within the macro.
%macro summary_report(dataset);
proc means data=&dataset;
var age height weight;
run;
%mend summary_report;
%summary_report(sashelp.class);
39. How do you use the %UPCASE
function in a macro?
The %UPCASE
function converts a string to uppercase.
%let text = sas;
%put %upcase(&text); /* Outputs: SAS */
40. How do you create a macro that loops through a list of variables?
You can create a macro that loops through a list of variables by using a macro loop with the variable names as a list.
%macro loop_variables;
%let vars = age height weight;
%do i = 1 %to %sysfunc(countw(&vars));
%let var = %scan(&vars, &i);
proc print data=sashelp.class;
var &var;
run;
%end;
%mend loop_variables;
%loop_variables;
41. How do you use the %QUOTE
function in a macro?
The %QUOTE
function masks special characters or mnemonics in the macro variable, similar to %BQUOTE
, but %QUOTE
is used during macro execution.
%let text = This is a test %quote(%str(%)macro);
%put &text;
42. How do you create a macro that runs multiple procedures conditionally?
You can create a macro that runs multiple procedures conditionally by using %IF-%THEN
logic within the macro.
%macro multi_conditional(proc_type);
%if &proc_type = means %then %do;
proc means data=sashelp.class;
var age height weight;
run;
%end;
%else %if &proc_type = freq %then %do;
proc freq data=sashelp.class;
tables sex;
run;
%end;
%mend multi_conditional;
%multi_conditional(means);
43. How do you use the %NRQUOTE
function?
%NRQUOTE
masks special characters or mnemonics in the macro variable and prevents them from being resolved.
%let text = %nrstr(&var);
%put %nrquote(&text);
44. How do you create a macro that exports data to different formats?
You can create a macro that exports data to different formats by including PROC EXPORT steps within the macro, with format options as parameters.
%macro export_data(dataset, format);
proc export data=&dataset
outfile="output_file.&format"
dbms=&format
replace;
run;
%mend export_data;
%export_data(sashelp.class, csv);
45. How do you handle missing values in a macro?
Handling missing values in a macro can be done by checking for missing values using %IF
statements or DATA step logic within the macro.
%macro check_missing(var);
%if &var = . %then %do;
%put WARNING: Missing value detected for &var;
%end;
%mend check_missing;
%check_missing(.);
46. How do you use the %SYMDEL
statement to delete macro variables?
The %SYMDEL
statement is used to delete one or more macro variables.
%let var1 = value1;
%let var2 = value2;
%symdel var1 var2;
%put &var1 &var2; /* Variables are deleted and no value is printed */
47. How do you create a macro that generates a histogram for different variables?
You can create a macro that generates a histogram for different variables by including a PROC SGPLOT step within the macro and looping through the variables.
%macro histogram(dataset, vars);
%do i = 1 %to %sysfunc(countw(&vars));
%let var = %scan(&vars, &i);
proc sgplot data=&dataset;
histogram &var;
run;
%end;
%mend histogram;
%histogram(sashelp.class, age height weight);
48. How do you use the %SUBSTR
function in a macro?
The %SUBSTR
function extracts a substring from a macro variable value.
%let text = SASMacro;
%let sub_text = %substr(&text, 1, 3);
%put &sub_text; /* Outputs: SAS */
49. How do you use the %INDEX
function in a macro?
The %INDEX
function searches for a substring within a string and returns the position of the first occurrence.
%let text = SASMacro;
%let position = %index(&text, Macro);
%put &position; /* Outputs: 4 */
50. How do you create a macro that conditionally stops execution?
You can create a macro that conditionally stops execution using the %ABORT
statement within the macro.
%macro stop_execution(condition);
%if &condition = stop %then %do;
%abort cancel;
%end;
%else %do;
%put Execution continues;
%end;
%mend stop_execution;
%stop_execution(stop);
Hi I must say this is the best effort put on by you.
ReplyDeleteOne more thing i would like to mention is about the question
Tell me about call symput?
The answer 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.
Seems to be partially incorrect as you can use the macro variable created by call symput in the same datastep using resolve or symget macro functions.
Thanks a load for your efforts
thank you for your great effort man......
ReplyDeleteby nagul chennai...
Good Excellent work....
ReplyDeleteSuch a GREAT explanation good work
ReplyDeleteNice effort,Keep posting such info..
ReplyDeleteThanks
Thanks for spending time.
ReplyDeleteIt was good, it helps beginner a lot.
very usefull and understandable for new sas programers.........thank you
ReplyDeletevery useful. Good effort and good job
ReplyDeleteThankyou for creating such a useful information
ReplyDeleteVery Useful....Thank you for the great effort and help.
ReplyDeleteplease give answers for these interview questions
ReplyDeleteWhat is the use of macro variable?
what is baseline?
thankyou so much...it was really very much helpful
ReplyDeleteexcellent, thanks a lot
ReplyDeletethanks a lot this was of great help to me.
ReplyDeleteThanks man it was of great help!
ReplyDeleteThanks very much.it is very useful.
ReplyDeletethax for a valubul sugation
ReplyDeleteall the answers are very clear to understand...
ReplyDeleteThanks.Very simple and informative.
ReplyDeleteawesome..thanks alot !!!
ReplyDeleteI am going on an interview and these were a great set to brush up on...thx
ReplyDeletegrt job
ReplyDeletesipmple to get macros..good one
ReplyDeleteAwesome work ! Thanks a lot
ReplyDeleteThe Q&A is very helpful
ReplyDeletereally good...god bless you!!
ReplyDeletediff b/w %let and %global
ReplyDelete% global macro statement creates a global macro variable if a variable with the same name doesn’t already exist there. You cannot use %global statement to make an existing local variable global.
ReplyDeleteFor ex:
%let new=class;
%macro test;
%let old=team;
%global new; *WRONG STATEMENT;
data &new;
set &old;
if &cond;
run;
%mend test;
%test;
Thanks A lot....
ReplyDelete