BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Monte1
Calcite | Level 5

I am using data file name SMQSHS_J from NHANES 2017-2018 Questionnaire Data on the CDC website. The file is downloaded as an XPT file. I am able to upload the file into SAS On Demand for Academics, but when I try to run it, the file just downloads into my computer again.

 

I have looked all around this site to figure out how to convert an xpt file into something that will allow me to read the data into SAS Studio, but nothing works. I prefer to convert it to a csv file, but I will take anything at this point.

 

I can't create a library out of it, so I am unable to code anything to read it in.

 

Help please!

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

The publish the NHANES data is SAS V5 Transport files. 

The one you are looking for is published here:  https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT

So to convert the XPORT file into a SAS dataset you just need to point a LIBREF at the file using the XPORT engine.  You can then reference the dataset directly or use PROC COPY to copy it somewhere.

 

Here is code to copy it to a WORK dataset.

filename SMQSHS_J url "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT" ;
libname  SMQSHS_J xport;

proc copy inlib=SMQSHS_J out=work;
run;

 

If you want to write it to a permanent dataset instead of a temporary WORK dataset then first make a libref and use that instead of WORK in the PROC COPY step.

 

So for example if your SAS/Studio session is connected to SAS running on a Unix machine and there exist a directory on that machine named /home/me/nhanes which you have permission to write into then you can change the code to:

filename SMQSHS_J url "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT" ;
libname  SMQSHS_J xport;

libname nhanes '/home/me/nhanes';
proc copy inlib=SMQSHS_J out=nhanes;
run;

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

The publish the NHANES data is SAS V5 Transport files. 

The one you are looking for is published here:  https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT

So to convert the XPORT file into a SAS dataset you just need to point a LIBREF at the file using the XPORT engine.  You can then reference the dataset directly or use PROC COPY to copy it somewhere.

 

Here is code to copy it to a WORK dataset.

filename SMQSHS_J url "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT" ;
libname  SMQSHS_J xport;

proc copy inlib=SMQSHS_J out=work;
run;

 

If you want to write it to a permanent dataset instead of a temporary WORK dataset then first make a libref and use that instead of WORK in the PROC COPY step.

 

So for example if your SAS/Studio session is connected to SAS running on a Unix machine and there exist a directory on that machine named /home/me/nhanes which you have permission to write into then you can change the code to:

filename SMQSHS_J url "https://wwwn.cdc.gov/Nchs/Nhanes/2017-2018/SMQSHS_J.XPT" ;
libname  SMQSHS_J xport;

libname nhanes '/home/me/nhanes';
proc copy inlib=SMQSHS_J out=nhanes;
run;

 

Monte1
Calcite | Level 5

1. Thank you so much you are amazing!
2. What is the difference between setting the information as a temporary dataset vs a permanent dataset?

3. Is there any way to import this information so that I can rename this info as IMPORT2 and also merge it with other data from the NHANES data? So something like this (this is from file slq_j of the NHANES data that was actually downloadable):

 

/* Generated Code (IMPORT) */
/* Source File: slq_j.csv */
/* Source Path: /home/u61005021 */
/* Code generated on: 4/10/22, 11:20 AM */
 
%web_drop_table(WORK.IMPORT1);
 
 
FILENAME REFFILE '/home/u61005021/slq_j.csv';
 
PROC IMPORT DATAFILE=REFFILE
DBMS=CSV
OUT=WORK.IMPORT1;
GETNAMES=YES;
RUN;
 
PROC CONTENTS DATA=WORK.IMPORT1; RUN;
 
 
%web_open_table(WORK.IMPORT1);

 

Tom
Super User Tom
Super User

When your SAS session ends the WORK datasets are deleted.  So if you only make a WORK dataset you have to make the dataset again when you restart SAS.  And if you use my example that means you have to download it again from the website.  With a permanent dataset you just need to point a libref at the directory with the dataset and you can start using the dataset in your code.

 

Why would you want to read a CSV version of the file when there is SAS dataset (or at least a SAS transport file) version of the dataset.  The only information a CSV file can have about how to define the variables is the optional header row.  With a SAS dataset the variables are already defined as proper type (numeric or character) and for character the proper length.  Also they can have labels and formats attached to them.

 

If you did want to read a CSV version of the file then write your own data step to read it instead of forcing SAS to guess how to name and define the variables.  The information about the variables is available on the website to guide you.

Monte1
Calcite | Level 5
Thank you for the clarification, Tom! I was able to make the dataset permanent with your help. Finally able to continue coding lol

Thanks again, I truly appreciate it!

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 2496 views
  • 1 like
  • 2 in conversation