Personal tools
You are here: Home Software Packages SAS Handy SAS programs Testing proportions using SAS and R

Testing proportions using SAS and R

Summarize data with SAS, then create R code to test proportions.

Earlier this year, the question was raised about testing proportions. The data was hospital admissions for children due to various conditions. The test was to compare the proportion of admissions by race/ethnicity with the population estimates for the age group being studied. Rather than summarize the numbers and enter them into one of the online calculators available online we decided to use R and the built-in "prop.test" function. Since there were over 30 conditions, entering the data would have been a tedious task. The approach was taken to summarize the data in SAS, then create code for input into the R statistical package. The SAS code simply summarized the admissions by race/ethnicity and conditions, summarized the population estimates by race/ethnicity and merged the summarized values into one dataset. This data was then put into statements to be executed by R by the following SAS statements: data _NULL_; /* Set lengths for character strings to be created */ length ACS_condition $30. object_string $40.; /* Output file that will contain the R code to be executed */ file "By-Ethnicity-Proportions-2001.R"; set patient.admissions end=eof; /* This statement tells R to sent the output to a file, rather than display the results on the terminal */ if _N_ eq 1 then put "sink(""2001-Proportions.txt"")"; /* This put the formated value of the variable into a character string e.g. 11 becomes "Asthma", 18 becomes "Diabetes", etc. */ ACS_condition = put(ACS_group,ACS_ALL.); /* This statement creates an R object for the population estimates */ put "population.estimate.2001 <- c(" White_est "," Black_est ", " Hispanic_est ")"; /* This statement creates an R object for the count of admissions */ put ACS_condition "<- c(" White_Admissions "," Black_Admissions ", " Hispanic_Admissions ")"; /* The name of the condition is concatenated with the word "result" */ object_string = trim(left(ACS_condition)) || ".result"; /* The code to be executed - */ put object_string "<- prop.test(" ACS_condition ", population.estimate.2001)"; /* This prints the R output into the file specified at the beginning */ put "print(" object_string ")"; /* This closes the R output file after all the tests are run */ if eof then put "sink()"; run; The resulting file looked like this: sink("2001-Proportions.txt") ... population.estimate.2001 <- c(2522467 ,409612 ,166495 ) Asthma <- c(3318 ,3403 ,746 ) Asthma.result <- prop.test(Asthma , population.estimate.2001) print(Asthma.result ) population.estimate.2001 <- c(2522467 ,409612 ,166495 ) Diabetes.A.B.C <- c(1062 ,283 ,61 ) Diabetes.A.B.C.result <- prop.test(Diabetes.A.B.C , population.estimate.2001) print(Diabetes.A.B.C.result ) population.estimate.2001 <- c(2522467 ,409612 ,166495 ) Hypoglycemia <- c(37 ,11 ,2 ) Hypoglycemia.result <- prop.test(Hypoglycemia , population.estimate.2001) print(Hypoglycemia.result ) ... sink() After the file was created, it was run with R by simply entering source("By-Ethnicity-Proportions-2001.R") at the command prompt. The values were calculated and written to the file. Here are some of the results: data: Asthma out of population.estimate.2001 X-squared = 7481.043, df = 2, p-value < 2.2e-16 alternative hypothesis: two.sided sample estimates: prop 1 prop 2 prop 3 0.001315379 0.008307862 0.004480615 data: Diabetes.A.B.C out of population.estimate.2001 X-squared = 59.5523, df = 2, p-value = 1.171e-13 alternative hypothesis: two.sided sample estimates: prop 1 prop 2 prop 3 0.0004210164 0.0006908977 0.0003663774 data: Hypoglycemia out of population.estimate.2001 X-squared = 3.4287, df = 2, p-value = 0.1801 alternative hypothesis: two.sided sample estimates: prop 1 prop 2 prop 3 1.466818e-05 2.685468e-05 1.201237e-05
Document Actions

Copyright ©2008, The Pennsylvania State University | Privacy and Legal Statements
Contact the Help Site Administrator | Last modified Aug 13, 2008 | Weblion Partner