How do you use the INFILE statement in SAS? 2

How do you use the INFILE statement in SAS?

The INFILE statement in SAS is used to specify the location and format of an external data file that needs to be imported into the SAS program. It allows the user to read data from various sources such as text, Excel, or database files. The syntax of the INFILE statement includes the name of the file, the data format, and any additional options. This statement is essential for data manipulation and analysis in SAS, as it allows for seamless integration of external data with the SAS program. By correctly using the INFILE statement, users can efficiently import and process large volumes of data, ultimately enhancing the accuracy and efficiency of their analyses.

Use the INFILE Statement in SAS (With Example)


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=' ' dsdmissoverfirstobs=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=' ' dsdmissoverfirstobs=2;
    input team $ position $ points assists;
run;

/*view dataset*/
proc printdata=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.

How to Import Text Files into SAS

Cite this article

stats writer (2024). How do you use the INFILE statement in SAS?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-do-you-use-the-infile-statement-in-sas/

stats writer. "How do you use the INFILE statement in SAS?." PSYCHOLOGICAL SCALES, 23 Jun. 2024, https://scales.arabpsychology.com/stats/how-do-you-use-the-infile-statement-in-sas/.

stats writer. "How do you use the INFILE statement in SAS?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-do-you-use-the-infile-statement-in-sas/.

stats writer (2024) 'How do you use the INFILE statement in SAS?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-do-you-use-the-infile-statement-in-sas/.

[1] stats writer, "How do you use the INFILE statement in SAS?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.

stats writer. How do you use the INFILE statement in SAS?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.

Download Post (.PDF)
Slide Up
x
PDF
Scroll to Top