2014年4月14日 星期一

SAS函數的介紹 INDEX functions

[INDEX]:找出字串中,某一個字或字串首次出現的位子
Function: INDEX(var,"value")
我們可以藉由他找出字串中是否有我們感興趣的字或是字串。
舉例來說:
found_A_first = index(X, "A"*found_A_first會回應變相X首次出現的位子;

當我們想要找字串中是否有我們感興趣的字或是字串時,
也可以利用index,
舉例來說:
if index(X, "A") > 0 then found_A = 1else  found_A = 0;
*若A有出現在X中,那index(X,"A")的回應值會大於0,因此語法可以這樣下;

若需要找出有A or B字元或是A&B字元可以利用
if found_A+found_B>= 1  then found_A_or_B = 1else  found_A_or_B = 0;
if found_A+found_B=2  then found_A_and_B = 1else  found_A_and_B = 0;




data a;
input X $ ;
cards;
AB
ABC
BCD
BCDE
CD
;
run;

data b;
set a;

found_A_first = index(X, "A");
found_B_first = index(X, "B");
found_AB_first = index(X, "AB");
found_BC_first = index(X, "BC");

if index(X, "A") > 0 then found_A = 1; else  found_A = 0;
if index(X, "B") > 0 then found_B = 1; else  found_B = 0;
if index(X, "AB") > 0 then found_AB = 1; else  found_AB = 0;
if found_A+found_B>= 1  then found_A_or_B = 1; else  found_A_or_B = 0;
if found_A+found_B=2  then found_A_and_B = 1; else  found_A_and_B = 0;
run;

proc print data = b;
run;


沒有留言:

張貼留言