Reading Data with INPUT
Prerequisites: Libraries and data sets
The INPUT statement describes how to read raw text into variables. So
far you've used list input: values separated by blanks.
The 8-character default
With list input, a character variable gets a length of 8 unless you say otherwise — longer values are cut off:
data people;
input name $ city $;
datalines;
Christopher Johannesburg
Ada London
;
run;
proc print data=people;
run;
Obs name city
1 Christop Johannes
2 Ada London
Fix it with a LENGTH statement before the INPUT:
data people;
length name $ 12 city $ 15;
input name $ city $;
datalines;
Christopher Johannesburg
Ada London
;
run;
proc print data=people;
run;
Obs name city
1 Christopher Johannesburg
2 Ada London
Missing values and delimiters
In list input, a period . means "missing". For comma-separated values,
use INFILE DATALINES with DSD, which also treats two commas in a row
as a missing value:
data scores;
infile datalines dsd;
input name $ math english;
datalines;
Ada,90,85
Alan,,70
Grace,78,
;
run;
proc print data=scores;
run;
Obs name math english
1 Ada 90 85
2 Alan . 70
3 Grace 78 .
Column and formatted input
For fixed-width data, give column positions — spaces inside values are then allowed:
data staff;
input name $ 1-12 dept $ 14-18 salary 20-24;
datalines;
Mary Jones Sales 52000
Li Wei IT 61000
;
run;
proc print data=staff;
run;
Obs name dept salary
1 Mary Jones Sales 52000
2 Li Wei IT 61000
Formatted input uses a pointer (@column) and an informat — a
rule for reading a value, such as date9. for dates or comma8. for
numbers with commas. The next lesson but one covers informats.
Several rows per line
A trailing @@ keeps reading the same line for the next observation:
data points;
input x y @@;
datalines;
1 2 3 4 5 6
;
run;
NOTE: SAS went to a new line when INPUT statement reached past the end of a line.
NOTE: The data set WORK.POINTS has 3 observations and 2 variables.
The first note is expected with @@ and harmless here.
Common mistakes
- Forgetting
$— SAS then tries to read text as a number, sets it to missing, and writesNOTE: Invalid data for name in line ...to the log. - Putting
LENGTHafterINPUT— by then the length is already set to 8, and SAS warns that the length can't be changed.