Advanced Topic: Macro Processing Flashcards

1
Q

Why do we use macro programming?

A

It allows us to make code more efficient. We can assing a name to a block of text, then just call it with the macro name, change code easily and make it flexible, reuse code over and over again.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Define macro processor.

A

Translates macro statements into standard SAS statements

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Define a macro (program).

A

A sequence of DATA and PROC steps and macro-specific statements.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Write the general syntax for macro varaibles.

A

%LET macro-name = value

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What are the rules for macro variable names?

A

1) can be up to 32 characters
2) Must start with a letter or underscore and contain only letters, underscores, or numbers

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the rules for macro variable values?

A
  • Can be a variable name, a numeral, or text
  • Can be over 65,000 characters long
  • Does NOT require quotation marks even when it contains characters
  • blanks at the beginning and end are trimmed
  • Everything between the equal sign and the semicolon become part of the value for that variable
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the difference between global and local macros?

A

Global Macros: created outside of another macro. Can be used anywhere in the program.
Local Macros: created inside of macros and can only be used inside of the macro created

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you reference a macro?

A

Names must be preceded by the amperstand prefix (&) and followed by a period (.)
Ex. &flowertype.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you resolve a macro?

resolve = replace it with a specific value

A

Enclose in DOUBLE quotation marks
Ex.
%LET country = US
countryname = “&Country.”; resolved to countryname = US;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Explain the lines in the following code.

1) %LET flowertype = Ginger;
2) proc print data=flowersales;
3) where variety = “&flowertype.”;
format saleamount dollar7.0;
4) title “Sales of &flowertype.”;
run;

A

1) defines macro flowertype as “Ginger”
2) calls data from flowersales
3) Displays only from selected flowertype of “Ginger”
4) Displays title “Sales of Ginger”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you delete a macro?

A

%SYMDEL macro-name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

List and describe 3 automatic macro variables.

Hint: All deal with date.

A

1) SYSDATE: the date of the SAS invocation (DATE7.0)
2) SYSDAY: the day of the week of the SAS invocation
3) SYSTIE: the time of the SAS invocation

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write the general syntax for creating modular code.

A

%MACRO macro-name;
macro-text; (aka SAS statements)
%MEND macro-name;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do you invoke a macro moduler program?

A

%macro-name

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain the lines in the following code.

1) %MACRO Select (Customer=, SortVar=);
proc sort data=flowersales out = salesout;
2) by &SortVar.;
3) where customerid = “&customer.”;
run;
4) %MEND Select;
5) %Select (Customer = 356W, SortVar = SalesQuantity);

A

1) Creates macro Select with the parameters customer and sortvar
2) Sorts by value in sortvar
3) Selects customer in value of customerid
4) Tells SAS macro ends here
5) Runs macro with the parameters of customer = 356W and sortvar = salesquantity variable.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Write the general syntax for conditional logic in macros.

A

%MACRO macro-name;
%IF condition %THEN %DO;
code-to-do;
%ELSE %IF condition %THEN %DO;
code-to-do;
%END;

17
Q

T/F: the %IF statement can contain actions that standard IF statements cannot. Such as complete DATA or PROC steps.

A

True

18
Q

Write the general syntax for iterative do loops in macros.

A

%DO macro-name = start-value %TO stop-value;
macro-text;
%END;

19
Q

T/F: The iterative %DO statement can be used in open SAS code.

A

False: Can only be used inside of macros.

20
Q

T/F: When the macro variable name is defined in the %DO statement, it does NOT start with an amperstand (&).

A

True, but do need to use it elsewhere outside of the %DO statement.

21
Q

What does CALL SYMPUTX do?

A

Takes a value from a data step and assigns it to a macro variable that can then be used later.

22
Q

Write the general syntax for CALL SYMPUTX.

A

CALL SYMPUTX (“macro-name”, value);

23
Q

List some macro functions that manipulate character strings

A
  • %UPCASE
  • %SUBSTR
  • %INDEX
  • %SCAN
24
Q

What does the %EVAL function do?

A

It evaluates arithmetic and logical expressions using integar arthmetic

25
Q

T/F: You can use non integers in the eval function.

A

False: only integers. Non-integers are truncated.
Ex. 8.5 => 8

26
Q

What does SYSFUNC do?

A

Executes SAS functions or user-defined functions in the macro facility

27
Q

What do the system options MERROR, SERROR, MLOGIC, MPRINT,and SYMBOLGEN do?

A

MERROR: issues a warning if you invoke a macro it cannot find
SERROR: issues a warning if you use a macro varaible it cannot find
MLOGIC: prints in log details about the execution of macro
MPRINT: prints in log the stadard SAS code generate by macros
SYMBOLGEN: prints in log the values of macro variables.

28
Q

T/F: By default, MERROR and SERROR are on and the others (MLOGIC, MPRINT, and SYMBOLGEN) are off.

A

True