This tutorial explains how to import CSV files into SAS, along with examples.
Syntax to import CSV file into SAS
You can use PROC IMPORT to import a CSV files into SAS. The syntax of PROC IMPORT is as follows:
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata.csv" DBMS=CSV REPLACE; GETNAMES=YES; RUN;
OUT
: Specify name of the dataset to be imported into SASDATAFILE
: File location of CSV file which you want to importDMBS
: Specify CSV FormatREPLACE
: Optional argument. If the file already exists, replace it.GETNAMES
: Optional argument. By default, it takes first row as variable names. If the file doesn't have a header,set GETNAMES=NO.
Suppose you have data in CSV file named customers.csv
. You can import it into SAS using the code below.
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata/customers.csv" DBMS=CSV REPLACE; GETNAMES=YES; RUN;
The code above uses the PROC IMPORT procedure in SAS to import a CSV file located at "/home/deepanshu88us0/mydata/customers.csv". The imported data will be stored in a dataset named "newdata". The DBMS option is set to CSV, telling SAS the file format is CSV. The REPLACE option is specified, which means that if a dataset with the same name already exists, it will be replaced. The GETNAMES option is set to YES, indicating that the first row of the CSV file contains variable names. It is by default so you can exclude this option if you want.
To view and print the dataset, you can use proc print data=newdata;
If your CSV file does not have header, you can use GETNAMES=NO
option in PROC IMPORT.
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata/customers.csv" DBMS=CSV REPLACE; GETNAMES=NO; RUN;
By default, SAS considers comma (,) as a delimiter which separates the values in the CSV file when importing using PROC IMPORT. To change delimiter, you can specify it in the DELIMITER=
option in PROC IMPORT. The following code uses semicolon (;) as a delimiter.
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata/customers.csv" DBMS=CSV REPLACE; DELIMITER=";"; RUN;
You can use the DATAROW=
option to import a CSV file from a specific row. The following code imports the CSV file from 5th row.
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata/customers.csv" DBMS=CSV REPLACE; DATAROW=5; RUN;
By default, PROC IMPORT makes decisions about the data type in SAS by examining the first 20 rows of the CSV file. This helps determine whether a variable should be treated as numeric or character. To change it to 30, you can use the GUESSINGROWS=30
option.
PROC IMPORT OUT=newdata DATAFILE="/home/deepanshu88us0/mydata/customers.csv" DBMS=CSV REPLACE; GUESSINGROWS=30; RUN;
How to Import a CSV File into SAS Studio
Please follow the steps below to import a CSV File into SAS Studio.
- Navigate to the desired folder where you want to import the CSV file. It is located on the left-hand side of the screen under
Server Files and Folders
pane. - Right click on the folder and then click on
Upload Files
. - Click on
Choose Files
and then locate and select the CSV file you want to import. Then click onUpload
button - In the folder where file is imported, locate the imported datafile and then right click on it and select the
Import Data
option. - Click on the
Run
button to start importing. - SAS Studio will import the CSV file and create a new dataset. By default, the imported dataset will be available in the WORK library.
Share Share Tweet