Starting with Raw Data: Beyond the Basics |
Sometimes you need to read a record, and hold that record in the input buffer while you test for a specified condition before a decision can be made about further processing. As an example, the ability to hold a record so that you can read from it again, if necessary, is useful when you need to test for a condition before SAS creates an observation from a data record. To do this, you can use the trailing at-sign (@).
For example, to create a SAS data set that is a subset of a larger group of records, you might need to test for a condition to decide if a particular record will be used to create an observation. The trailing at-sign placed before the semicolon at the end of an INPUT statement instructs SAS to hold the current data line in the input buffer. This makes the data line available for a subsequent INPUT statement. Otherwise, the next INPUT statement causes SAS to read a new record into the input buffer.
You can set up the process to read each record twice by following these steps:
Use a trailing @ at the end of the INPUT statement to hold the record in the input buffer for the execution of the next INPUT statement.
Use an IF statement on the portion that is read in to test for a condition.
If the condition is met, use another INPUT statement to read the remainder of the record to create an observation.
If the condition is not met, the record is released and control passes back to the top of the DATA step.
For example, the health and fitness club data contains information about all members. This DATA step creates a SAS data set that contains only members of the red team:
data red_team; input Team $ 13-18 @; 1 if Team='red'; 2 input IdNumber 1-4 StartWeight 20-22 EndWeight 24-26; 3 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 . ; 4 proc print data=red_team; title 'Red Team'; run;
In this DATA step, these actions occur:
The following output shows the resulting data set:
Subset Data Set Created with Trailing @
Red Team 1 Id Start End Obs Team Number Weight Weight 1 red 1023 189 165 2 red 1219 210 192 3 red 1078 127 118
Copyright © 2012 by SAS Institute Inc., Cary, NC, USA. All rights reserved.