* %robust_hb(ch10tab11, y, x2, .01, 0.00005, 10) ;
%macro robust_hb(data, dep, predictors, conhub, conlimit, iterations );
data &data;
  set &data;
  _w1_ = 1;
run;
proc reg data=&data; 
  model &dep = &predictors;
  output out=_tempout_ r=r;
run;
quit;
%global mad;
%global ind;
%global sumdw;
%let count = 1;
%mad(_tempout_, r)

data _tempout_;
  set _tempout_;
  u = r/&mad;
  _w2_ = .;
  if abs(u) LE 1.345 then _w2_ = 1;
  else _w2_ = 1.345/abs(u) ;
  difw = abs(_w1_ - _w2_);
run;
proc print data = _tempout_ (obs=10);
 var r u _w2_;
run;
proc sql noprint;
  select sum(difw) into :sumdw 
  from _tempout_;
quit;
data _null_;
  x1 = &sumdw+0;
  x2 = &conhub+0;
  count= &count+0;
  iterations = &iterations + 0;
  indicator = (x1>x2) and (count<iterations);
  call symput ('ind', indicator);
run;

%do %while( %eval(&ind = 1) );

%let count = %eval(&count + 1); 
data _tempout_;
  set _tempout_;
  drop r u _w1_;
run;
proc reg data = _tempout_ noprint;
  weight  _w2_;
  model &dep = &predictors;
  output out=_tempout_ r=r;
run;
quit;
%mad(_tempout_, r)

data _tempout_;
  set _tempout_ (rename = (_w2_ = _w1_) );
  u = r/&mad;
  _w2_ = .;
  if abs(u) LE 1.345 then _w2_ = 1;
  else _w2_ = 1.345/abs(u) ;
  difw = abs(_w1_ - _w2_);
run;
proc print data = _tempout_ (obs=10);
 var r _w2_;
run;
proc sql noprint;
  select sum(difw) into :sumdw 
  from _tempout_;
quit;
%put The difference is &sumdw;
data _null_;
  x1 = &sumdw+0;
  x2 = &conhub+0;
  count= &count+0;
  iterations = &iterations + 0;
  indicator = (x1>x2) and (count<iterations);
  call symput ('ind', indicator);
run;
%end;

%put Starting second loop. ;


%let count = 1;
data _null_;
  x1 = &sumdw+0;
  x2 = &conlimit+0;
  count= &count+0;
  iterations = &iterations + 0;
  indicator = (x1>x2) and (count<iterations);
  call symput ('ind', indicator);
run;


%do %while( %eval(&ind = 1) );

%let count = %eval(&count + 1); 
data _tempout_;
  set _tempout_;
  drop r u _w1_;
run;
proc reg data = _tempout_ noprint;
  weight  _w2_;
  model &dep = &predictors;
  output out=_tempout_  r=r;
run;
quit;
%mad(_tempout_, r)

data _tempout_;
  set _tempout_ (rename = (_w2_ = _w1_) );
  u = r/&mad;
  _w2_ = .;
  if abs(u) > 4.685 then _w2_ = 0;
  else _w2_ = (1 - (u/4.685)**2)**2 ;
  difw = abs(_w1_ - _w2_);
run;
proc print data = _tempout_ (obs=10);
 var r _w2_;
run;
proc sql noprint;
  select sum(difw) into :sumdw 
  from _tempout_;
quit;
%put The difference is &sumdw;
data _null_;
  x1 = &sumdw+0;
  x2 = &conlimit+0;
  count= &count+0;
  iterations = &iterations + 0;
  indicator = (x1>x2) and (count<iterations);
  call symput ('ind', indicator);
run;
%end;
proc reg data = _tempout_ ;
  weight  _w2_;
  model &dep = &predictors;
  plot &dep*(&predictors) r.*p.;
run;
quit;
%mend;

