Lesson 3.2: Importing data from CSV file
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.
Below is the csv file that I am going to read into SAS:
PROC IMPORT DATAFILE="C:\test.csv"
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.
Let’s import the same file using the infile statement.
DELIMITER = ","
INPUT Name $ Score;
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!