Using Stata's -infix- Command

-infix- is a command designed to read fixed-format data into Stata. Fixed-format datafiles are files in which each variable is assigned a certain number of columns that contain values for a variable, with one column recording one letter or number that is part of that value (unlike a spreadsheet column that contains the value in its entirety). The data are thus recorded on records or rows of a particular length (e.g. old punch cards that could only hold a certain amount of data for each row). -infix- is designed to deal with such files and has more options than -insheet-, which is better-suited for reading in delimited ASCII text files.

Example - Fixed-Format Dataset:



Click here for full-size image

These data have not been formatted by a spreadsheet program. Instead, they are recorded in a certain number of columns. In this case, one column is one character-length wide.

To use -infix- you must know how many columns wide each variable is. This information is usually contained in the data codebook. To read data into Stata using -infix-, you simply type infix followed by the first variable name and the column range. Notice that you pair the variable name with the column numbers in which its values are recorded for each variable you want to read in, and that you do this all in one -infix- command. The filename you specify is the original data file that you want Stata to read. The syntax for the command is along the lines of the following:

infix variablename1 #ofcolumns variablename2 #ofcolumns ... using filename

Example:
infix age 1-3 height 4-6 weight 6-10 using "C:/patrons/username/example.txt"

Note that the command syntax is all in one line. Also keep in mind that Stata assumes that the variables you are creating are numeric. If any of your variables contain string values (letters or symbols) then you must specify this in the command by inserting "str" in front of the variable name that contains string values. If you do not do so, Stata will be most unhappy and will not hesitate to let you know this is the case by giving you an error message. So if you enter

infix age 1-3 height 4-6 weight 6-10 state 11-13 using "C:/patrons/username/example.txt"

Stata will return an error message because the variable "state" is a string variable:



Click here for full-size image

If you open up your data in the Data Browser, you will also see that Stata did not record the string values for that variable:



Click here for full-size image

To avoid this difficulty, be sure to include the 'str' syntax in front of any string variable:

infix age 1-3 height 4-6 weight 6-10 str state 11-13 using "C:/patrons/username/example.txt"

Then, when we look at our data in the Data Browser, we will see that Stata has read in all of the variables correctly, including the string "state" variable":



Click here for full-size image

Notice that when using the -infix- command, you specify the variable names in the actual command, so -infix- will always include the variables names as you specify them.

-infix- also allows the use of what is called a data dictionary, which is a file designating the locations, names, formats, and variable/value labels for variables in a file. This option is somewhat complicated, however, so we will not cover it here.

Other Means for Getting Data in Stata:

If your data are not in a fixed-format ASCII file, there are other methods for reading your data into Stata format:

Cutting and pasting data directly into Stata

Using StatTransfer

Using Stata's -insheet- command

Converting ICPSR Data in ASCII Format into Stata Data Files




Data Analysis

Page adapted from Electronic Data Center, Emory University Libraries
Original text by Amy Yuen