標籤:des style blog http io ar color os 使用
宏類似於c中的函數,傳入指定參數後執行,並且宏內部可以包含data步程式和條件運算子號。
宏變數只是小小的變數、、、、(by the way作用也很大)
1:宏的基本文法
如何建立一個簡單的宏並使用?
%macro prtlast;proc print data=&syslast (obs=5);title "Listing of &syslast data set";run;%mend;%prtlast /*不要加分號,加了有可能出錯*/
宏建立過程中做了什麼工作?
1:提交程式後會傳遞給word scanner,掃描分解為tokens,然後再傳給宏處理器
2:檢查所有宏文法,其他的不是宏的部分在執行時再檢查(比如上面的程式,即使proc寫成pro c在宏建立時也不會報錯,他只會檢查&%這些地方或者條件陳述式中的%do等部分)
3:如果有錯誤,則建立dummy (non-executable) macro
4:如果無錯誤則會儲存在work.sasmacr目錄下,並命名為macro-name.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
如何查看宏是否建立成功?(調試宏選項)
mcompilenote=none/noautocall/all
none不會在日誌中輸出資訊
noautocall會在日誌中輸出非autocall macros的所有宏的資訊
all輸出所有資訊
1 options mcompilenote=all;2 %macro mymacro;3 %mend mymacro;NOTE: The macro MYMACRO completed compilation without errors.
宏是如何被啟動並執行?
1. searches the designated SAS catalog (Work.Sasmacr by default) for an entry named Macro-name.Macro.
2. executes compiled macro language statements within Macro-name.(執行被編譯好的宏的部分)
3. sends any remaining text in Macro-name to the input stack for word scanning.(執行其餘的文本部分,比如上面的print過程)
4. suspends macro execution when the SAS compiler receives a global SAS statement or when it encounters a SAS step boundary.
5. resumes execution of macro language statements after the SAS code executes.
宏運行過程可以和兩部分打交道,第一部分為符號表,第二部分為input stack
2:調試宏
2.1:當調用宏時,宏中不是宏語言的部分不會在log中顯示,可以用如下選項將其顯示
options mprint/nomprint
the text that is sent to the SAS compiler as a result of macro execution is printed in the SAS log
23 options mprint;24 %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 log about the parameter values passed into this macro when invoked
看看啥叫feedback!!!注意後三行
MLOGIC(MAKEPGM): 準備開始執行。MLOGIC(MAKEPGM): 參數 NEWNAME 的值為 res MLOGIC(MAKEPGM): 參數 SETNAME 的值為 sashelp.classMLOGIC(MAKEPGM): 參數 PRINT 的值為 YES
/*紅色字型為作用效果*/
options nomprint mlogic;107 %prtlastMLOGIC(PRTLAST): Beginning execution.NOTE: There were 1 observations read from the datasetWORK.SALES.NOTE: PROCEDURE PRINT used:real time 0.02 secondscpu time 0.02 secondsMLOGIC(PRTLAST): Ending execution.
2.3:log中列印內部宏的步驟,列印內部宏的邏輯
OPTIONS MPRINTNEST | NOMPRINTNEST;
OPTIONS MLOGICNEST | NOMLOGICNEST;
2.4:SOURCE2 writes source statements that are inserted by %INCLUDE statements
3:建立帶參數的宏
3.1:Macros That Include Positional Parameters
3.2:Macros That Include Keyword Parameters
Keyword parameters can be listed in any order. Whatever value you assign to each parameter (or variable) in the %MACRO statement becomes its default value. Null values are allowed.
When you call a macro whose definition includes keyword parameters, you specify both the keyword and the value for each parameter, in any order. If you omit a keyword parameter from the macro call, the keyword variable retains its default value,引用時一定要列出keyword和value,如果不列出則表示使用預設值
關鍵字參數可以擺放在任意位置,賦值的話即為預設值,預設值可以為null
其實這裡的形式和c++中函數的預設參數十分類似,賦值為預設的話調用時省略不寫即為調用預設
使用方式
%macro-name(keyword-1=value-1<,...,keyword-n=value-n>)
將course_code course_title days賦值給vars
%macro printdsn(dsn=sasuser.courses,vars=course_code course_title days);proc print data=&dsn;var &vars;title "Listing of %upcase(&dsn) data set";run;%mend;%printdsn() /*調用參數為預設參數的情況*/
3.3:Macros That Include Mixed Parameter Lists
All positional parameter variables in the %MACRO statement must be listed before any keyword parameter variable is listed
所有位置參數要列在關鍵字參數前(調用時原則也是這樣)
4:理解符號表
全域符號表的生存周期
The global symbol table is created during the initialization of a SAS session and is deleted at the end of the session
?? are available anytime during the session
?? can be created by a user
?? have values that can be changed during the session (except for some automatic macro variables).
建立全域宏變數的幾種方式
?? a %LET statement (used outside a macro definition)
?? a DATA step that contains a SYMPUT routine
?? a SELECT statement that contains an INTO clause in PROC SQL
?? a %GLOBAL statement.
The %GLOBAL statement
能定義多個全域宏,可以再宏定義中也可以在宏定義外,對已經產生了的宏無用
局部宏的生存周期
A local symbol table is created when a macro that includes a parameter list is called or when a request is made to create a local variable during macro execution. The local symbol table is deleted when the macro finishes execution
一句話:the local symbol table exists only while the macro executes.
局部宏的建立方式
?? 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 INTO clause in PROC SQL within a macro definition
?? a %LOCAL statement.
著重介紹%local
只能在宏定義中出現,能定義多個變數,對已存在的變數無效
%let dsn=sasuser.courses;/*全域宏*/%macro printdsn;%local dsn;/*局部*/%global hehe;/*全域*/%let dsn=sasuser.register;%let hehe = ‘say hehe‘;%put The value of DSN inside Printdsn is &dsn;%mend;%printdsn%put The value of DSN outside Printdsn is &dsn;
全域變數與局部變數的關係
這裡的關係和c++中的範圍類似,當全域和局部中有同名變數時,局部覆蓋全域,越局部,越優先考慮使用局部的變數。
這裡做個例子來描述全域宏與局部宏的關係
宏的更新機制如下:如果在局部出現就先看看局部符號表中有沒有對應變數(有就更新),沒有就看全域(有就更新),沒有就建立一個局部變數
下例中,dsn在全域有,所以更新全域的,但是這裡要注意了,前一個put輸出的是register後一個無輸出,為啥?
因為在宏中更新的全域符號表中的值,當宏執行完後,自動銷毀了,所以第二個put輸出為缺失
如果將注釋去掉,那麼就以為著建立了一個局部變數,這樣輸出的值就各是各的了!
options symbolgen;options nomlogic;%let dsn=sasuser.courses;%macro printdsn;*%local dsn;%let dsn=sasuser.register;%put The value of DSN inside Printdsn is &dsn;%mend;%printdsn%put The value of DSN outside Printdsn is &dsn;
5:條件運算子
這裡就是普通的運算子前面加%,nothing important to say
6:在宏中進行運算
6.1:The %EVAL function(不支援小數運算)
?? 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支援小數運算
%macro figureit(a,b);%let y=%sysevalf(&a+&b);%put The result with SYSEVALF is: &y; /*101.59*/%put BOOLEAN conversion: %sysevalf(&a +&b, boolean); /*1*/%put CEIL conversion: %sysevalf(&a +&b, ceil);/*102*/%put FLOOR conversion: %sysevalf(&a +&b, floor);/*101*/%put INTEGER conversion: %sysevalf(&a +&b, integer);/*101*/%mend figureit;%figureit(100,1.59)
sas宏(3)宏,調試宏,建立帶參數的宏,理解符號表(全域宏與局部宏解析),宏條件運算子,在宏中進行運算