2017年10月27日 星期五

分組

%macro my_cn2(data =, y =, x =, c_x =, l_r=, z =);

ods trace on / listing; 
proc contents data =  &data.;
 ods output Variables = contents_;
run;
ods trace off;

data contents_;
 set contents_;
 where upcase(Variable) = "%upcase(&y.)";
 call symputx("type", type);
run;

%if "&type." = "Num" %then %do;
 data temp_data;
  set &data.;
  y_ = put(&y. ,z&z..);
 run;
%end; %else %do;
  data temp_data;
   set &data.;
   y_ = &y.;
  run;
%end;


proc sql;
 create table class_ as
  select  distinct  y_
  from temp_data;
quit;

%let data_class = class_;
%let open_d = %sysfunc (open(&data_class.));
%let row_n = %sysfunc(attrn(&open_d., nobs));
%let close_d = %sysfunc(close(&open_d.));

proc sql noprint;
 select  y_
  into :y1 - :y&row_n.
  from class_;
quit;

data _null_;
 comb_ = comb(&row_n.,2);
 call symputx("comb_", comb_);
run;

proc iml;
 allcomb_ = allcomb(&row_n., 2);
 create allcomb var {allcomb_}; 
 append;       
 close allcomb;
quit;

%let data__ = allcomb;
%let open_d = %sysfunc (open(&data__.));
%let row_nn = %sysfunc(attrn(&open_d., nobs));
%let close_d = %sysfunc(close(&open_d.));

proc sql noprint;
 select  allcomb_
  into :r2n_1 - :r2n_&row_nn.
  from allcomb;
quit;



%do comb_i = 1 %to &row_nn. %by 2;
%let comb_i_ = %sysevalf(&comb_i. +1);
%let comb__ = 1;/*66*/
/*%put &comb_i. &comb_i_.;*/
/*data comb_&comb__.;*/
data _&&&&y&&r2n_&comb_i...vs_&&&&y&&r2n_&comb_i_...;
 set temp_data;
  where y_= "&&&&y&&r2n_&comb_i..." or y_ = "&&&&y&&r2n_&comb_i_...";
run;


 %if "&l_r." = "t" %then %do; 
  proc logistic data = _&&&&y&&r2n_&comb_i...vs_&&&&y&&r2n_&comb_i_... plots=all;
   class &c_x.;
   model y_ = &x. &c_x.;
   output out = o__&&&&y&&r2n_&comb_i...vs_&&&&y&&r2n_&comb_i_... PREDICTED= p PREDPROBS= p2;
  quit;
 %end;
 %let comb__ = %sysevalf(&comb__. +1);/*66*/
%end;

%mend;
%my_cn2(data = mysas.unk, y = final, z=1)


data 資料集
y 類別變數
z 類別變數為數值型態時且類別總類小於10 z=1 小於100 z=2 ....  文字形態時可不輸入


4類別分組後結果

Read More