Professional Documents
Culture Documents
# input styles:
1 list
2 column
3 formatted
SAS has now reached the end of the DATA step, and the program automatically d
the following:
writes the rst observation to the data set
loops back to the top of the DATA step to begin the next iteration
increments the _N_ automatic variable by 1
resets the _ERROR_ automatic variable to 0
except for _N_ and _ERROR_, sets variable values in the program data vector to
missing values, as the following gure shows
ce or delimiter
data
he defaultbehavior, a blank eld causes the variable names and values to become
s a longer value when it writes the value to the program data vector. (To read a character
e a LENGTH statement
be read without an informat)
er
t characters
nput
Character Data That Contains Embedded Blanks
rsand format modier (&) for reading variable with a space in between.
ata=club2;
Club Members;
In the next example, SAS reads data lines by using formatted input wit
column-pointer control:
In the following pr
data january_sales;
input Item $10.
+6 Amount comma5.;
datalines;
trucks 1,382
datalines;
vans 1,235
trucks 1,382
sedans 2,391
vans 1,235
;
sedans 2,391
After SAS reads the rst value for the variable Item, the pointer is left in the ;
position, column 11. The absolute column-pointer control, @17, then directs The data in these two pro
to move to column 17 in the input buffer. Now, it is in the correct position toinstruct the pointer to mo
value for the variable Amount.
specications; with forma
together with pointer cont
n the following program, the relative column-pointer control, +6, instructs the
ointer to move six columns to the right before SAS reads the next data value.
ata january_sales;
+6 Amount comma5.;
rucks 1,382
ans 1,235
edans 2,391
he data in these two programs is aligned in columns. As with column input, you
nstruct the pointer to move from eld to eld. With column input you use column
pecications; with formatted input you use the length that is specied in the informat
ogether with pointer controls.
To read from a record twice, you must prevent SAS from automatically placing a new
record into the input buffer when the next INPUT statement executes. Use of a trailing
@ in the rst INPUT statement serves this purpose.
line-hold speciers that enable you to hold a record in the input buffer for further
processing.
data red_team;
input Team $ 13-18 @;
if Team=red;
input IdNumber 1-4 StartWeight 20-22 EndWeight 24-26;
datalines;
1023 David red 189 165
1049 Amelia yellow 145 124
1219 Alan red 210 192
1246 Ravi yellow 194 177
1078 Ashley red 127 118
1221 Jim yellow 220 .
;
proc print data=red_team;
title Red Team;
run;
Using the
Sometimes you may need to create multiple observations from a single record o
data. One way to tell SAS how to read such a record is to use the other line-hold
one of two
specier, the double trailing at-sign (@@ or double trailing @). The double tra
er for further
not only prevents SAS from reading a new record into the input buffer when a ne
INPUT statement is encountered, but it also prevents the record from being relea
when the program returns to the top of the DATA step. (Remember that the trail
does not hold a record in the input buffer across iterations of the DATA step.)
data body_fat;
input Gender $ PercentFat
@@;
datalines;
m 13.3 f 22
m 22 f 23.2
m16 m12
;
proc print data=body_fat;
title Results of Body Fat Testing;
run;
data club2;
input IdNumber 1-4;
input;
input StartWeight 1-3 EndWeight 5-7;
datalines;
1023 David Shaw
red
189 165
1049 Amelia Serrano
yellow
145 124
1219 Alan Nance
red
210 192
1246 Ravi Sinha
yellow
194 177
1078 Ashley McKnight
red
127 118
1221 Jim Brown
yellow
220 .
;
proc print data=club2;
title Weight Club Members;
run;
data club2;
input IdNumber 1-4 / / StartWeight 1
datalines;
1023 David Shaw
red
189 165
1049 Amelia Serrano
yellow
145 124
1219 Alan Nance
red
210 192
1246 Ravi Sinha
yellow
194 177
1078 Ashley McKnight
red
127 118
1221 Jim Brown
yellow
220 .
;
proc print data=club2;
title Weight Club Members;
run;
McKnight
ata=club2;
Club Members;
Using
data club2;
input #2 Team $ 1-6 #1 Name $ 6-23 IdNumber
#3 StartWeight 1-3 EndWeight 5-7;
datalines;
1023 David Shaw
red
189 165
1049 Amelia Serrano
yellow
145 124
1219 Alan Nance
red
210 192
1246 Ravi Sinha
yellow
194 177
1078 Ashley McKnight
red
127 118
1221 Jim Brown
yellow
220 .
;
proc print data=club2;
title Weight Club Members;
run;
ointer control
er Weight Weight
46 194 177
1078 127 118
Problem Solving: When an Input Record Unexpectedly Does Not Have Enough Values
Understanding the Default Behavior
When a DATA step reads raw data from an external le, problems can occur when
SAS encounters the end of an input line before reading in data for all variables
specied in the input statement. This problem can occur when reading variable-length
records and/or records containing missing values.
The following is an example of an external le that contains variable-length records:
3
44
555
This DATA step uses the numeric informat 5. to read a single eld in each record of
raw data and to assign values to the variable TestNumber:
data numbers;
inle your-external-le;
input TestNumber 5.;
run;
proc print data=numbers;
title Test DATA Step;
run;
The DATA step reads the rst value (22). Because the value is shorter than the 5
characters expected by the informat, the DATA step attempts to nish lling the value
with the next record (333). This value is entered into the PDV and becomes the value of
the TestNumber variable for the rst observation. The DATA step then goes to the next
record, but encounters the same problem because the value (4444) is shorter than the
value that is expected by the informat. Again, the DATA step goes to the next record,
reads the value (55555), and assigns that value to the TestNumber variable for the
second observation.
The following output shows the results. After this program runs, the SAS log
contains a note to indicate the places where SAS went to the next record to search for
data values.
Output
Test DATA Step 1
Test
Obs Number
1 333
2 55555
The
following example demonstrates the use of the TRUNCOVER statement:
3 4444
4 55555
ER statement: