Reading Free-Format Data | |
Using List Input |
Working With Delimiters
Most free-format data fields are clearly separated by blanks and are easy to imagine as variables and observations. But fields can also be separated by nonblank delimiters, such as commas, as shown below. |
Raw Data File Credit
MALE,27,1,8,0,0
FEMALE,29,3,14,5,10
FEMALE,34,2,10,3,3
MALE,35,2,12,4,8
FEMALE,36,4,16,3,7
MALE,21,1,5,0,0
MALE,25,2,9,2,1
FEMALE,21,1,4,2,6
MALE,38,3,11,4,3
FEMALE,30,3,5,1,0
When characters other than blanks are used to separate the data values, you can specify the field delimiter by using the DLM= option in the INFILE statement to specify one or more characters (up to 200) to be read as delimiters. The list of characters must be enclosed in quotation marks. |
The following program creates the output shown below. |
data perm.survey; infile credit dlm=','; input Gender $ Age Bankcard FreqBank |
Obs | Gender | Age | Bankcard | FreqBank | Deptcard | FreqDept |
1 | MALE | 27 | 1 | 8 | 0 | 0 |
2 | FEMALE | 29 | 3 | 14 | 5 | 10 |
3 | FEMALE | 34 | 2 | 10 | 3 | 3 |
4 | MALE | 35 | 2 | 12 | 4 | 8 |
5 | FEMALE | 36 | 4 | 16 | 3 | 7 |
6 | MALE | 21 | 1 | 5 | 0 | 0 |
7 |
MALE | 25 |
2 |
9 |
2 |
1 |
8 |
FEMALE | 21 |
1 |
4 |
2 |
6 |
9 |
MALE | 38 |
3 |
11 |
4 |
3 |
10 | FEMALE | 30 | 3 | 5 | 1 | 0 |
It's important to note that the field delimiter must
not be a character that occurs in a data value. For example,
this raw data file contains values for LastName and
Salary . Notice that the values for Salary contain
commas. |
BROWN
24,456.09
JOHNSON
25,467.17
McABE
21,766.36
If the field delimiter is also a comma, the fields are identified incorrectly, as shown below. |
BROWN,24,456.09
JOHNSON,25,467.17
McABE,21,766.36
Obs | LastName | Salary |
1 | BROWN | 24 |
2 | JOHNSON | 25 |
3 | McABE | 21 |
Copyright © 2002 SAS Institute Inc.,
Cary, NC, USA. All rights reserved.