Sunday, June 30, 2013

Downloading and Merging NHANES datasets in R

The National Health and Nutrition Examination Survey (NHANES) is a program of studies designed to assess the health and nutritional status of adults and children in the United States. The survey is unique in that it combines interviews and physical examinations. The data files for more recent surveys are given in SAS Export format. To read these files in to R, one needs to use functions in the foreign package. If you don’t have this package, you may need to install it first. In the first step, we download these files and then in the second step we import these files to R.

# load foreign package (Converts data files into R)
require(foreign)    

# Set your working directory
setwd( "<YOUR WORKING DIRECTORY>")

### Download demographics file of NHANES 2005-2006 dataset
download.file(
ftp://ftp.cdc.gov/pub/Health_Statistics/nchs/nhanes/2005-2006/DEMO_D.XPT,
"Demo0506.xpt", mode='wb')

###Read downloaded file
Demo56<-read.xport("Demo0506.xpt")

### Download Blood pressure file of NHANES 2005-2006 dataset
download.file(
ftp://ftp.cdc.gov/pub/Health_Statistics/nchs/nhanes/2005-2006/BPX_D.XPT,
"BP0506.xpt", mode='wb')

### Read downloaded file
BP56<-read.xport("BP0506.xpt")

### Merge the two files
N_05_06 <- merge(Demo56, BP56, all=T)

You can download several files and then merge them together to get your dataset.

1 comment:

Walker Blackston said...

This was incredibly helpful.

I am a PhD student working on a paper using NHANES, and lost my SAS license, panicked, then found this to my great joy.

Great work and keep it up!