1 / 17

The INFILE Statement

The INFILE Statement. Reading files into SAS from an outside source: A Very Useful Tool!.

colby
Download Presentation

The INFILE Statement

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The INFILE Statement Reading files into SAS from an outside source:A Very Useful Tool!

  2. Often the data you want to use are in an outside source, such as an Excel spreadsheet or text editor. You can import these data into SAS with the INFILE statement, rather than typing in all of the data by hand. In addition to coming from different sources, the data may be delimited by spaces, tabs, or commas. There are codes to tell SAS what type of data it will be reading into the Editor.

  3. Basic SAS Code The program you will use to read in a data file will follow this basic format, with slight variations: DATAname of data set; INFILE‘C:\location and name of file’ ; INPUTvar1 var2 etc.; RUN; Let’s try a couple of examples.

  4. Space delimited data with no missing values and no labels This will be the simplest type of data set you could encounter. Download the file called relief.txt, and save it to your hard drive where you will be able to easily locate it. Open SAS and type the following code into your Editor window, keeping in mind that the path to the file may be different on your computer:

  5. Sample Code DATA relieftime; INFILE‘C:\Documents and Settings\My Documents\relief.txt’ ; INPUT group $ time; TITLE‘Reading in Data from an External File’ ; PROC PRINT DATA = relieftime; RUN;

  6. The first line creates a data set called relieftime. • The INFILE statement tells SAS where to find the dataset. • The third line labels the two variables in the data set (just as you have seen before). • The TITLE statement gives anything you print a title. • The PRINT statement prints your data in the Output window, so that you can make sure you did not lose any data. • Try running the program in SAS. Were you successful? Check your log for any ERROR messages and try running the program again.

  7. Check your log for valuable information. It tells you how many observations were read into SAS, and you can see if you are missing any data.

  8. Output of PROC PRINT

  9. Comma delimited data with missing values and labels in the first row You will also encounter comma-separated value (csv) files, which are often created using Excel. To read in this type of data, you will need to use the DSD option in your SAS code. Save the file about blood pressure (bp.csv) to your hard drive, then type the following code into your Editor window, keeping in mind the path to your file may be different from the example. Before you read the file into SAS, open it and look at the format. Notice that first row contains variable names and there are a few missing values. We will address that using various SAS statements.

  10. SAS Code • dlm [delimiter] = ‘,’ tells SAS that the data is delimited by commas. • dsd (delimiter-sensitive data) allows SAS to read in a .csv file. This also allows SAS to recognize the missing data. • firstobs = 2 tells SAS to start reading the data from the second row (because the first row contains variable labels).

  11. SAS Log

  12. PROC PRINT Output

  13. Tab delimited Data Other types of data may be tab delimited, and this is specified in the dlm option. We use the statement dlm = ’09’x. (’09’x is the hexadecimal representation of the tab character, but you don’t need to know that; it’s basically the technical way of specifying TAB as the delimiter.) Download the file aplastic.txt, open it to see that the data begin on the first line, then read the file into SAS.

  14. SAS Code for Tab Delimited Data • After running this program, check your SAS log. • Are there any Errors or Warnings? • If not, look at your Output. • Is any data missing?

  15. SAS Log

  16. PROC PRINT Output

  17. Conclusions These are just a few examples of the most common types of files you may encounter when reading in an external data file into SAS using the INFILE statement. This skill will be valuable for future tutorials and homework assignments. Here are some helpful links for more info: UCLA Academic Technology Services University of Michigan Software Help University at Albany CSDA

More Related