Task 3: How to Extract and Save NHANES Data Files in a SAS-Accessible Library

Extracting an SAS transport file and saving it to a SAS-accessible library involves three steps:

Step 1: Assign LIBNAME

Assign a LIBNAME to each SAS transport file being downloaded.  In this example, the transport files, which will be used in sample programs throughout the tutorial, are stored in the DOWNLOAD folder, created in Task 2 of this module. The extension “_b” is used to denote files from the 2001-2002 survey cycle and the extension “_c” is used to denote files from the 2003-2004 survey cycle.

The XPORT statement tells SAS to extract the data from the transport file, using the XPORT engine, into a SAS-accessible format.  Remember to surround the pathnames with quotation marks.

Finally, assign a LIBNAME (NH) to the C:\NHANES\DATA folder. This is where the permanent datasets will be saved on your computer.

 

Program to Assign LIBNAMES

Sample Code

libname XPDB xport "c:\nhanes\download\demo_b.xpt" ;  

libname XPDC xport "c:\nhanes\download\demo_c.xpt" ;  

libname XPIB xport "c:\nhanes\download\drxiff_b.xpt" ;

libname XPTB xport "c:\nhanes\download\dr1tot_b.xpt" ;  

libname XPTC xport "c:\nhanes\download\dr1tot_c.xpt" ;  

libname XPIC xport "c:\nhanes\download\dr1iff_c.xpt" ;  

libname XPOC xport "c:\nhanes\download\osq_c.xpt" ;  

libname XPSA xport "c:\nhanes\download\dsq1_c.xpt" ;  

libname XPSB xport "c:\nhanes\download\dsq2_c.xpt" ;  

libname XPSC xport "c:\nhanes\download\DSPI.xpt" ;  

libname XPSD xport "c:\nhanes\download\DSII.xpt" ;  

libname NH "c:\nhanes\data" ;

 

 

Step 2: Save to a SAS-Accessible Library

Use the PROC COPY statement to save the extracted datasets from the transport file to the C:\NHANES\DATA folder.  Use IN to denote the folder where the dataset is temporarily being stored and use OUT to denote the library where the dataset will be saved on your computer in a SAS-accessible format. 

This procedure must be run for each of the downloaded data files.  The output folder is always the same but the input file varies for each run.

 

Program to Save an Extracted Data File

Sample Code

proc copy in =XPDB out =NH;
run ;

proc copy in =XPDC out =NH;
run ;

proc copy in =XPIB out =NH;
run ;

proc copy in =XPTB out =NH;
run ;

proc copy in =XPTC out =NH;
run ;

proc copy in =XPIC out =NH;
run ;

proc copy in =XPOC out =NH;
run ;

proc copy in =XPSA out =NH;
run ;

proc copy in =XPSB out =NH;
run ;

proc copy in =XPSC out =NH;
run ;

proc copy in =XPSD out =NH;
run ;

 

Step 3: Check Results

To check the results of your program, open Windows Explorer and go to your C:\NHANES\DATA folder. You should see the downloaded files in that folder.  You now have these files saved to your computer. 

You can also run a proc contents statement in SAS to check that your dataset contains the correct number of observations and variables, based on the documentation.

 

Program to Check Datasets Contents

Sample Code

proc contents data = "C:\NHANES\DATA\demo_b" ;
run ;

proc contents data = "C:\NHANES\DATA\demo_c" ;
run ;

proc contents data = "C:\NHANES\DATA\drxiff_b" ;
run ;

proc contents data = "C:\NHANES\DATA\dr1tot_b" ;
run ;

proc contents data = "C:\NHANES\DATA\dr1tot_c" ;
run ;

proc contents data = "C:\NHANES\DATA\dr1iff_c" ;
run ;

proc contents data = "C:\NHANES\DATA\osq_c";
run ;

proc contents data = "C:\NHANES\DATA\dsq1_c" ;
run ;

proc contents data = "C:\NHANES\DATA\dsq2_c" ;
run ;

proc contents data = "C:\NHANES\DATA\DSPI" ;
run ;

proc contents data = "C:\NHANES\DATA\DSII" ;
run ;

Output of Program

Click here to view program output

Note that datasets saved in this SAS-accessible library can be used between SAS sessions.  This is in contrast to a dataset used within a SAS session, which is deleted when the session ends.

 

 

close window icon Close Window to return to module page.