SAS macro (3) macro, debug macro, create a macro with parameters, understand symbol table (global macro and local macro resolution), macro conditional operator, perform operation in macro

Source: Internet
Author: User
Tags vars

A macro resembles a function in C, executes after the specified parameter is passed in, and inside the macro can contain the data step program and the conditional operation symbol.

A macro variable is just a small variable 、、、、 (by the same is a great role)

1: Basic syntax for macros

How do I create a simple macro and use it?

% macro Prtlast; proc Print data=&syslast (obs=5of&Set  "; run; % mend; %prtlast/    * do not add a semicolon, add a possible error * /

What has been done during the macro creation process?

1: After the program is submitted to Word scanner, the scan is decomposed into tokens and then passed to the macro processor

2: Check all macro syntax, the other is not the macro part of the implementation of the check again (such as the above program, even if the proc written Pro C in the macro creation will not error, he will only check &% these places or conditional statements in the%do and other parts)

3: If there is an error, create the dummy (non-executable) macro

4: If no error is saved in the WORK.SASMACR directory, and is named Macro-name.macro

Which keywords cannot be named for a macro?

ABORT ACT ACTIVATE
Bquote by
CLEAR CLOSE CMS Comandr COPY
Deact DEL DELETE DISPLAY dmidsply dmisplit do
EDIT ELSE EVAL
FILE
GLOBAL GO GOTO
IF INC INCLUDE INDEX INFILE INPUT
Keydef
LENGTH Let LIST listm LOCAL
MACRO Metasym
Nrbquote Nrquote Nrstr
On OPEN
PAUSE PUT
Qscan qsubstr Qsysfunc QUOTE qupcase
RESOLVE RETURN RUN
SAVE SCAN STR SUBSTR superq SYSCALL sysevalf sysexec sysfunc sysget sysrput
Then to TSO
Unquote unstr UNTIL UPCASE
While WINDOW

How do I see if a macro was created successfully? (Debug macro option)

mcompilenote=none/noautocall/all

None will not output information in the log

Noautocall will output all macros in the log that are not Autocall macros

All output All information

1 options mcompilenote=all; 2 % macro MyMacro; 3 % mend MyMacro; NOTE:THE macro MyMacro completed compilation without errors.

How is a macro run?

1. Searches the designated SAS catalog (WORK.SASMACR by default) for a entry named Macro-name.macro.
2. Executes compiled macro language statements within Macro-name. (the part that executes the compiled macro)
3. Sends any remaining text on Macro-name to the input stack for Word scanning. (Executes the rest of the text, such as the print process above)
4. Suspends macro execution when the SAS compiler receives a global SAS statement or when it encounters a SAS step Boundar Y.
5. Resumes execution of macro language statements after the SAS code executes.

macro Run process can deal with two parts , the first part of the symbol table, the second part of the input stack

2: Debug macro

2.1: When a macro is called, the part of the macro that is not in the macro language is not displayed in the log and can be displayed with the following options

Options Mprint/nomprint

The text that's sent to the SAS compiler as a result of macro execution are printed in the SAS log

 at    mprint;  -   % Prtlastmprint (prtlast):    proc Print Data=work. SALES (Obs=5); Mprint (prtlast):   run;

2.2:prints messages that indicate macro actions that were taken during macro execution.

OPTIONS Mlogic | Nomlogic;

Provide feedback in the logs about the parameter values passed to this macro when invoked

Look what's called feedback!!! Note After three lines

mlogic (MAKEPGM): Ready to start execution. Mlogic (MAKEPGM): The value of the parameter NEWNAME  is resPRINT is YES

/   * Red font for effect * /
Options Nomprint mlogic;107 %prtlastmlogic (prtlast): Beginning execution. Note:there were1ObservationsRead fromThe DataSet Work. SALES. Note:PROCEDURE PRINTused:RealTime0.02secondscpu Time0.02secondsmlogic (prtlast): ending execution.

To print internal macros in 2.3:log, print the logic of the internal macro

OPTIONS Mprintnest | Nomprintnest;

OPTIONS Mlogicnest | Nomlogicnest;

2.4:source2 writes source statements that is inserted by%INCLUDE statements

3: Create a macro with parameters

3.1:macros that Include positional Parameters

3.2:macros that Include Keyword Parameters

Keyword parameters can be listed on any order. Whatever value assign to each parameter (or variable) in the%macro statement becomes its default value. Null values are allowed.

When your call a macro whose definition includes keyword parameters, you specify both the keyword and the value for EAC H parameter, in any order. If you omit a keyword parameter from the macro call, the keyword variable retains its default value< /c6>, Make sure to list keyword and value when referencing, and if not listed, use the default value

Keyword parameters can be placed anywhere, the value is assigned to the default value, the default value can be null

In fact, the form here and the default parameters of the function in C + + is very similar, the assignment is the default, the call is omitted to write the default

How to use

%Macro-name (keyword-1=value-1<,..., Keyword-n=value-n>)
Assign    course_code course_title days to VARs
%Macro Printdsn (DSN=Sasuser.courses,vars=Course_code course_title days);proc PrintData=&DSN;var &vars;title "Listing of %UpCase&DSN) dataSet"; run;%mend;%PRINTDSN ()/* Call parameter as default parameter */

3.3:macros that Include Mixed Parameter Lists

All positional parameter variables in the%MACRO statement must is listed before any keyword parameter variable i s listed

All positional parameters are listed before the keyword parameter (which is the same as the principle at the time of invocation)

4: Understanding Symbol Table

Lifetime of the global symbol table

The global symbol table is created during the initialization of a SAS session and are deleted at the End of the session

?? is available anytime during the session
?? Can is created by a user
?? Has the values that can is changed during the session (except for some automatic macro variables).

Several ways to create global macro variables

?? A %let statement (used outside a macro definition)
?? A DATA step that contains a symput routine
?? A SELECT statement that contains an to clause in PROC SQL
?? A %globaL statement.

The%global statement

The ability to define multiple global macros, which can be defined in a macro or outside the macro definition, is useless for a macro that has already been generated.

The life cycle of a local macro

A local symbol table is created when a macro, includes a parameter list is called or when a request was made to create A local variable during macro execution. The local symbol table is deleted when the macro finishes execution

sentence: The local symbol table exists only while the macro executes.

How local macros are created

?? Parameters in a macro definition
?? A%let statement within a macro definition
?? A DATA step that contains a symput routine within a macro definition
?? A SELECT statement that contains an to clause in PROC SQL within a macro definition
?? A%local statement.

Emphasis on the introduction of%local

Can only be seen in macro definitions, multiple variables are defined, invalid for existing variables

%let dsn=sasuser.courses;/* Global macro * /%macro Printdsn;%local dsn;/* Local */%global hehe;/* global * /%Let DSN=Sasuser.register;%Let hehe= 'say hehe';%Put the value ofDSN inside Printdsn is &DSN;%mend;%Printdsn%Put the value ofDSN outside PRINTDSN is &dsn

The relationship between global variables and local variables

The relationship here is similar to the scope in C + +, when there is a variable with the same name in both global and local, local coverage is global, the more local, the higher the priority is to use local variables.

Here's an example to describe the relationship between a global macro and a local macro

The update mechanism of the macro is as follows: if the local symbol table is the first to see if there is no response to the strain (there is an update), do not look at the global (with the update), did not create a local variable

In the following example, the DSN is global, so update the global, but note here, the previous put output is the register after a no output, why?

Because the values in the global symbol table that are updated in the macro are automatically destroyed when the macro finishes executing , the second put output is missing

If the comment is removed, then it is assumed that a new local variable is created so that the value of the output is individual!

options Symbolgen;options nomlogic;%Let DSN=sasuser.courses;%macro Printdsn;*%local DSN;%Let DSN=Sasuser.register;%Put the value ofDSN inside Printdsn is &DSN;%mend;%Printdsn%Put the value ofDSN outside PRINTDSN is &dsn

5: Conditional operator

Here is the normal operator front plus%,nothing important to say

6: Perform operations in macros

6.1:the%eval function (fractional arithmetic not supported)

?? translates integer strings and hexadecimal strings to integers.
?? translates tokens representing arithmetic, comparison, and logical operators to macro-level operators.
?? performs arithmetic and logical operations.

The%eval function does not convert the following to numeric values:

?? Numeric strings that contain a period or e-notation
?? SAS Date and time constants.

6.2:%sysevalf function supports decimal operations

%macro Figureit (A, b);%Let Y=%Sysevalf (&A+&b);%Put the result withSysevalf is:&Y/*101.59*/%Put BOOLEAN conversion:%Sysevalf (&A+&b, Boolean);/*1*/%Put Ceil conversion:%Sysevalf (&A+&b, Ceil);/*102*/%Put FloorConversion%Sysevalf (&A+&B Floor);/*101*/%PutINTEGERConversion%Sysevalf (&A+&Binteger);/*101*/%mend Figureit;%Figureit ( -,1.59)

SAS macro (3) macro, debug macro, create a macro with parameters, understand symbol table (global macro and local macro resolution), macro conditional operator, perform operation in macro

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.