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!
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;
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;
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):
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.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.