SAS Macros

Discover essential SAS macros concepts in this Q&A guide. Learn how to create and identify macro variables, distinguish %LOCAL vs. %GLOBAL scope, reuse code with %INCLUDE and macros, leverage DATA _NULL_, perform arithmetic on macro variables, call macros inside a data step, understand macro variable length limits, and explore SAS validation tools. Perfect for SAS programmers looking to sharpen their macro skills.

SAS Macros: 10 Key Q&As for Programmers

Describe the way in which one can create a macro variable in SAS?

There are 5 ways to create macro variables in SAS:

  • %LET statement – defines a macro variable explicitly:
    %let var = value;
  • CALL SYMPUT / CALL SYMPUTX – creates a macro variable from a data step:
    call symput(‘var’, value);
  • INTO clause in PROC SQL – stores query results into macro variables:
    select count(*) into :var from dataset;
  • Macro parameters – defined in a macro definition:
    %macro mymacro(var=);
  • Iterative %Do Statement
sas macros

How would you identify a macro variable in SAS?

A macro variable is identified in SAS code by an ampersand (&) preceding its name, e.g., &var. To view existing macro variables and their values, one can use:

  • %PUT _USER_; – lists all user-defined macro variables.
  • %PUT &var; – displays the value of a specific macro variable.

What is the difference between %LOCAL and %GLOBAL in SAS?

%LOCAL creates a macro variable with scope limited to the current macro; it is not accessible outside that macro.
%GLOBAL creates a macro variable accessible anywhere (global scope), even after the macro finishes.

How would you define the end of a macro in SAS?

The end of a macro in SAS is defined using the %MEND statement. Optionally, you can include the macro name after %MEND for clarity (e.g., %MEND mymacro;).

How would you include common code or reuse code to be processed along with your statements?

One can reuse code in SAS by:

  • Using %INCLUDE to insert an external file containing SAS statements.
  • Defining macros (%MACRO / %MEND) to encapsulate reusable logic, then calling them as needed.

Explain DATA_NULL_

DATA _NULL_; It is a SAS data step that does not create an output dataset. It is used for executing logic without producing data, such as writing to the log, creating macro variables (via CALL SYMPUT), generating custom reports with PUT statements, or performing calculations that only need to be processed in memory.

How do you add a number to a macro variable in SAS?

To add a number to a macro variable, use %EVAL (for integers) or %SYSEVALF (for floating-point) within a %LET statement:

%let var = 5;
%let var = %eval(&var + 3);

For decimal values:

%let var = %sysevalf(&var + 0.5);

How can we call macros within a data step?

One can call a macro within a data step by:

  • Directly invoking the macro (e.g., %my_macro;): it executes during data step compilation, not for each observation.
  • Using CALL EXECUTE: to execute the macro conditionally or for each observation:sascall execute(‘%my_macro’);
  • Using %SYSFUNC: to call a SAS function within a macro without requiring a full macro invocation.

Note: Macro calls placed directly in the data step are resolved before the data step runs, while CALL EXECUTE resolves during data step execution.

What is the maximum length of the macro variable?

The maximum length of a macro variable value in SAS is 65,534 characters. The macro variable name itself can be up to 32 characters long. Note: In very old versions (SAS 6), the maximum length was 32,767 characters. The 65,534 limit applies to SAS System 8 and later.

What validation tools are used in SAS?

The common validation tools in SAS include:

  • PROC COMPARE: compares datasets for differences.
  • PROC FREQ / PROC MEANS: validates data distributions and summary statistics.
  • Data step debugging: using PUT statements and _ERROR_ variable.
  • Macro debugging options: MPRINT, SYMBOLGEN, MLOGIC to trace macro resolution.
  • SAS Code Analyzer (in SAS Enterprise Guide): checks code for errors and best practices.
  • DS2 and FedSQL: offer validation features in SAS Viya.
  • Validation framework: in SAS Viya for automated model validation.

Simulation in R Language

Common Errors in SAS

Learn to fix common SAS programming errors with expert debugging tips. Prevent data step, PROC, and macro errors to improve SAS coding efficiency and performance. SAS programming errors solved: Data step, PROC, macro, and syntax errors explained with examples.

Common Errors in SAS Programming

What are the common errors in SAS?

The common errors that are committed in SAS Programming are described in various categories:

DATA Step Errors

The following are common errors in SAS that occurs in Data Step:

  • Missing semicolon: Most common SAS error
  • Incorrectly referencing variables: (set, merge, array issues)
  • Uninitialized variable warnings: (using variables before assigning values)
  • Incorrect IN= variable usage: in merge operations
  • Data type mismatches: (character/numeric conversion issues)
  • Infinite loops: from improper DO loop conditions

PROC Step Errors

The following are common errors in SAS that occurs in PROC Step:

  • Missing required statements: (like VAR in PROC MEANS or MODEL in PROC REG)
  • Incorrect dataset options: (where=, keep=, drop= placement errors)
  • Misaligned CLASS and VAR statements:
  • Format mismatches: between data and procedures

The following are common errors in SAS Programming that occurs in SAS Macros:

  • Unresolved macro variables: (&variable not defined)
  • Missing %MEND statements
  • Incorrect macro quoting
  • Scope issues (local vs. global macro variables)
  • Macro variable name conflicts with dataset variables

Logical/Algorithmic Errors

The following are logical or algorithmic errors that are committed in SAS Programming.

  • Incorrect BY-group processing (missing SORT or improper BY statement)
  • Merge without proper BY statement causing Cartesian products
  • Misunderstanding of SET statement behavior (automatic retain of variables)
  • WHERE vs IF confusion (compile-time vs execution-time filtering)

Syntax & Format Errors

The following are syntax and Format errors in SAS Programming

  • Unclosed quotes/comments (/* without */)
  • Incorrect RUN/QUIT usage (some PROCs require QUIT)
  • Mismatched parentheses in complex expressions
  • Invalid format names or missing format libraries

Efficiency & Performance Errors

The following are efficiency and performance errors committed in SAS Programming.

  • Unnecessary sorting of large datasets
  • Reading entire dataset when subsetting would suffice
  • Not using KEEP/DROP to limit variables
  • Inefficient WHERE clause construction

Debugging Tips

The following are debugging tips for error detection in SAS Programming.

  • Check log carefully – SAS error messages are often specific
  • Use OPTIONS ERRORS=1; to stop on first error
  • Validate with PROC CONTENTS before complex operations
  • Test macros with MPRINT SYMBOLGEN;
  • Use PUT statements for variable value inspection in DATA step

What are the prevention strategies to prevent errors in SAS?

The following are prevention strategies to commit errors in SAS Programming Language:

  1. Consistent indentation and code structure
  2. Comment complex logic
  3. Test incrementally (build code in small pieces)
  4. Use ODS TRACE to understand procedure output
  5. Implement version control even for SAS code

The SAS log is your best diagnostic tool – always review it completely, not just the error section, as earlier warnings often explain later errors.

What is the error in the example below?

proc factor data=SASHELP.CLASS;
var name ;
run;

Statistical procedures like factor analysis require numeric variables because they perform mathematical operations that are meaningless with character/text data.

In the PROC FACTOR procedure, the variables mentioned in the VAR statement should be numeric in nature. The problem in the above code is

  • SASHELP.CLASS contains both character and numeric variables
  • NAME is a character variable (contains text values like “Alfred”, “Alice”, etc.)
  • PROC FACTOR performs factor analysis, which requires numeric variables only (continuous data for correlation/covariance calculations)

The corrected version of the code above is:

proc factor data=SASHELP.CLASS;
var age height weight;  /* All numeric variables */
run;

What is the error in the example below?

proc mixed data=SASHELP.IRIS plots=all;
model petallength= /;
class species;
run;

The error in this code is on line 2: The MODEL statement has an empty dependent variable specification. The equals sign (=) is followed by nothing before the slash (/). This means:

  • But no independent variables are specified after the = sign
  • The dependent variable is correctly specified as petallength

There must be at least one independent variable specified after the = sign (or the keyword _ALL_ for all numeric variables).

It is important to note that the MODEL statement must have at least something (even if just an implicit intercept) after the equals sign. If you want an intercept-only model, the syntax model y = /; is actually valid in PROC MIXED, so check if there’s another issue like variable name misspelling or missing CLASS statement before MODEL.

Learn R Programming Language

Master Advanced Excel: Formulas, PivotTables, Charts

Ready to move beyond the basics? This comprehensive “Master Advanced Excel Quiz” dives into advanced Excel functions like SUBSTITUTE, UNICHAR, and VALUE, and teaches you how to master PivotTables, create dynamic charts like Gauges, and automate tasks with macros. Transform your data analysis today! Let us start with the Online Master Advanced Excel Quiz with Answers

Online Master Advanced Excel MCQs Test with Answers

Online Multiple Choice Questions about MS Excel

1. When inserting a Calculated Field, you cannot use the VLOOKUP function because

 
 
 

2. To make a map interactive, what could you do?

 
 
 

3. For the PivotTable below, what would happen to the contents of cell D4 if we added a field to the Columns quadrant that resulted in the table expanding by two or more columns?

Master Advanced Excel: Formulas, PivotTables, Charts

 
 
 

4. What would be an appropriate formula using the function SUBSTITUTE to correct the contents of cell B2, which currently is Macquarie|University|Business School, by only replacing the first instance of “|” with ” ” (a space)?

 
 
 

5. What will the following formula =UNICHAR(65) return?

 
 
 
 

6. MS Excel Quiz mcqs 4

What type of fill has been used in the segments of the chart below?

 
 
 

7. If our PivotTable changes, any conditional formatting will be re-applied appropriately based on the new table contents; however, any custom number formats will not be re-applied appropriately based on the new table contents.

 
 
 

8. When coding a macro that has to repeat a procedure, what are the Visual Basic code words?

 
 
 
 

9. The pointer of a Gauge chart is essentially a very thin segment of a

 
 
 

10. When creating a PivotChart

 
 
 
 
 

11. When adding the visualisation of conditional formatting, what is the purpose of the repeated column in the PivotTable below

MS Excel Pivot Table 2

 
 
 

12. To begin the creation of a Gauge chart, we need to begin with a

 
 
 

13. MS Excel Quiz mcqs

What will be the result of the following formula: =VALUE(RIGHT(C3,6))?

 
 
 
 

14. You cannot insert a Calculated Item for grouped fields

 
 

15. What would be an appropriate formula using the function SUBSTITUTE to correct the contents of cell B2, which currently is Macquarie|University|Business School, by replacing all instances of “|” with ” ” (a space)?

 
 
 
 

16. MS Excel Quiz mcqs 3

What is the most likely explanation for the Map chart appearing the way below?

 
 
 

17. You can right-click a shape and select Assign Macro. This will then run the macro when the shape is clicked

 
 

18. MS Excel Quiz mcqs 2

What do the letters in the box plot above represent?

 
 
 
 

19. The worksheet contains the following data:

MS Excel Quiz mcqs

What will be an appropriate formula from the list below to return the value: 700.43 as numeric data?

 
 
 
 

20. Calculated fields will add new columns to your PivotTable, whereas Calculated items will add new rows to your PivotTable

 
 

21. In a PivotChart under PivotChart Fields, Rows changes to Legend (Series) and Columns changes to Axis (Categories)

 
 

22. How can we adjust the chart below so the female bars are lined up with the male bars in the chart?

 
 
 
 

Question 1 of 22

Online Master Advanced Excel Quiz with Answers

  • What would be an appropriate formula using the function SUBSTITUTE to correct the contents of cell B2, which currently is Macquarie|University|Business School, by replacing all instances of “|” with ” ” (a space)?
  • What would be an appropriate formula using the function SUBSTITUTE to correct the contents of cell B2, which currently is Macquarie|University|Business School, by only replacing the first instance of “|” with ” ” (a space)?
  • What will the following formula =UNICHAR(65) return?
  • What will be the result of the following formula: =VALUE(RIGHT(C3,6))?
  • The worksheet contains the following data: What will be an appropriate formula from the list below to return the value: 700.43 as numeric data?
  • What do the letters in the box plot above represent?
  • What is the most likely explanation for the Map chart appearing the way below?
  • To make a map interactive, what could you do?
  • How can we adjust the chart below so the female bars are lined up with the male bars in the chart?
  • When coding a macro that has to repeat a procedure, what are the Visual Basic code words?
  • You can right-click a shape and select Assign Macro. This will then run the macro when the shape is clicked
  • To begin the creation of a Gauge chart, we need to begin with a
  • The pointer of a Gauge chart is essentially a very thin segment of a
  • What type of fill has been used in the segments of the chart below?
  • For the PivotTable below, what would happen to the contents of cell D4 if we added a field to the Columns quadrant that resulted in the table expanding by two or more columns?
  • Calculated fields will add new columns to your PivotTable, whereas Calculated items will add new rows to your PivotTable
  • When inserting a Calculated Field, you cannot use the VLOOKUP function because
  • You cannot insert a Calculated Item for grouped fields
  • When adding the visualisation of conditional formatting, what is the purpose of the repeated column in the PivotTable below
  • If our PivotTable changes, any conditional formatting will be re-applied appropriately based on the new table contents; however, any custom number formats will not be re-applied appropriately based on the new table contents.
  • When creating a PivotChart
  • In a PivotChart under PivotChart Fields, Rows changes to Legend (Series) and Columns changes to Axis (Categories)

R Frequently Asked Questions