x
Need help studying for the new
SAS Certified Specialist Exam?
SAS Certified Specialist Exam?
Get access to:
- Two Full Certificate Prep Courses
- 300+ Practice Exercises
|
Lesson 3.2: Importing data from CSV file
IMPORTANT! This tutorial is updated on March 9, 2017. See the latest training on importing CSV files into SAS. This section will explain how to import data from a csv file. There are two ways to do this: Proc Import or Data Step with an Infile Statement. 2. CSV
Example Below is the csv file that I am going to read into SAS: PROC IMPORT DATAFILE="C:\test.csv" OUT=SASCrunch DBMS=csv REPLACE; GETNAMES=Yes; RUN; As you can see, it is very similar to importing data from an excel file. You just need to change the DBMS to ‘csv’ and use the correct extension (csv) on the data file.
Now, let’s take a look at the dataset that we have just created: Another way to import a csv file into SAS is to use the infile statement. The infile statement allows more flexibility in terms of assigning attributes to the data imported. Attributes will be explained in later lessons.
Example Let’s import the same file using the infile statement. DATA SASCrunch; INFILE "C:\test.csv" DELIMITER = "," MISSOVER DSD FIRSTOBS=2 LRECL=32767; INPUT Name $ Score; RUN; Let's break down each statements above: (1) INFILE statement identifies the path to the external datafile. (2) DELIMITER identifies which delimiter to use. In a CSV (Comma-Separated Value) file, a delimiter will be, well obviously, a comma! (3) <IMPORTANT> MISSOVER and DSD are two important function when using the INFILE method to import the data. They allow SAS to read a missing value rather than skipping it. If these two functions are not specified, a missing value will could cause tremendous errors when importing the data! (4) FIRSTOBS=2 tells SAS to start reading the data from the second row. (5) <Optional> LRECL assigns the Logical Record Length to the value captured. The default value is 256 and it is normally sufficient to capture most of the data. However, for a long string of text, a larger value can be assigned through the LRECL= option. The range for lrecl is 1 to 32767. DONE! You have learned how to import data from a CSV file. Again, you do not have to memorize the codes. Copy us and modify the path whenever you need to import a csv file! |
|