/*---------------------------------------------------------------- ex2.sas Code to run exemplar 2 How to use this file Open it in SAS and highlight and run the code one are a few lines at a time. Code can be run 1) from the RUN menu 2) by clicking the running man icon 3) by the function keys - usually F3 ALways check the log file to see if it has worked ANd see the results in the Results files. Code between 'slash star' and 'star slash' is comments to help you understand what you are doing If this whole section is submitted to SAS it will be ignored. BUT !!!!! be careful not to just submit the first half or SAS will treat everything that follows as comments. --------------------------------------------------------------------*/ ; /*----------------------------------------------------------------- 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 "G:\peas\ex2datafiles\data"; /*------------------------------------------------- exemplar first for simple proportions of internet use and other results in Table 2.3 INTUSE is a 0/1 variable so its mean gives the percentage use The declaration of the survey design is done withing the survey procedures in SAS ----------------------------------------------------*/ title 'Simple proportion internet use'; options nocenter; 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 -----------------------------------------------------------------------*/ title 'Hours of internet use for users'; proc surveymeans data=exemp2.ex2 mean stderr nobs clm; 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 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 looking at the effect of different designs on the precision of estimates for results as in Table 2.4 ----------------------------------------------------------------*/ title 'clustering no stratification'; proc surveymeans data=exemp2.ex2 mean stderr nobs clm; cluster psu; weight ind_wt; var intuse; run; title ' stratification no clustering'; proc surveymeans data=exemp2.ex2 mean stderr nobs clm; weight ind_wt; strata stratum; var intuse; run; title 'no stratification or clustering'; proc surveymeans data=exemp2.ex2 mean stderr nobs clm; weight ind_wt; var intuse; 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 with row percentages and the base numbers shown in the final column ------------------------------------------------------------*/ title 'nice table layout'; proc tabulate data=exemp2.ex2 missing; var ind_wt; class rc5 shs_6cla; format rc5 hours. shs_6cla rural.; ; table shs_6cla='', (rc5 all)*ind_wt=''*pctsum*f=4.1 n='base'*f=5.0; run; /*---------------------------------------------------------- sas does not do tables or logistic regression or surveys ----------------------------------------------------------------*/