/*----------------------------------------------------------------
ex2.sas  Code to run exemplar 2
Written by Gillian Raab
 August 2004

first tell sas where the data for this exemplar is stored
  Change this to the directory  where YOU have the data
-----------------------------------------------------------------*/
libname exemp2 "c:/documents and settings/gillian raab/my documents/aprojects/peas/web/exemp2/data";
/*-------------------------------------------------
exemplar first for simple proportions of internet use

INTUSE is a 0/1 variable so its mean gives the percentage use
----------------------------------------------------*/
title 'Simple proportion  internet use';
proc surveymeans data=exemp2.ex2 mean stderr nobs clm; * no strata;
weight ind_wt;
cluster psu;
var intuse;
strata stratum;
run;
/*-------------------------------------------------------
we can also get proportions in groups that have several categories by defining 
the variable as a class variable
IMPORTANT IMPORTANT  IMPORTANT
!!!!!!!!!!!!!????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Before this you should open and run the SAS programme ex2_formats.sas
This provides formats s to use as value labels for your variables

This gives proportions of internet users who use the internet for different
numbers of hours per day

BUT SEE EXEMPLAR DESCRIPTION FOR A DISCUSSION AS TO WHETHER THERE IS A 
TECHNICAL PROBLEM HERE
-----------------------------------------------------------------------*/
title 'Hours of internet use for users';
proc surveymeans data=exemp2.ex2 mean stderr nobs clm; * no strata;
weight ind_wt;
cluster psu;
strata stratum;
var rc5;                 * say which variable(s) you want;
class  rc5;              * and define it as a class (category);
format rc5 hours.;       * with format defined as hours. See ex2_formats.sas;
run;
/*-------------------------------------------------------------------------
WE CAN ALSO get proportions in subgroups for simple means by
using the DOMAIN statement
THIS is AVAILABLE in SAS 8.2 only and is not in the SAS online help yet

Here we look at usage by sex
--------------------------------------------------------------------------------*/
title 'internet use by sex';
proc surveymeans data=exemp2.ex2 mean stderr nobs clm; 
weight ind_wt;
cluster psu;
strata stratum;
var intuse;
domain sex;
format sex sex.;
run;
title 'hours used by sex';
/*---------------------------------------------------------------------------------
and we can similarly look at the proportions of men and women internet users
who use the internet for different times each day
----------------------------------------------------------------------------------*/
proc surveymeans data=exemp2.ex2 mean stderr nobs clm; 
weight ind_wt;
cluster psu;
strata stratum;
var rc5;
class rc5;
domain sex;
format sex sex.;
run;
/*------------------------------------------------------
NOW ANALYSES BY FOR TABLES

first do PROC freq to get a table of weighted percentages
!!!!!!!!!!!!!!!!!
IMPORTANT IMPORTANT  IMPORTANT
!!!!!!!!!!!!!????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Before this you should open and run the SAS programme ex2_formats.sas
This puts value labels on your variables
--------------------------------------------------------*/
title 'tables with wrong chi square results';
proc freq data=exemp2.ex2;
table intuse*sex/nopercent nocol chisq;
weight ind_wt;
format rc5 hours. emp_sta employ.;
run;
/*--------------------------------------------------------
this has given you a table of weighted percentages
that are the correct estimates of the percentages using internet 
by sex. It also gives a chi squared value, but it makes 
no allowance for the sample design and so is not correct
------------------------------------------------------------*/
/*--------------------------------------------------------
same is true for all other tables - here isa larger one
------------------------------------------------------------*/
proc freq data=exemp2.ex2;
table shs_6cla*rc5/nopercent nocol chisq missing;
weight ind_wt;
format rc5 hours. shs_6cla rural.;
run;
/*--------------------------------------------------------
Now use PROC tabulate to get a nice layout for a 
weighted Table
------------------------------------------------------------*/
proc tabulate data=exemp2.ex2 missing;
var ind_wt;
class rc5  shs_6cla;
format rc5 rc5Z. shs_6cla rural.; ;
table	shs_6cla='', (rc5 all)*ind_wt=''*pctsum<rc5 all>*f=4.1 n*f=5.0;
run; 
/*----------------------------------------------------------
sas does not do tables or logistic regression or surveys
----------------------------------------------------------------*/