UCLA Academic Technology Services HomeServicesClassesContactJobs
Search

SAS Code Fragments
Getting Robust Standard Errors for Clustered Data

/************************************************************
  Finite-sample Adjustment for standard error estimates
  for ordinary least square regression
  data: the input data set
  cluster: cluster variable
  dep : outcome variable
  indvars : variable list of all the independent variables
  Example:
  %reg_cluster(auto, rep78, dep=price, indvars=mpg weight)
************************************************************/

%macro reg_cluster(data, cluster, dep =, indvars = );
  data &data._1;
    set &data;
	array all(*) &dep &indvars;
	do _i = 1 to dim(all);
	  if all(_i) = . then delete;
	end;
    drop _i;
  run;
  proc genmod data = &data._1;
    class &cluster;
  model &dep = &indvars;
  repeated subject = &cluster /type = ind covb;
  ods output geercov = gcov;
  ods output GEEEmpPEst = parms;
run;
quit;
proc sql;
  select count(&cluster),count(distinct &cluster)  into :n, :m
  from &data._1;
 quit;
proc sql;
  select count(prm1) into :k
  from gcov;
quit;
data gcov_ad;
  set gcov;
  array all(*) _numeric_;
  do i = 1 to dim(all);
  all(i) = all(i)*((&n-1)/(&n-&k))*(&m/(&m-1));
  if i = _n_ then std_ad = sqrt(all(i));
  end;
drop i;
keep std_ad;
run;
data all;
  merge parms gcov_ad;
run;
proc print data = all noobs;
run;
%mend;

How to cite this page

Report an error on this page

UCLA Researchers are invited to our Statistical Consulting Services
We recommend others to our list of Other Resources for Statistical Computing Help
These pages are Copyrighted (c) by UCLA Academic Technology Services


The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.