Advanced Topic: Macro Processing Flashcards
Why do we use macro programming?
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.
Define macro processor.
Translates macro statements into standard SAS statements
Define a macro (program).
A sequence of DATA and PROC steps and macro-specific statements.
Write the general syntax for macro varaibles.
%LET macro-name = value
What are the rules for macro variable names?
1) can be up to 32 characters
2) Must start with a letter or underscore and contain only letters, underscores, or numbers
What are the rules for macro variable values?
- 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
What is the difference between global and local macros?
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 do you reference a macro?
Names must be preceded by the amperstand prefix (&) and followed by a period (.)
Ex. &flowertype.
How do you resolve a macro?
resolve = replace it with a specific value
Enclose in DOUBLE quotation marks
Ex.
%LET country = US
countryname = “&Country.”; resolved to countryname = US;
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;
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 do you delete a macro?
%SYMDEL macro-name
List and describe 3 automatic macro variables.
Hint: All deal with date.
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
Write the general syntax for creating modular code.
%MACRO macro-name;
macro-text; (aka SAS statements)
%MEND macro-name;
How do you invoke a macro moduler program?
%macro-name
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);
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.