You can use the INFILE statement to import data from a file into a dataset in SAS.
This statement uses the following basic syntax:
data my_data;
infile '/home/u13181/bball_data.txt' dlm=' ' dsd missover firstobs=2;
input team $ position $ points assists;
run;
Here’s what each line does:
- data: Name to give dataset once imported into SAS
- infile: Location of file to import
- dlm: The delimiter that separates values in the file
- dsd: Treat two consecutive delimiters as a missing value
- missover: Assume each line in file represents one observation
- firstobs: Which line in file to consider the first line with observations
The following example shows how to use this function in practice.
Example: How to Use INFILE Statement in SAS
Suppose we have the following text file called bball_data.txt:
We can use the following code to import this file into a SAS dataset called my_data:
/*import data from txt file into SAS dataset*/
data my_data;
infile '/home/u13181/bball_data.txt' dlm=' ' dsd missover firstobs=2;
input team $ position $ points assists;
run;
/*view dataset*/
proc print data=my_data;
By using the INFILE statement, we were able to successfully import the values from the text file into a dataset.
Notice how we used the following arguments:
- infile: Specified where the file was located.
- dlm: Specified that the values in the file were separated by spaces.
- dsd: Specified that two consecutive delimiters should be treated as a missing value. This came in handy with the missing value in the points column of the first row.
- missover: Specified that each line in the file represented one observation.
- firstobs: Specified that the first observation was located on the second row of the file.
- input: Specified the names to give to the columns in the dataset.
By using each of these arguments, we were able to successfully import the text file into a dataset with the correct format.
Additional Resources
The following tutorials explain how to perform other common tasks in SAS:
How to Import Text Files into SAS
How to Import CSV Files into SAS
How to Import Excel Files into SAS