# Include Foreign Package To Read SAS Transport Files # Include Survey Package For Survey Analysis library(foreign) library(survey) # Create Temporary File To Store Downloaded SAS Transport Files tf <- tempfile() # Download NHANES 1999-2000 Demographic Data To Temporary File download.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/1999-2000/DEMO.xpt", tf, mode="wb") # Create Data Frame From Temporary File & Keep Variables Of Interest DEMO <- read.xport(tf)[,c("SEQN","SDMVPSU","SDMVSTRA","WTMEC4YR","RIAGENDR","RIDAGEYR")] # Download NHANES 2001-2002 Demographic Data To Temporary File download.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/DEMO_B.xpt", tf, mode="wb") # Create Data Frame From Temporary File & Keep Variables Of Interest DEMO_B <- read.xport(tf)[,c("SEQN","SDMVPSU","SDMVSTRA","WTMEC4YR","RIAGENDR","RIDAGEYR")] # Append Demographic Data DEMO_4Yr <- rbind(DEMO, DEMO_B) # Download NHANES 1999-2000 Cholesterol Data To Temporary File download.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/1999-2000/LAB13.xpt", tf, mode="wb") # Create Data Frame From Temporary File & Keep Variables Of Interest LAB13 <- read.xport(tf)[,c("SEQN","LBXTC")] # Download NHANES 2001-2002 Cholesterol Data To Temporary File download.file("ftp://ftp.cdc.gov/pub/Health_Statistics/NCHS/nhanes/2001-2002/L13_B.xpt", tf, mode="wb") # Create Data Frame From Temporary File & Keep Variables Of Interest L13_B <- read.xport(tf)[,c("SEQN","LBXTC")] # Append Cholesterol Data LAB13_4Yr <- rbind(LAB13, L13_B) # Merge Files Together Analysis_Data <- merge(DEMO_4Yr, LAB13_4Yr, by="SEQN", all=TRUE) # Create Factor Variables Analysis_Data$Gender <- factor(Analysis_Data$RIAGENDR, labels=c("Male","Female")) Analysis_Data$AgeGroup <- cut(Analysis_Data$RIDAGEYR, breaks=c(-1,19,39,59,86), labels=c("0-19","20-39","40-59","60+")) # Define Survey Design NHANES <- svydesign(id=~SDMVPSU, strata=~SDMVSTRA, nest=TRUE, weights=~WTMEC4YR, data=Analysis_Data) # Get Counts For The Total svyby(~LBXTC, "Total", subset(NHANES, RIDAGEYR >= 20), unwtd.count, keep.names=FALSE) # Get Mean Total Cholesterol For The Total svymean(~LBXTC, subset(NHANES, RIDAGEYR >= 20), na.rm=TRUE) # Get Counts By Gender svyby(~LBXTC, ~Gender, subset(NHANES, RIDAGEYR >= 20), unwtd.count, keep.names=FALSE) # Get Mean Total Cholesterol By Gender svyby(~LBXTC, ~Gender, subset(NHANES, RIDAGEYR >= 20), svymean, keep.names=FALSE) # Get Counts By Age Group svyby(~LBXTC, ~AgeGroup, subset(NHANES, RIDAGEYR >= 20), unwtd.count, keep.names=FALSE) # Get Mean Total Cholesterol By Age Group svyby(~LBXTC, ~AgeGroup, subset(NHANES, RIDAGEYR >= 20), svymean, na.rm=TRUE, keep.names=FALSE) # Get Counts By Gender, Age Group svyby(~LBXTC, ~AgeGroup+Gender, subset(NHANES, RIDAGEYR >= 20), unwtd.count, keep.names=FALSE) # Get Mean Total Cholesterol By Gender, Age Group svyby(~LBXTC, ~AgeGroup+Gender, subset(NHANES, RIDAGEYR >= 20), svymean, na.rm=TRUE, keep.names=FALSE)