Task 1: How to Format and Label NHANES Variables

Here are the steps to formatting and labeling NHANES variables:

 

Step 1: Name, Define, and Apply Custom Formats

To create custom formats for your dataset, you will need to use the PROC FORMAT procedure.  Using the VALUE statement, you first assign a name to a format.  Then, you use descriptive text to define the values of the format.  Note that all assigned text names for the values must be surrounded by single quotation marks in order to be applied properly. 

The sample code, which comes from the "Food Sources" program, below shows how to name and define a custom format.  This example uses the format FOODGRPF.  (Note that you can assign any name you choose, so long as it meets the SAS specifications for a valid format name.  See the SAS manual for more information.)  This format defines values 1 through 9, with each value representing one of the broad food groups based on the USDA food code.  FOODGRPF will be applied as a format to the variable FOODGRP later in this step. 

 

Program to Name and Define a Custom Format

Sample Code

*--------------------------------------------------;
* The PROC FORMAT procedure assigns text names     ;       
* to the numeric values of the FOODGRPF format.    ;
*--------------------------------------------------;

proc format ;
    value FOODGRPF
        1 = "Milk & Milk Products"
        2 = "Meat, Poultry, Fish & Mixtures"
        3 = "Eggs"
        4 = "Legumes, Nuts and Seeds"
        5 = "Grain Products"
 
       6 = "Fruits"
        7 = "Vegetables"
        8 = "Fats, Oils & Salad Dressings"
        9 = "Sugar, Sweeteners & Beverages" ;

run ;

 

After you have named and defined a format, you apply it to selected variables using the FORMAT statement in the data step of your code.  Applying a format to a variable allows you to determine how the values will look in the output (e.g., food group 1 will be represented by the text “Milk and Milk Products,” food group 2 will be represented by the text “Meat, Poultry, Fish & Mixtures”).  When assigning formats to variables, note that format names always come directly after variable names and MUST end with a period, as shown in the sample code below.

 

Program to Apply a Custom Format

Sample Code

*--------------------------------------------------------------;
* The FORMAT statement applies the format FOODGRPF to FOODGRP  ;
*--------------------------------------------------------------;

data FDSRC;
    set FDSRC;
    format FOODGRP FOODGRPF. ;
run ;

 

Info iconIMPORTANT NOTE

In each of the Preparing an Analytic Dataset modules beginning with this module, you will be working with temporary datasets, which are saved in the WORK folder of your SAS program.  The dataset exists only as long as your SAS session and is deleted when you exit the program.  If you would like to save these datasets so that you can return to them at a later time, you can learn how to do this in the Save a Dataset module at the end of this course.

 

Step 2: Apply Labels to Variables

Variables are given a text description using a LABEL statement.  One way to do this is by using a SAS data step, as shown below in the sample code from the “Food Sources” program.  User-defined labels should always be surrounded by quotation marks. 

 

 

Program to Label Variables

Sample Code

*----------------------------------------------------;
* The LABEL statement applies a text description     ;
* to the variable  FOODGRP                           ;
*----------------------------------------------------;

data FDSRC;
    set FDSRC;
    label FOODGRP = "Broad food grp based on 1st digit
                     of USDA food code"
;
run ;

 

 

 

Info iconIMPORTANT NOTE

In each of the Preparing an Analytic Dataset modules, you will be working with temporary datasets, which are saved in the WORK folder of your SAS program.  The dataset exists only as long as your SAS session and is deleted when you exit the program.  If you would like to save these datasets so that you can return to them at a later time, you can learn how to do this in the Save a Dataset module at the end of this course.

 

 

close window icon Close Window to return to module page.