Reading Standard Data with List Input

The Basics of List Input

Syntax, INPUT statement using list input:
INPUT variable <$>;
  • variable specifies the variable whose value the INPUT statement is to read.
  • $ specifies that the variable is a character variable.
Suppose you have an external data file like the one shown below. The file, which is referenced by the fileref Creditc, contains the results of a survey on the use of credit cards by males and females in the 18-39 age range.
Figure 18.2 Raw Data File Creditc
External file that contains free-format raw data in columns that are separated by blank spaces.
You need to read the data values for the following:
  • gender
  • age
  • number of bank credit cards
  • bank card use per month
  • number of department store credit cards
  • department store card use per month
List input might be the easiest input style to use because, as shown in the INPUT statement below, you simply list the variable names in the same order as the corresponding raw data fields. Remember to distinguish character variables from numeric variables with dollar signs.
input Gender $ Age Bankcard FreqBank Deptcard FreqDept;
By default this input style does not specify column locations. Therefore, in order to read data using list input, the following must be true:
  • All fields must be separated by at least one blank or other delimiter.
  • Fields must be read in order from left to right.
  • Fields cannot be skipped or re-read.
Note: Blank is the default delimiter in list input.

Processing List Input

It is important to remember that list input causes SAS to scan the input lines for values rather than reading from specific columns. When the INPUT statement is submitted for processing, the input pointer is positioned at column 1 of the raw data file.
data sasuser.creditsurvey; 
   infile creditc;
   input Gender $ Age Bankcard FreqBank Deptcard 
         FreqDept; 
run;
SAS reads the first field until it encounters a blank space. The blank space indicates the end of the field, and the data value is assigned to the program data vector for the first variable in the INPUT statement.
Next, SAS scans the record until the next nonblank space is found, and the second value is read until another blank is encountered. Then the value is assigned to its corresponding variable in the program data vector.
This process of scanning ahead to the next nonblank column, reading the data value until a blank is encountered, and assigning the value to a variable in the program data vector continues until all of the fields have been read and values have been assigned to variables in the program data vector.
Program Data Vector.
When the DATA step has finished executing, you can display the data set with the PRINT procedure. The following code produces the output below.
proc print data=sasuser.creditsurvey; 
run;
Figure 18.3 Data Set Displayed by PROC PRINT
Data set displayed by the PRINT procedure

Reading a Range of Variables

When the variable values in the raw data file are sequential and are separated by a blank (or by another delimiter), you can specify a range of variables in the INPUT statement. This is especially useful if your data contains similar variables, such as the answers to a questionnaire.
For example, the following INPUT statement creates five new numeric variables and assigns them the names Ques1, Ques2, Ques3, and so on. You can also specify a range in the VAR statement with the PROC PRINT step to list a range of specific variables.
data sasuser.phonesurvey; 
   infile phonsurv; 
   input IDnum $ Ques1-Ques5; 
run; 
proc print data=sasuser.phonesurvey; 
   var ques1-ques3; 
run;
Figure 18.4 Raw Data File with Sequential Variables
Input statement that creates five new numeric values and assigns them the names Ques1, Ques2, Ques3, and so on.
Figure 18.5 Output Using Sequential Variable Names
Output that shows new numeric values with the names Ques1, Ques2, Ques3.
If you specify a range of character variables, both the variable list and the $ sign must be enclosed in parentheses.
data sasuser.stores; 
   infile stordata;
   input Age (Store1-Store3) ($); 
run; 
proc print data=sasuser.stores; 
run; 
Note: You can also specify a range of variables using formatted input. If you specify a range of variables using formatted input, both the variable list and the informat must be enclosed in parentheses, regardless of the variable's type.
data sasuser.scores;  
   infile group3;  
   input Age (Score1-Score4) (6.); 
run;
Last updated: January 10, 2018
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.119.170