SAS(十)DATA步--控制語句
怎樣選擇使用不同的控制語句
- DO:多次執行同樣的代碼塊
- DO while:當while條件爲真時,多次執行同樣的代碼塊
- DO until: 多次執行同樣的代碼塊,直到until條件爲真,代碼塊總會執行一次
- SELECT:知道明確的離散選擇項時使用
- IF:希望從2個或多個可能的事件中完成一個,允許連續選擇項
- 跳轉代碼(go to,link):需要暫停主代碼,執行“標籤”指明的其它代碼
- 中止代碼(return,continue,leave):負責某種形式的中止。
DO語句
- DO語句規定,在DO後面直到出現END語句之前的這些語句作爲一個單元被執行。
- 簡單DO語句,常用在if-then/else語句裏
- 循環DO語句,用下標變量規定重複次數
do i=1 to exit;
do i=2 to 8 by 2;
data a;
input x @@;
do i=1 to 4;/*把i改成其他字符同樣可行*/
y=4*x;
output; /*如果略去output,則只輸出最後一次循環的結果*/
end;
cards;
11 22 33 44 55 66
;
run;
proc print data=a;
run;
部分結果輸出
data iteratel;
input x @@;
exit=10;
do i=1 to exit;
seed=1; /*初始種子值*/
call ranuni(seed,y);
z=x*y;
if z>25 then i=exit;
output;
end;
cards;
5 1000 2500
;
proc print data=iteratel;
run;
DO while語句
- 當條件成立時重複執行Do組裏的語句
Do while (expression);
- 括號裏的表達式在Do組裏的語句被執行前在循環的開頭被計算。如果表達式是真的,Do組被執行
data while;
n=0;
do while (n<4);
file print;
put n=;
n+1;
end;
run;
DO until語句
- 有條件地執行Do組裏的語句,在循環的最後而不是在循環的開頭計算表達式,如果表達式是真的,Do組就不再被執行。
- Do組裏的語句至少執行一次。
data until;
n=0;
do until(n>0);
file print;
put n=;
n+1;
end;
run;
Select語句
- 允許SAS去執行幾個語句或者語句組中的一個。
- 當一個特殊條件爲真時,執行對應when語句,如果所有when條件均不成立,選擇執行otherwise語句,用end結束
data old;
input grade $ @@;
cards;
A B1 B2 B3 C D
;
run;
data new;
set old ;
x=1000;
select(grade);
when('A') salary=x*1.5;
when('B1','B2','B3') salary=x*1.3;
when('C') salary=x*1.1;
otherwise salary=x;
end;
run;
PROC PRINT;
RUN;
If語句
ELSE statement;
2.形如:IF variable
例:if x then y=x;當變量x的值爲0和缺失之外的任意值時表達式均爲真
data;
input x @@;
if x=1 then y=x;
else if 2<=x<=4 then y=x*10;
else if 5<=x<=7 then y=x*100;
else y=x*1000;
cards;
1 2 3 4 5 6 7 8
;
proc print;
run;
data aa;
input x @@ ;
if x then y=x;
cards;
1 0 . 4
;
run;
proc print;
run;
Return語句
- 告訴SAS系統在data步當前這個位置上停止執行語句並在繼續執行之前返回到一個預定的位置(通常是data步開頭)
- 當return語句使得SAS返回到data步開頭時,SAS首先輸出當前觀測到新數據集中。
- 每個data步都有一個隱含的return語句作爲最後一個可執行的語句
data survey;
input x y z;
if x=y then return;/*試試把return換成output*/
x=y+z ;
a=x**2;
cards;
1 1 3
2 3 3
;
proc print;
run;
Go To語句
- 告訴SAS系統立即轉到Go to語句所指示的那個語句,並從那個語句開始執行後面的語句。
形如:GO TO label;
- label規定語句標號來指示Go to的目標,它必須與go to語句在同一個data步
- Return選項規定返回而不執行下面語句
data info;
input x @@;
file print;
if 1<=x<=5 then goto ok;
put x;/*不是屬於IF的執行*/
count+1;/*不是屬於IF的執行*/
*return;
ok:y+x;/*這句一直要執行*/
cards;
7 2 16 5 323 1 11
;
proc print;
run;
Link語句
- 形如link label;
- 立即轉到由label語句指示的位置,並從那裏開始繼續執行語句直到一個return語句被執行。
- 與goto語句的區別是:標籤內的return語句讓SAS立即返回到link語句後面的那個語句並從那裏繼續執行
data;
input type $ wd station $ ;
elev=.;
if type='ALUV' then link calcu;
year=1985;
return;/*返回到data步開頭*/
calcu:
if station='SITE_1' then elev=6650-wd;
if station='SITE_2' then elev=5500-wd;
return;/*返回到link語句的下面*/
cards;
ALUV 523 SITE_1
UPPA 234 SITE_2
ALUY 666 SITE_2
;
proc print;
run;
Continue和leave語句
- Continue語句使得某個do循環中當前這次循環過程停止進行,並繼續進行下一次循環過程。
- Leave語句停止當前整個do組循環或select組的處理過程,並用跟在do組或select組後面的下一個語句繼續執行data步
data zzz;
*drop i;
do i=1 to 5;
input name $ idno status $;
if status='PT' then continue ;
input benefits $10.;
output;
end;
cards;
Jones 9011 PT
Thomas 876 PT
Richards 1002 FT
Eye/Dental
Kelly 85111 PT
Smith 433 FT
HMO
;
PROC PRINT;
RUN;
data week;
input name $ idno start;
bonus=0;
do year=start to 1991;/*from start to year add 50 every year*/
if bonus ge 500 then leave;
bonus+50;/*如果在do循環裏是類似累加,循環進行到exit+1*/
end;
cards;
Jones 9011 1990
Thomas 876 1976
Barnes 7899 1991
Harrell 1250 1975
Richards 1002 1990
Kelly 85 1981
Stone 091 1990
;
PROC PRINT;
RUN;