You can use the PROC IMPORT statement to quickly import data from a text file into SAS.
This procedure uses the following basic syntax:
/*import data from text file called data.txt*/ proc import out=my_data datafile="/home/u13181/data.txt" dbms=dlm replace; getnames=YES; run;
Here’s what each line does:
- out: Name to give dataset once imported into SAS
- datafile: Location of text file to import
- dbms: Format of file being imported (dlm assumes spaces are used as delimiters)
- replace: Replace the file if it already exists
- getnames: Use first row as variable names (Set to NO if first row does not contain variable names)
The following example shows how to use this syntax in practice.
Example: Import Text File into SAS
Suppose we have the following text file called data.txt:
We can use the following code to import this dataset into SAS and call it new_data:
/*import data from text file called data.txt*/ proc import out=new_data datafile="/home/u13181/data.txt" dbms=dlm replace; getnames=YES; run; /*view dataset*/ proc print data=new_data;
The data shown in the SAS output matches the data shown in the text file.
Note #1: We used getnames=YES when importing the file since the first row of the text file contained variable names.
Note #2: You can find the complete documentation for the PROC IMPORT statement here.
The following tutorials explain how to perform other common tasks in SAS: