Html Version of SAS Code to run exemplar 1
Analyses from total scores.
It is intended to show you the code and to allow links, not to use as a SAS program. The SAS program is the file which you should save to a file and/or read in to SAS. To see output from the commands go to the SAS results file.

Links in this page
Mean income with different design assumptions Subgroup lone parents Percentiles Raking with CALMAR Jacknife macro for mean

EVERYTHING INSIDE /* AND */ IS TAKEN AS A COMMENT in SAS programs These are shown in green here
back to top uparrow

analysis for peas project exemplar 1

Gillian Raab 5/04
data ex1.ex1; set ex1.ex1; drop ad12 fyear;run;
libname ex1 'C:\Documents and Settings\gillian raab\My Documents\aprojects\peas\ex1datafiles\exemp1\data';
first analyses of mean income per household
title 'Mean income without weighting from PROC MEANS';
proc means data=ex1.ex1 mean stderr; * inappropriate assumption of total random sampling;
 var hhinc; run;
 title 'Mean income with weighting from PROC MEANS';
proc means data=ex1.ex1 mean stderr; * inappropriate assumption of total random sampling;
 var hhinc;
weight gross2; run;
 title 'Survey means analysis with weighting  from  PROC SURVEYMEANS';
proc surveymeans data=ex1.ex1 mean stderr;
weight gross2;
var   hhinc;
 title 'Mean income with weighting and clustering from  PROC SURVEYMEANS';
proc surveymeans data=ex1.ex1 mean stderr; * introducing clusters ;
weight gross2;
cluster psu;
var   hhinc;
note that this next analysis is wrong as it makes too many psus
by splitting up the clusters by strata
title 'Survey means with post-strata wrongly defined as strata';
 proc surveymeans data=ex1.ex1 mean stderr; * introducing clusters and strata by ctband ;
weight gross2;
cluster psu;
var  hhinc ;
strata ctband;

now return to previous model without post strata
and get mean income by subgroup
data ex1new; set ex1.ex1;
if adulth=1 and depchldh>0 then lonep=1;
else lonep=0;
title 'income for lone parents';
proc freq data=ex1new; table lonep; run;
proc surveymeans data=ex1new mean stderr; * introducing clusters ;
weight gross2;
cluster psu;
domain lonep;
var   hhinc;
now save the file containing the macro ( from the
web page to your computer and change the code below so that it
includes the file from YOUR machine
You can open it to get help in the comments field on how to use
back to top uparrow

%include "C:\Documents and Settings\gillian raab\My Documents\aprojects\peas\ex1datafiles\exemp1\program_code\";
now use it to get the s.e.s and c.i.s for percentiles in one group.
These will be somewhat too wide as they do not allow for the
precision gained by post-stratification. This seems to make more difference
at the middle percentiles.
proc format ; value lonep 0='not lone parent' 1='lone parent';run;
/*---------------- first percentiles  all-----------------------*/
   final= new);
     /*----------------and now grouped by lone parents----------------------*/
   format =format lonep lonep.,
   final= new);
back to top uparrow

   Next code rakes survey using the MACRO CALMAR
   available from INSEE
   Documentation  is in French. Key words to know
   POIDS = weights
   OUI= yes
   NON = no
   To use download tyhe ZIP file and store the file called
   SASMACR.SAS7Bcat it in a SAS data library.
   Here it is in the same directory as the data from exemplar 6.
   Set options to allow SAS to se it
   options nomprint;
options mstored sasmstore=ex1;
now set up table of margins
data margins;
input var $ n mar1 mar2 mar3 mar4 mar5 mar6 mar7 mar8 mar9;
CTBAND 9  24.83 24.62 15.45 11.91 11.96 5.95 3.94 0.45 0.89
tenure 4 62.63  21.59 5.58  10.20   .    .     .   .    .
now rake data with CALMAR M=2 gives the method of raking
obseli=OUI (yes) sends observations eliminated to another file
effpop is the effective population size
pct=OUI indicates data in %s
		labelpoi='new weight',pct=oui,effpop=2242012);
proc sort data=ex1.ex1; by sernum;
proc sort data=newdata; by sernum;* just new weight and ID;
data poststrat; * this is the new complete data set with the raked weights;
merge ex1.ex1 newdata;
by sernum;
proc format ;
value tenure 1='owned' 2='rented LA' 3='oth pub rent' 4='private rent or rent free';
value CTBAND 1='A' 2='B' 3='C' 4='D' 5='E' 6='F' 7='G'8='H' 9='not sep valued';
proc gplot data=poststrat; * check to see how much weights have changed;
plot newweight*gross2=tenure;
plot newweight*gross2=ctband;
format tenure tenure. ;
proc surveymeans data=poststrat mean stderr; * now get new survey mean analysis ;
weight newweight;
cluster psu;
var   hhinc;

back to top uparrow

this next bit of code does a Jacknife analysis
It is not meant to be comprehensive, just to give an
indication of how it could be done. It is not
Send output to a file and switch off listing before you run it
so as not to clog system
ods html   file='temp.htm'
              path='c:\' (url=none)
/*------------------- first make the jacknife weights and rake each one of them-------------*/
%macro makejkwts(data=ex1.ex1,origwt=GROSS2,out=jck,psu=PSU,npsu=320,
* first rake the original data set;
data &out; merge alldata &data; run;
proc sort data=&out; by sernum;
%do nrep=1 %to &npsu;
	data new; set &data;
	if &psu=&nrep then &origwt=0;
	data new;
	set newdata __obseli (in=excl);
	if excl then newweight&nrep=0;
	keep &sernum newweight&nrep;
	proc sort data=new; by &sernum; run;
	data &out ;merge &out  new; by sernum; run;

ods html close;

/*----------------- now a macro to do a simple jacknife------------------*/
input parameters:
indata - input data set.
Outdata - output data (mean and std err.)
var - analysis variable on input data set.
wt - variable name on input data for full sample weights.
Repwt - prefix used in replicate weight names. Replicate weight
names must be in the form: repwt01, repwt02, ... repwt45
n - number of replicate weights on input data set.
Program creates an output dataset containing the method, the
number of replicates, the mean, and the standard error.

%macro jack2_mean(indata,outdata,var,repwt,n);
%do i=1 %to &n; ** cycle through the replicate weights **;
%put calculating results for replicate weight &i;
	proc means data=&indata noprint;
	var &var;
	weight &repwt&i;
	output out=rep_mean_junk mean=mean;
	data rep_mean_junk2; set rep_mean_junk2
	if j then rep=&i;
proc means data=rep_mean_junk2 noprint;
var mean;
output out=&outdata mean=mean var=var;
data &outdata; set &outdata(keep=mean var);
_METHOD_ = "JK2";
&var._mean = mean;
&var._std = sqrt(var*(&n-1)*(&n-1)/&n);
*drop mean var;
proc print;
%mend jack2_mean;