Reading in NHAMCS data, 2003-2009

New Contributor
Posts: 4

Reading in NHAMCS data, 2003-2009

Hello all,


I am wondering if anyone knows how to input NHAMCS data into SAS. For a project, I am to reproduce Table 1 from the following article:


I am having trouble inputting the dataset, and figuring out how to merge 2003-2009 data. The SAS input, format, and label codes are located here for all years:


Does anyone know what code or macros I would have to use? I really have no idea where to start. Thanks in advance!







Super User
Posts: 11,114

Re: Reading in NHAMCS data, 2003-2009

[ Edited ]

It appears that CDC is being its typically partially helpful self.

You will need to download the data sets, which are apparently elsewhere. There is a chance that there may be SAS data as well as text files to read. if so, get the SAS data sets. Otherwise you will need to write a data step to read the text versions. That would be something like:

Libname YourLib "your path to your library";

filename Data2009 "path to the 2009 data";

data yourlib.data2009;
   infile data2009 <appropriate options for LRECL=10000 or similar likely needed>;
   <The code from the 09INP.txt file goes here OR an %INCLUDE pointing to that file>
  <the 09LAB.txt or an %include goes here>
run; The FOR.TXT files have formats, which will also document the values of the variable.
And a section of code to associate variables with the format. That part of the code could
be in the data step to read the file.

BEFORE you combine any data sets you need to make sure that the coding of any variables in common is the same. If they aren't you'll have to account for that.



The basic approach would be to APPEND the data&colon;


   set yourlib.data2003 yourlib.data2004 ... yourlib.data2009;



HOWEVER you still have at least one major stumbling block: Weights. I do not see a variable that looks like the final weight to use for analysis. Also depending on the sample type (pretty much anything other than a simple random sample) you need the sample design and will almost certainly need to use the SURVEY procs, Surveyfreq, Surveymeans, surveyreg, surveylogisitic for analysis.

New Contributor
Posts: 4

Re: Reading in NHAMCS data, 2003-2009

Thank you so much! This helped alot. I currently have this code (a macro to run through 2003-2009), but the SAS log indicates that, for example:

WARNING: Apparent symbolic reference YEARFOR not resolved.
WARNING: Physical file does not exist, E:\BIS 679\NHAMCS\Formats\ed&yearfor.txt.
ERROR: Cannot open %INCLUDE file E:\BIS 679\NHAMCS\Formats\ed&yearfor.txt.

Clearly, my format for reading in the different years' files is incorrect. Do you know what I am doing wrong? Thanks again!
New Contributor
Posts: 4

Re: Reading in NHAMCS data, 2003-2009

Sorry, I forgot to include the code

libname nh "E:\BIS 679\NHAMCS";
%Let dir=E:\BIS 679\;

%macro dataset;
  %do i=03 %to 09;
  	%let year=&i;
	%include "&dir.NHAMCS\Formats\ed&yearfor.txt";
*import dataset;
	data ed&year; 
	infile "&dir.NHAMCS\ed&.txt" DSD MISSOVER LRECL=999;
	%include "&dir.NHAMCS\Inputs\ed&yearinp.txt";
	%include "&dir.NHAMCS\Labels\ed&yearlab.txt"; 
%mend dataset;
%dataset; run;
Super User
Super User
Posts: 6,842

Re: Reading in NHAMCS data, 2003-2009

You are referencing macro variables that you did not define.

Most likely because you did not add a period to let SAS know where the stop the search for the macro variable name.

So this line 


%include "&dir.NHAMCS\Inputs\ed&yearinp.txt";

Should probably be changed to this.

%include "&dir.NHAMCS\Inputs\ed&year.inp.txt";


Respected Advisor
Posts: 4,131

Re: Reading in NHAMCS data, 2003-2009

Just as an addendum:

This readme tells you pretty much what to do:


You've got the data as self-extracting zip files here (from the readme file):

This data are text files.


You then got the SAS code to read this data into SAS here:


There are also quite a few other readme.txt files explaining per year what to do for this year. You need to read these "readme".

New Contributor
Posts: 4

Re: Reading in NHAMCS data, 2003-2009

Thanks for your reply, Patrick!
Ask a Question
Discussion stats
  • 6 replies
  • 1 like
  • 4 in conversation