National Health and Nutrition Examination Survey

Sample Code

The Continuous NHANES Tutorials have been updated as of December 2019.

See the new sample code to replicate the estimates from an NCHS Data Brief on depression, using SUDAAN, SAS Survey, Stata, and R software.

This page contains three types of code samples:

  • supplemental materials for the revised tutorial modules 1 through 5;
  • sample code to replicate NHANES estimates from NCHS publications such as Data Briefs, QuickStats, or National Health Statistics Reports; and
  • sample code to execute the "tasks" described in tutorial modules 6 through 10. Note that modules 6-10 are under construction and are subject to change.

These codes demonstrate the basic steps to download and import a Continuous NHANES data file in SAS transport format (.xpt).

The sample codes to replicate NHANES estimates from NCHS publications also demonstrate how to download, append, and merge data files as needed to generate the analytic dataset for each project.

This code replicates the examples discussed in module 3 to illustrate oversampling in NHANES and the imporance of using sample weights.

This code includes statements to download the public-release NHANES data files that are needed for the analysis. A tutorial-specific dataset is not required.

SAS or SAS-callable SUDAAN
Stata
R

The following code examples demonstrate how to replicate selected results from NCHS publications such as Data Briefs, QuickStats, or National Health Statistics Reports.

Accessing the data sets

These code examples use the standard public-release NHANES datasets. The Continuous NHANES data (since 1999-2000) is released in SAS transport file (.xpt) format. SAS transport files can be imported into many software packages. See Module 1, Datasets and Documentation, for a detailed description of how the data files are organized.

The code examples include statements to download the required data files from the NHANES website and to import them into the software package for use.

Editing and running the code

You may need to edit some lines of the code in order to run it on your computer. For example, you may need to edit the path to a directory where you want to save a permanent dataset or the location where you have saved another piece of code provided by the tutorial. After you have downloaded a code example and opened it in the appropriate software program, search for comments with the keyword “TutorialUser” to identify lines of code that you will need to customize before you can run the code.

Software versions

Unless otherwise noted, the example programs use procedure syntax available in the following software versions:

  • SAS-callable SUDAAN version 11
  • SAS version 9.4 with SAS/STAT 14.1
  • R version 3.5.2 with “survey” package version 3.35.1
  • STATA/SE version 15

Review the documentation for the software version you are using to check for different capabilities or syntax changes.

This example replicates the estimates in Figure 1. Percentage of persons aged 20 and over with depression, by age and sex: United States, 2013–2016.

Data table for Figure 1 (PDF)

Concepts: proportions, t-test

Data file documentation

Demographics (DEMO): 2013-2014, 2015-2016

Mental Health-Depression Screener (DPQ): 2013-2014, 2015-2016

SUDAAN
SAS Survey
Stata
R

Reference

Brody DJ, Pratt LA, Hughes J. Prevalence of depression among adults aged 20 and over: United States, 2013–2016. NCHS Data Brief, no 303. Hyattsville, MD: National Center for Health Statistics. 2018.

Below is a listing of all the sample code and datasets used to complete the "tasks" in the Continuous NHANES tutorial. They are organized by module and then task. If a module or task is not listed it is because it did not have a related program. Programs are available as SAS programs (i.e. SAS Survey Procedures and SAS-callable SUDAAN) and Stata programs. The datasets are SAS or Stata datasets for Windows.

WARNING

Modules 6-10 are under construction and are subject to change. If you are working through these modules, you may wish to download the example codes and datasets to ensure your continued access.

These datasets are intended to be used with the tutorial only, as they may contain a subset of the variables available. If you wish to conduct your own analyses, please download the most current and complete datasets from the NHANES website.

Program code used in this tutorial follow procedures available in SAS 9.2, SUDAAN 9.0 or SUDAAN 8.0, and Stata/SE 10.0.

These programs use variable formats listed in the Tutorial Formats page. You may need to format the variables in your dataset the same way to reproduce results presented in the tutorial.

A regular left-click may not download the SAS datasets or SAS code correctly. Your browser may try to open and attempt to interpret the contents of the dataset or program. If this happens, right-click the link and save the dataset or program to your hard drive. After it has downloaded, then use your statistical software program to open the dataset to view it.

Task 1a: Check Frequency Distribution and Normality in SAS

Step 1: Use the univariate procedure to generate descriptive statistics in SAS
Step 3: Request selective statistics and output results to SAS dataset

Task 1c: How to Check Frequency Distribution and Normality in Stata

Step 1: Use the summarize command to generate weighted summary statistics for a population subset
Step 3: Use tabstat to request selective statistics

Task 2: Generate Percentiles

Task 3a: Generate Means Using SUDAAN

Step 1: Use proc descript to Generate Means in SUDAAN
Step 4: Use proc descript to Generate Geometric Means in SUDAAN

Task 3b: Generate Means Using SAS Survey Procedures

Task 3c: Generate Means Using Stata

Step 2: Use svy:mean to generate means and standard errors in Stata
Task 4a: Generate Proportions Using SUDAAN
Task 4b: Generate Proportions Using SAS Survey Procedures
Task 4c: Generate Proportions Using Stata

Task 1a: Set Up a t-test in NHANES Using SUDAAN

Step 1: Set Up SUDAAN to Produce Means
Step 3: Perform t-test to Test for Significance

Task 1b: Set Up a t-test in NHANES Using SAS

Step 2: Set Up SAS Survey Procedures to Produce Means
Step 4: Perform t-test to Test for Significance (SAS Survey Procedures 9.2)

Task 1c: Set Up a t-test in NHANES Using Stata

Task 2a: Generate Confidence Intervals for Geometric Means Using SUDAAN

Task 2c: Generate Confidence Intervals and Geometric Means Using Stata

Task 3a: Perform Chi-Square Test Using SUDAAN

Task 3b: Perform Chi-Square Test Using SAS Survey Procedures

Task 3c: Perform Chi-Square Test Using Stata

Task 1a: How to Generate Age-Adjusted Prevalence Rates in SUDAAN

Step 1: How to Generate Age-Adjusted Prevalence Rates in SUDAAN
Optional Step 2: How to Generate Age-Adjusted Means
Step 1: How to Generate Age-Adjusted Prevalence Rates in SAS Survey Procedures 9.2
Optional Step 2: How to Generate Age-Adjusted Means in SAS Survey Procedures 9.2

Task 1c: How to Generate Age-Adjusted Prevalence Rates in Stata

Step 1: How to Generate Age-Adjusted Prevalence Rates in Stata
Optional Step 2: How to Generate Age-Adjusted Means in Stata

Task 2a: Generate Population Counts in SUDAAN

Task 2b: Generate Population Counts in SAS Survey Procedures

Task 2c: Generate Population Counts in Stata

To report problems or comments, use the NHANES Web Tutorial email. Do not direct questions to this email address, as it can only receive feedback and we will not be able to reply.

Page last reviewed: 2/21/2020