Continue with the SAS macro function (I), introduce some common SAS macro functions, macro variables, and their macros Program Statement.
Macro variable
Unique value
Store strings.
Is a global variable.
It can be referenced in a data step, but cannot be defined in a value assignment statement in a data step.
Add"&.
The reference result is that the variable value replaces the variable name.
Macro variables are not interpreted between single quotes when being referenced, but only between double quotes.
Observe the macro variable value:
% Put &Macro variable name; the result is inLogWindow.
Macro variable type
Automatic macro variable
InSASThe process is automatically created by the system when it starts or when the program is running.
InSASAlways valid before exiting
Is a global macro variable.SASReferenced anywhere
View All automatic macro variable statements:% Put _ automatic _;
Custom macro variables
By% LetStatement definition:% LetVariable name=Value;
BySymputFunction generation. Call method:Call symput ("Macro variable name",Variable);Creates a macro variable and assigns a value to the macro variable with the content of the second parameter. Example:
Call
Symput ( "Teddy" , RO );
% Put
& Teddy;
The value can be null.
A string composed of numbers has no numerical meaning and is saved as a string.
The mathematical expression is not assigned a value.
The leading and trailing spaces of the string are deleted.
If a macro variable is referenced in the value string, it is interpreted first and then assigned a value.
View All custom macro variables:% Put _ User _;
View All macro variables (automatic+Custom)
% Put _ all _;
Macro Functions
% Str
When you assign a value to a macro variable, use this function to expand the value, for example:% Str(Value), the program regards the value in the brackets as the macro variable value, for example:% Let
Print = % STR (print data = sashelp. Class; run ;);
% Eval
When you assign a value to a macro variable, use this function to expand the value, for example:% Eval(Value). If the value contains an expression, the value of the expression is calculated first, and then the result is assigned to the macro variable.
% Sysfunc
When assigning values to macro variables, to use functions in the Data step (for example:Substr, scan, trim, leftSome data step functions cannot apply this macro function, such:Put, input, lag, dif) To process the value, you need to use this macro function to expand the value. For example:% Sysfunc (Data step function name (independent variable)<, Output format>). Example:% Let currdate =
% Sysfunc (today (), worddate .);
Note: When nested data step functions are used in Macro or macro statements, they must be used out of each data step function.% Sysfunc.
Macro variables referenced in strings
Use macro variables and strings directly"."Separated. Example:% Let DRC = V; proc
Gchart DATA = DST. Project; & DRC. Bar region; run; (vbar region ;)
Generate macro variables in data step
Call symput (Independent Variable1, Independent variable2);
FunctionSymget
In the data step, expressions are dynamically generated (calledSymputFunction.
Macro Program
% MacroMacro name;
Macro text
% Mend <Macro name>;
Note: The macro text can only be normal text,SASStatement and program step, macro variable, Macro function or macro statement, the combination of the above content.
Call time:%Macro name (plus points later)
Macro with Parameters
Example:% Macro
Printclass (class );
Proc print data = & Class;
Run;
% Mend;
Call:% Printclass (sashelp. Class)
You can assign default values to parameters during definition:% Macro
Printclass (class = sashelp. Class );
Call time:% Printclass ()