This webpage provides SAS code for different methods for calculating the HEI. To learn more about a particular method, visit Overview of the Methods and Calculations, Mean Ratio Method, Population Ratio Method, Bivariate Method, or Multivariate MCMC Method. SAS code for the HEI-Toddlers-2020 and the HEI-2020 is coming soon.
The code provided below is for the SAS software package. Please direct questions about the HEI SAS code to RFAB@mail.nih.gov.
Researchers are encouraged to ask questions and share their code via the HEI list-serv. To join the HEI list-serv, send an email to listserv@list.nih.gov with SUBSCRIBE HEI <first name> <last name> in the body of the text (remove > and <, for example: SUBSCRIBE HEI John Smith).
In the table below, sample SAS code is available for calculating the HEI with different dietary assessment tools. These include:
- 24-hour recall(s) example: Automated Self-Administered 24-Hour Dietary Assessment Tool
- National Health and Nutrition Examination Survey: NHANES weighted national survey with 24-hour recall(s) example
- Cohort with food frequency questionnaire example: NIH-AARP Diet and Health Study: NIH-AARP
Where possible, the zip files provided include sample SAS code, macros for scoring, and sample output. These macros can be used to calculate HEI–2015, HEI–2010, and HEI–2005 component and total scores and can be applied to any SAS dataset containing the requisite variables.
HEI-2015 includes only the macro for scoring, as legumes (beans and peas) are now allocated to both the vegetable and protein food groups. For some applications, such as an analysis of the food environment, the scoring macro can be used (provided below).*
In the 2010 and 2005 versions, there are two macros, one of which allows for the allocation of legumes (beans and peas) to count as part of the vegetable or protein food groups, and the other creates ratios and scores them using the applicable standards.
Download HEI Sample Code
Click each to expand for more information.
Simple HEI Scoring Algorithm - Per Day
ASA24 Example
To calculate HEI scores for each intake day regardless of how many recalls or record days are available per person.
HEI-2015- ASA24-2016, ASA24-2018, & ASA24-2020-Per Day HEI-2015 [ZIP - 40.3 KB]
- For ASA24 users with 2011, 2012 or 2014 version data, a separate SAS program must be run to calculate Food Patterns Equivalents Database (FPED) values in order to use this code to calculate the HEI-2015.
HEI-2010
- ASA24-2016, ASA24-2018, & ASA24-2020-Per Day HEI-2010 [ZIP - 44.4 KB]
- ASA24-2014-Per Day HEI-2010 [ZIP - 46.6 KB]
- Note that there are two SAS programs to calculate HEI-2010 because the pyramid equivalents databases for ASA24-2016 vs. ASA24-2014 versions are different (2016 uses USDA's Food Patterns Equivalents Database or FPED, and 2014 versions use the MyPyramid Equivalents Database or MPED).
Note: Some users have found that these SAS programs will drop observations from the analysis if the ID field is not the same length for all observations. To prevent this error, the observations with the longest ID length should be listed first when the data is imported into SAS.
NHANES Example
HEI-2010
These two programs calculate the HEI for one day of 24HR data.
- NHANES-2003-2004-MPED-Per Day HEI-2010 [ZIP - 61.2 KB]
- NHANES-2009-2010-FPED-Per Day HEI-2010 [ZIP - 44.1 KB]
For more information about NHANES analyses with the HEI, see the Frequently Asked Questions page, including the question “What information do I need when utilizing National Health and Nutrition Examination Survey (NHANES) data to calculate HEI scores?“
FFQ Example
This code uses NIH-AARP Diet and Health Study data as an example. This code estimates component and total HEI scores for each individual and can be modified for use with other FFQs.
HEI-2015HEI-2010
HEI-2005
Simple HEI Scoring Algorithm - Per Person
ASA24 Example
To calculate one HEI score per individual across all days of ASA24 recalls or records collected, even if some Respondents have varying number of intake days, including only one.
In this program, the cup equivalents of total fruit summed over x days is divided by the total energy summed over the x days:
∑ total fruit over x days
∑ total energy over x days
rather than the daily average:
total fruit/total energy per day
x days
The ratio is compared with the HEI standard on a cup equivalent per 1000 kcal basis as are many HEI components.
HEI-2015- ASA24-2016, ASA24-2018, & ASA24-2020-Per Person HEI-2015 [ZIP - 38.4 KB]
- For ASA24 users with 2011, 2012 or 2014 version data, a separate SAS program must be run to calculate Food Patterns Equivalents Database (FPED) values in order to use this code to calculate the HEI-2015.
HEI-2010
- ASA24-2016, ASA24-2018, & ASA24-2020-Per Person HEI-2010 [ZIP - 43.8 KB]
- ASA24-2014-Per Person HEI-2010 [ZIP - 47.1 KB]
- Note that there are two SAS programs to calculate HEI-2010 because the pyramid equivalents databases for ASA24-2016 vs. ASA24-2014 versions are different (2016 uses USDA's Food Patterns Equivalents Database or FPED, and 2014 versions use the MyPyramid Equivalents Database or MPED).
Note: Some users have found that these SAS programs will drop observations from the analysis if the ID field is not the same length for all observations. To prevent this error, the observations with the longest ID length should be listed first when the data is imported into SAS.
NHANES Example
The programs below correctly calculate ratios used in the HEI algorithm when a participant has more than one day of 24HR data. For example, the cup equivalents of total fruit summed over x days is divided by the total energy summed over the x days:
∑ total fruit over x days
∑ total energy over x days
rather than the daily average:
total fruit/total energy per day
x days
The ratio is compared with the HEI standard, in this case on a cup equivalent per 1000 kcal basis.
HEI-2015HEI-2010
- NHANES-2003-2004-MPED-Per Person HEI-2010 [ZIP - 58.4 KB]
- NHANES-2009-2010-FPED-Per Person HEI-2010 [ZIP - 64 KB]
For more information about NHANES analyses with the HEI, see the Frequently Asked Questions page, including the question “What information do I need when utilizing National Health and Nutrition Examination Survey (NHANES) data to calculate HEI scores?“
Population Ratio Method
ASA24 Example
Mean HEI scores for a population, subpopulation, or group can be estimated using ASA24 data and the population ratio method. This approach has been shown to be the preferred method of estimating a population's mean usual HEI component and total scores on the basis of a single day of data, which is sufficient for this purpose.
The output includes mean component and total HEI scores for the population, along with their standard errors and confidence intervals. Because the HEI is a multi-dimensional construct involving multiple densities (amounts of food per 1,000 calories and ratios of fatty acids), a simple method for estimating standard errors is not available. In this code, a Monte Carlo simulation step is included for the calculation of standard errors.
To apply the population ratio method, the mean intake of the relevant food groups, nutrients and energy among the population of interest is calculated first; then ratios of the means are calculated and compared with the applicable standards for scoring. See Freedman et al. 2008 for a description and application of the population ratio method. This code can be modified to use with other datasets that do not involve complex sampling designs.
HEI-2015- ASA24-2016, ASA24-2018, & ASA24-2020-Population Ratio HEI-2015 [ZIP - 56.2 KB]
- For ASA24 users with 2011, 2012 or 2014 version data, a separate SAS program must be run to calculate Food Patterns Equivalents Database (FPED) values in order to use this code to calculate the HEI-2015.
HEI-2010
- ASA24-2016, ASA24-2018, & ASA24-2020-Population Ratio HEI-2010 [ZIP - 59.9 KB]
- ASA24-2014-Population Ratio HEI-2010 [ZIP - 60.4 KB]
- Note that there are two SAS programs to calculate HEI-2010 because the pyramid equivalents databases for ASA24-2016 vs. ASA24-2014 versions are different (2016 uses USDA's Food Patterns Equivalents Database or FPED, and 2014 versions use the MyPyramid Equivalents Database or MPED).
HEI-2005
Note: Some users have found that these SAS programs will drop observations from the analysis if the ID field is not the same length for all observations. To prevent this error, the observations with the longest ID length should be listed first when the data is imported into SAS.
NHANES Example
HEI-2010
For the calculation of HEI-2010 scores using NHANES data, two examples are available.
- NHANES-2009-2010-FPED-Population Ratio HEI-2010 [ZIP - 50.3 KB]
- NHANES-2003-2004-MPED-Population Ratio HEI-2010 [ZIP - 53.1 KB]
HEI-2005
SAS code for calculating HEI-2005 scores using NHANES data is also available. This code uses NHANES 2001-2002 data as an example and links to the MPED.
For more information about NHANES analyses with the HEI, see the Frequently Asked Questions page, including the question “What information do I need when utilizing National Health and Nutrition Examination Survey (NHANES) data to calculate HEI scores?“
Bivariate Methods
NHANES Example
If more than one recall is available for at least a subsample, a bivariate approach can be used to estimate distributions of scores for the components of the HEI. This approach is described in detail in Freedman et al. 2010. SAS code for implementing this approach is available here.
With individual-level data, it also is possible to calculate mean scores or scores of the mean ratio. This approach is not recommended for the purposes of describing mean HEI scores. See Freedman et al. 2008 for details.
The use of food frequency questionnaire (FFQ) data for the estimation of means or distributions of HEI scores is not recommended.
For more information about NHANES analyses with the HEI, see the Frequently Asked Questions page, including the question “What information do I need when utilizing National Health and Nutrition Examination Survey (NHANES) data to calculate HEI scores?“
Multivariate Markov Chain Monte Carlo (MCMC) Method
The MCMC method estimates the distribution of HEI component and total scores. The method addresses measurement error, episodic consumption, skewness, and the correlation between the components and energy.
NHANES Example
MCMC SAS code for the HEI-2015 (including a wrapper) [ZIP - 4.25 MB] is available here. Although this example is for NHANES 2011-12 data, the programs can be used with other cycles of NHANES and any data with a similar survey structure with BRR weights. For analyses using other types of weights, additional modifications would be needed. Please note that this code may take 1 to 3 days to run.
HEI-2010 and HEI-2005
The MCMC approach has been described for the HEI–2005 by Zhang et al. 2011 and applied in the Evaluation of the HEI–2010. SAS macros for the implementation of this approach are available here.
For more information about NHANES analyses with the HEI, see the Frequently Asked Questions page, including the question “What information do I need when utilizing National Health and Nutrition Examination Survey (NHANES) data to calculate HEI scores?“