2014年7月10日 星期四

常用的資料整理程式

/*依 ID,Merge 兩個資料檔 ,再依某變項 (cmdecod),保留唯一的值*/

/*方法一*/
DATA t ; merge cm(in=a )  treat ; by subjid  ;  if a ;
run;
proc sort data=T    nodupkey out=t3;
by subjid cmdecod;
run;

/*方法二*/
DATA t ; merge cm(in=a )  treat ; by subjid  ;  if a ;
run;
data t2  ; set t ; by subjid cmdecod;
if first.cmdecod;
run;

/*方法三*/
proc sql
   noprint;
   create table cmtosum as
      select unique (c.cmdecod) as cmdecod, c.subjid, t.trtcd
         from cm as c, treat as t
         where c.subjid = t.subjid
         order by subjid, cmdecod;
quit;


/*依組別(藥名),把資料檔由直向拉成橫向*/

/*方法一*/
Proc sort    data = counts;       by cmdecod trtcd; run;
Data cc2 ; set counts; by cmdecod trtcd;
/*記住3個變項值,不然數據長成一斜線*/
Retain n1-n3;
/*新組別時,先清空數據*/
Array nn {3} n1-n3;
   if first.cmdecod then do i =1 to 3;
nn{i}=.;
   end;
/*指定數值,亦可用Array的方式 nn{trtcd} ,但本例 trtcd有missing,固改用 if then 的方式*/
  if trtcd=. then n1=frequency;
  if trtcd=0 then n2=frequency;
  if trtcd=1 then n3=frequency;
/*輸出每組的最後一筆*/
if last.cmdecod;
run;

/*方法二*/
Proc sort    data = counts;  by cmdecod trtcd; run;
/*利用 Merge ,分組別進行*/
Data cm;
   merge counts(where = (trtcd = 1) rename = (frequency = count1))
         counts(where = (trtcd = 0) rename = (frequency = count2))
         counts(where = (trtcd = .) rename = (frequency = count3))
         end = eof;
      by cmdecod;
run;

/*方法三*/
/*較不建議,若是 ID 的變項值為 missing ,資料會被刪除*/
Proc transpose data=counts prefix=nn out=cc ;
by cmdecod ; id trtcd;
var Frequency;
run;




部分程式引用,Ref:  Jack Shostak, SAS Programming in the Pharmaceutical Industry.

2014年7月9日 星期三

常用的Macro

/*資料筆數存成 Macro Variable*/
/*方法一*/
Proc Sql noprint;
/*Macro Variable N1 */
select Count ( Distinct subjid ) format=3.
into :N1
      from treat
      where trtcd = 1;

 /*Macro Variable N2 */
select count(distinct subjid) format = 3.
      into :n2
      from treat
      where trtcd = 0;
Quit;
%put &N1 &N2;

/*方法二*/
data _null_;
   set treat end = eof;
   **** 分組的 COUNTER 並 retain 住;
   if trtcd = 1 then
      n1 + 1;
   else if trtcd = 0 then
      n2 + 1;
   **** 總 COUNTER.;
   n3 + 1;
   **** 資料最後一列即為所要的數值;
   if eof then
      do;  
         call symput("n1", put(n1,3.)); *用 PUT 數值轉為文字;
         call symput("n2", put(n2,3.));
         call symput("n3", put(n3,3.));
      end;
run;


/*一組變項定義為 Macro Variables*/
/*方法一 用 Arrary*/
DATA A;
Array Vars {8} $18.     (
'Dis_hypertension'
'Dis_DM'
'Dis_hyperlipidemia'
'Dis_stroke'
'dis_asthma'
'Dis_Kidney'
'Dis_HD'
'Dis_osteoporosis'
)  ;
Do i =1 to 8;
Call symput (  'I_num'  , compress(i)  );
Call symput (  'Vv'||compress(i) , Vars{i}  );
End;
run;
%Put   &I_num;
%Put &Vv1 &Vv2  &Vv8 ;

/*方法二 用資料檔處理的方式*/
%LET Text=  那一整群變項;
/*以變項群建立新資料檔(橫向)*/
DATA M_LIST ; input &text;   run;
/*讀 sashelp.Vcolumn取得資料檔訊息(直向)*/
Proc Sql;
Create Table M_LIST2  AS Select Name as MVAR_LIST   From sashelp.Vcolumn
Where libname='WORK' and memname='M_LIST' ;
Quit;
/*以及變項群數目*/
Proc Sql noprint;
Select nvar into :Num  From Sashelp.Vtable
Having libname='WORK' and memname='M_LIST' ;
Quit;
/*巨集變項名稱 Var1,  Var2, ~ VarN */
DATA M_Vars  (Keep= MVAR_PRE);
Pre='Var' ;
do i=1 to &Num;   MVAR_PRE=Pre||compress(i);  output;  End;
run;
/*Macro name 與 變項群 MERGE,Symput為 Macro Variable*/
DATA _NULL_  ; Merge M_vars M_list2 ;   Call Symput ( MVAR_PRE, MVAR_LIST);  run;

2014年6月30日 星期一

十大死因排名、死亡人數

Chronic diseases dominate death stats: MHW

Thursday, June 26, 2014
By Joy Lee, The China Post 

TAIPEI, Taiwan -- The Ministry of Health and Welfare (MHW) yesterday announced the latest cause of death statistics, and chronic diseases take up seven spots on the top-10 list for 2013.
According to the MHW, since the aging of the population in Taiwan has intensified, more people than ever before are dying from chronic diseases.

The MHW said that out of every 100 people, 64 die from chronic diseases like diabetes or hypertension.

According to the MHW, the average time between deaths, the annual number of people who died from cancer and the difference between the average lifespan of both sexes all reached record highs in 2013.

The total number of deaths recorded last year was 154,374, translating into one death every 3 minutes and 24 seconds; the average death rate in 2012 was one per every 3 minutes and 25 seconds.

The MHW said that cancer has been the number one cause of death for 32 years straight, and a total of 44,791 died from cancers, which suggests that one cancer patient passed away every 11 minutes and 44 seconds.

The 10 leading causes of death in 2013 were cancer, heart disease, cerebrovascular disease, diabetes, pneumonia, accidents, lower respiratory illness, hypertensive disease, chronic liver disease and nephrotic syndrome and nephritis.

Compared to the 10 leading causes of death 10 years ago, the MHW said, the rankings for heart disease, pneumonia and hypertensive disease soared dramatically while the rankings for nephrotic syndrome and nephritis and chronic liver disease dropped as a result of new advanced medical treatments for those diseases.

To further analyze common causes of death, the MHW said that the main cause of death for children under the age of 14 is cancer; the main cause for people between the ages of 15 and 24 is accident; and for people over the age of 25, the main cause of death is cancer.

In general, according to the MHW, about 64.2 percent of people who died last year died from the seven chronic diseases that are included in the 10 leading causes of death.

Wu Chien-yuan, division chief of the Bureau of National Health Promotion's Cancer Control and Prevention Division, said that nearly 70 percent of people who passed away last year were people over 65 years old.


Wu said that compared to young people, elders have a higher likelihood of suffering from cancers, which means that as people age, their risk of cancer increases.
====================================================================

年齡說法
people under the age of 14
people between the age of 15 to 24
people over the age of 25
people over 25 years old

14-year-old individual

職稱講法
Prof. Peter Wang, Deputy Director, National Taiwan University
Peter Wang, division chief of the Bureau of Health Promotion's Cancer Control and Prevention Division

數字說法
23,569,741,168  
twenty-three billion, five hundred and sixty-nine million, seven hundred and forty-one thousand, one hundred and sixty-eight

154,374
one hundred and fifty-four thousand, three hundred and seventy-four

220,000,000
Two hundred and twenty million