2014年11月27日 星期四
健保資料庫-抓某一疾病一年內發生幾次
以下Macro可以協助解決這個問題,
**************************Beginning***********************************;
%macro Dz_times(data_in,dz_date_name,occur1,occur2,data_out);
data b1;set &data_in;
by
id;
dz_date2=input(&dz_date_name,yymmdd8.);
one_yr_&occur1.times=ifn(
first.ID , (.) , dif&occur2.(dz_date2)
);
if
30<one_yr_&occur1.times<=365 then day365_&occur1.times=1;
run;
data day365_&occur1.times; set b1;if
day365_&occur1.times=1;run;
data &data_out; set
day365_&occur1.times; by id; if first.id ;run;
*有 ? 人在一年內發生occur1
次疾病;
%mend;
%Dz_times(data_in=DM_op_all,dz_date_name=dz_date,occur1=4,occur2=3,data_out=b2);
*變項解釋;
*data_in:放入的資料之名稱;
*dz_date_name:疾病發生日期的變數之名稱;
*occur1:一年內發生多少次疾病;
*occur2:請填入occur1-1的數字 (可以再想如何改寫語法);
*data_out:輸出的資料之名稱;
******************************End**********************************;
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 = 1; else 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 = 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;
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;
訂閱:
文章 (Atom)