SAS macro variables
1. enable you to substitute text in your SAS programs(替代作用,和c++的 #define 差不多)
2. When you reference a macro variable in a SAS program, SAS replaces the reference with the text value that has been assigned to that macro variable. By substituting text into programs, SAS macro variables make your programs more reusable and dynamic
3. 判斷宏變量定義的結束是以分號為分隔符。
為什么要使用宏變量?
因為,宏能起到一處替換,多處替換的效果!
宏變量儲存在哪?
The value of a macro variable is stored in a symbol table。
The values of automatic macro variables are always stored in the global symbol table(意味着你總可以引用到這些宏)
The values of user-defined macro variables can reside either in a macro symbol table local or in the global symbol table.(自定義的宏變量大部分也儲存到全局符號表中)
宏變量的使用范圍有多大?
除了datalines步,其他任何地方都可以使用!
如何刪除宏?
%symdel mvariable;
1:宏變量
如何使用宏變量?
使用引號符號(&)對宏進行引用,當sas程序遇到&時,會在symbol table中搜尋同名變量,進而得出變量中的值。如果要在字符串中進行宏的引用,需要用雙引號,SAS不會對單引號中宏的引用進行任何處理。
引用宏時,對宏變量的大小寫無要求,就是&sex,&Sex得出的結果是一樣
title "June Totals as of &sysdate"; %let sex='男'; data juntot; set sashelp.class; if sex = &sex; run;
1.1:Automatic Macro Variables (用來創建腳注十分好)
Automatic macro variables contain information about your computing environment, such as the date and time of the session, and the version of SAS you are running.
性質:sas系統啟動時創建,全局的,通常由sas賦值,有時可以由用戶自己賦值
footnote1 "Created &systime &sysday, &sysdate9"; footnote2 "on the &sysscp system using Release &sysver";
1.2:User-Defined Macro Variables
最基本形式與相關規定如下圖
宏的一些常用用法
當定義了一個宏后,要記住以下幾點:
1:所有的值都是以character strings的形式儲存(有個地方不是寫的string,不記得是哪,以這個為標准)
2:value的case(大小寫)被保留
3:數學運算符號不被運算(如下表的第五條,那么&sum*&sum就等於4+3*4+3=19)
4:SAS 會將宏對應的值在放到macro symbol時自動移除leading and trailing blanks,除非你用一些特殊的宏函數進行處理。
5:單引號也被包含在宏值內 (比如下圖行2)
6:variable的命名好像沒有任何限制,因為連proc 和run都可以用、、、、暫時沒找到限制
雖然說是以character strings的形式儲存,但是這並不就是從字面上以為着就是字符串,我自己寫了個ex,log如下
177 %let xx="xx"; 178 %let yy=yyy; 179 data _null_; 180 k0 = (&xx=xx); 181 k1 = (&xx="xx"); 182 k2 = (&yy=yyy); 183 put k0= k1= k2=; 184 run; NOTE: 變量 xx 未初始化。 NOTE: 變量 yyy 未初始化。 k0=0 k1=1 k2=1
宏處理器、Word Scanner、Input Stack 運行帶宏的程序的具體流程
當submit program時,程序會進入input stack,對於所有模塊都成立
當程序進入后會做以下五件事
reads the text in the input stack (left-to-right, top-to-bottom)
routes text to the appropriate compiler upon demand
suspends this activity when a step boundary such as a RUN statement is reached
executes the compiled code if there are no compilation errors
repeats this process for any subsequent steps.
總的來說就是,將代碼按從左到右從上到下的順序讀入緩存棧,然后將對應的代碼分配給對應的編譯器進行編譯,如果遇到了run語句就執行。
執行完后如果后面還有語句就繼續按上面的步驟重復執行!
在input stack和compiler之間,sas程序會被一個叫word scanner的東西分解成小的token
Tokens are passed on demand to the compiler.
The compiler requests tokens until it receives a semicolon.(編譯器在遇到分號時會請求上次讀入的全部tokens)
The compiler performs a syntax check on the statement.(然后編譯器在語句上做語法檢查)
某些token是macro trigger(like & %)會通知Word scanner接下來相關的code要被移交到macro processor去處理
宏處理器(macro processor)會進行如下工作
examines these tokens
requests additional tokens as necessary
performs the action indicated.
對於宏變量,宏處理器會做以下幾件事
creates a macro variable in the symbol table and assigns a value to the variable
changes the value of an existing macro variable in the symbol table
looks up an existing macro variable in the symbol table and returns the variable's value to the input stack in place of the original reference.
圖例 448 - 457
1.3:輸出宏變量的值的幾種方式
1.3.1:系統選項--->>options SYMBOLGEN;
在log中顯示每個宏被調用時所使用的值,包括系統宏變量和用戶自定義宏變量
它的關鍵字是解析為或者resolve to
SYMBOLGEN: 宏變量 X 解析為 3
1.3.2:%put語句
%put <text | _ALL_ | _AUTOMATIC_ | _GLOBAL_ | _LOCAL_ | _USER_ | ERROR:... | WARNING:...| NOTE:... >;
1. The text option includes both literal text and references to macro variables.
2. The next five options are keywords that list different classifications of macro variables.
3. The last three options are keywords that can simulate color-coded SAS messages. The ‘…’ represents the text that you want the %PUT statement to display following the keyword.
5:宏與text結合
宏跟在text后面,形成新的text
%let year=02;
%let month=jan;
proc chart data=sasuser.y&year&month;編譯前 proc chart data=sasuser.y02jan;編譯后
%let year=02; %let month=jan; proc chart data=sasuser.y&year&month; hbar week / sumvar=sale; run; proc plot data=sasuser.y&year&month; plot sale*day; run;
宏在text之前,在text與宏之間加特殊的符號,以便能被分解為token
%let var=sale; plot &var*day;
對於一般的前連接
%let graphic=g; proc &graphicplot ... 想表達的意思是proc gplot,但是c p之間無分隔符,不能被分解為token,所以要想表達完整的意思需使用 proc &graphic.plot ->>>proc gplot
%let graphics=g; %let year=02; %let month=jan; %let var=sale; proc &graphics.chart data=sasuser.y&year&month; hbar week / sumvar=&var; run; proc &graphics.plot data=sasuser.y&year&month; plot &var*day; run;