📌 How to Read Data in Fixed Columns Using SAS
Working with fixed-width (or fixed-column) text files is a common task in SAS programming, especially when importing data from legacy systems or mainframes. Unlike delimited files (like CSV), fixed-column files store data in predefined character positions. In this guide, you'll learn how to read such data files effectively using SAS.
📂 What is a Fixed-Width File?
A fixed-width file is a plain-text file where each data field occupies a specific number of characters. There are no delimiters like commas or tabs; instead, the position and length of each field are predefined.
Example:
- ID: 1–3
- Name: 4–11
- Age: 12–13
- Gender: 14–19
📥 Reading Fixed Column Data Using INFILE
and INPUT
SAS uses the combination of INFILE
and INPUT
statements to read fixed-column data.
✅ Syntax:
$
indicates a character variablestart-end
indicates the column positions
🧪 Example: Reading Employee Data
Let’s say you have a file named employees.txt
with the following structure:
To read this in SAS:
🔍 Explanation:
ID
is numeric (columns 1 to 3)Name
is character (columns 4 to 11)Age
is numeric (columns 12 to 13)Gender
is character (columns 14 to 19)
📎 Tips for Handling Fixed-Width Files
- Ensure each line in the text file has a uniform width.
- Use
$
in theINPUT
statement to define character fields. - Always check column positions carefully—especially if fields are padded with spaces.
- Use
LRECL
(logical record length) inINFILE
if the line length is long:
🔁 Alternative: Using COLUMN POINTERS (
Formatted Input Method)
You can also use column pointers (@
) to specify exact starting positions for each field.
@n
tells SAS where to begin reading.- The numeric formats like
3.
or8.
specify field lengths.
More on Reads data from fixed columns
- Reads character and numeric data including standard and nonstandard numerical values, such as numbers with dollar signs and commas
- Reads dates in different formats, not like column input which can only read dates as character values
- It is the most common and powerful of all the input methods
- Any time you have nonstandard data in fixed columns, you should consider using formatted input to read the file.
Let's see More Example -
Suppose we have this raw data stored in any flat file -
1234567890123456789012 ---- Columns indicator, not part of the data
Tim M14510/21/1978
Sara 13009/20/1964
Mike M18011/23/1965
LauraF13011/06/1980
Sean M16704/07/2000
The variables information is listed as below.
There are 4 variables in the data -
Name: occupies total five columns, column 1-5
Gender: occupies total one column, column 6
Weight: occupies three columns, column 7-9
DOB: occupies ten columns, column 10 - 19
data Form_input;
infile "/folders/myfolders/DATA_column.txt";
input
@1 Name $ 5.
@6 Gender $ 1.
@7 Weight 3.
@10 DOB mmddyy10.;
Run;
proc print data=sdata_column;run;
Explanation -
1. We Used @ to specify the starting position of every variable
2. Also used SAS Informats
Informats are built-in instructions that tell SAS how to read a data value. The choice of which informat to use is determined by the data.For standard numeric and character values, two basic informats are w.d and $w.
The w.d Informat: reads standard numeric values. The w tells SAS how many columns to read. The optional d tells SAS that there is a decimal point in the value, the decimal point counts as one of the columns to be read. For example, the variable Weight containing 3 digits values (such as 145 lbs) has an informat of 3. ; Another example, value 2.1 takes up 3 column spaces, 2 digits + 1 decimal point, so it has an informat of 3.1
For date: the MMDDYY10. informat tells SAS that the date you are reading is in the mm/dd/yyyy form, 10 means this form takes up 10 columns including 8 digits and 2 slash signs. For example, the variable DOB has values like 10/21/1978. SAS reads the date and converts the value into a SAS date. SAS stores dates as numeric values equal to the number of days from January 1, 1960. So, if you read the value 01/01/1960 with the MMDDYY10. informat, SAS stores a date value of 0. The date 01/02/1960 read with the same informat would result in a date value of 1, and so forth.
That’s why if you look at the output of above program than DOB displays as some numbers instead of dates, TO Display DOB as Date Value we need to use the Format.
Suppose we have this raw data stored in any flat file -
1234567890123456789012 ---- Columns indicator, not part of the data
Tim M14510/21/1978
Sara 13009/20/1964
Mike M18011/23/1965
LauraF13011/06/1980
Sean M16704/07/2000
The variables information is listed as below.
There are 4 variables in the data -
Name: occupies total five columns, column 1-5
Gender: occupies total one column, column 6
Weight: occupies three columns, column 7-9
DOB: occupies ten columns, column 10 - 19
data Form_input;
infile "/folders/myfolders/DATA_column.txt";
input
@1 Name $ 5.
@6 Gender $ 1.
@7 Weight 3.
@10 DOB mmddyy10.;
Run;
proc print data=sdata_column;run;
For standard numeric and character values, two basic informats are w.d and $w.
📊 Output Dataset
After reading the data, you can view the dataset using:
✅ Summary
Task SAS Statement Specify file location INFILE
Read fixed columns INPUT
with positionsRead character data Add $
after variable Use column positions Use ranges like 1-3
or pointers like @1
View dataset PROC PRINT
Task | SAS Statement |
---|---|
Specify file location | INFILE |
Read fixed columns | INPUT with positions |
Read character data | Add $ after variable |
Use column positions | Use ranges like 1-3 or pointers like @1 |
View dataset | PROC PRINT |
📌 Conclusion
Reading fixed-column data in SAS is straightforward once you understand the layout of the file. By using INFILE
, INPUT
, and positional referencing, you can efficiently convert raw text files into structured SAS datasets. This skill is especially important for data analysts working with legacy systems or industry-standard text data formats.
Search Tags:
SAS Fixed Column Input
Read Fixed Width Files in SAS
SAS INFILE Statement
SAS INPUT Statement
Column Pointer in SAS
SAS Text File Import
SAS Programming Basics
Importing Data in SAS
Legacy Data Processing SAS
SAS Tutorial for Beginners
0 Comments
If you have any doubt please comment or write us to - datahark12@gmail.com