Use PROC IMPORT to read in a delimited file.
Note: PROC IMPORT is available only on the following operating systems: OS/2, UNIX, OpenVMS, and Windows.
For more information and additional documentation on PROC IMPORT, see the SAS Procedures Guide.
For even more information about how to import data from CSV files, see the following SAS tutorial video:
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
/*******************************************************************************/
/* PROC IMPORT reads data from a delimited file (DATAFILE=) and writes it to a */
/* SAS data set (OUT=). If the file to be read has an extension of CSV, */
/* specify DBMS=CSV. If the file to be read is tab delimited, use DBMS=TAB. */
/* For all other delimited files, use DBMS=DLM. By default, the delimiter is */
/* assumed to be a space. If it is not, use the DELIMITER= option to indicate */
/* the correct delimiter. */
/* */
/* The optional statement GETNAMES= indicates whether the first record of the */
/* file is to be used as the variable names. */
/* */
/* Beginning in SAS 9.1, GUESSINGROWS= is also available to specify how many */
/* rows of data are to be sampled to determine variable attributes. Prior */
/* to SAS 9.1 the value of GUESSINGROWS was changed by editing the registry: */
/* How to scan more than 20 records to determine variable attributes in EFI. */
/* */
/* Please refer to the SAS Procedures Guide for more details and additional */
/* options and statements. */
/*******************************************************************************/
/* Example 1: Reading an exclamation point (!) delimited file with */
/* variable names on the first row */
/* Create test file to read using PROC IMPORT below. */
data _null_;
file 'c:\temp\pipefile.txt';
put"X1!X2!X3!X4";
put "11!22!.! ";
put "111!.!333!apple";
run;
/* By default, it is assumed the variable names are on the first row, so the */
/* GETNAMES= statement is not required. */
proc import
datafile='c:\temp\pipefile.txt'
out=work.test
dbms=dlm
replace; /* note this is the first semi-colon */
delimiter='!';
getnames=yes;
run;
proc print;
run;
/* Example 2: Reading a comma delimited file with a .csv extension */
/* */
/* Since the DBMS= value is CSV, you do not have to use the DELIMITER= */
/* statement. By default, it is assumed the variable names are on the first */
/* row, so the GETNAMES= statement is not required. */
/* Create comma delimited test file to read using PROC IMPORT below. */
data _null_;
file 'c:\temp\csvfile.csv';
put "Fruit1,Fruit2,Fruit3,Fruit4";
put "apple,banana,coconut,date";
put "apricot,berry,crabapple,dewberry";
run;
proc import
datafile='c:\temp\csvfile.csv'
out=work.fruit
dbms=csv
replace;
run;
proc print;
run;
/* Example 3: Reading a tab delimited file */
/* */
/* Since the DBMS= value is TAB, you do not have to use the DELIMITER= */
/* statement. If the data starts on the first record, specify */
/* GETNAMES= NO. By default, the variable names will be VAR1, VAR2...VARn. */
/* Create tab delimited test file to read using PROC IMPORT below. */
data _null_;
file 'c:\temp\tabfile.txt';
put "cereal" "09"x "eggs" "09"x "bacon";
put "muffin" "09"x "berries" "09"x "toast";
run;
proc import
datafile='c:\temp\tabfile.txt'
out=work.breakfast
dbms=tab
replace;
getnames=no;
run;
proc print;
run;
These sample files and code examples are provided by SAS Institute Inc. "as is" without warranty of any kind, either express or implied, including but not limited to the implied warranties of merchantability and fitness for a particular purpose. Recipients acknowledge and agree that SAS Institute shall not be liable for any damages whatsoever arising out of their use of this material. In addition, SAS Institute will provide no support for the materials contained herein.
RESULTS -- Example 1 Obs X1 X2 X3 X4 1 11 22 . 2 111 . 333 apple RESULTS -- Example 2 Obs Fruit1 Fruit2 Fruit3 Fruit4 1 apple banana coconut date 2 apricot berry crabapple dewberry RESULTS -- Example 3 Obs VAR1 VAR2 VAR3 1 cereal eggs bacon 2 muffin berries toast
Type: | Sample |
Topic: | SAS Reference ==> DATA Step SAS Reference ==> Procedures ==> IMPORT Common Programming Tasks ==> Reading and Writing External Data |
Date Modified: | 2021-01-12 14:32:20 |
Date Created: | 2004-09-30 14:09:01 |
Product Family | Product | Host | Product Release | SAS Release | ||
Starting | Ending | Starting | Ending | |||
SAS System | Base SAS | z/OS | 9.4 | 9.4 TS1M0 | ||
z/OS 64-bit | 9.4 | 9.4 TS1M0 | ||||
Microsoft® Windows® for x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8 Enterprise x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8 Pro x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8.1 Enterprise 32-bit | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8.1 Enterprise x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8.1 Pro 32-bit | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 8.1 Pro x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows 10 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2008 R2 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2008 for x64 | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2012 Datacenter | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2012 R2 Datacenter | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2012 R2 Std | 9.4 | 9.4 TS1M0 | ||||
Microsoft Windows Server 2012 Std | 9.4 | 9.4 TS1M0 | ||||
Windows 7 Enterprise x64 | 9.4 | 9.4 TS1M0 | ||||
Windows 7 Professional x64 | 9.4 | 9.4 TS1M0 | ||||
Linux for x64 | 9.4 | 9.4 TS1M0 | ||||
Solaris for x64 | 9.4 | 9.4 TS1M0 | ||||
SAS System | N/A | 64-bit Enabled AIX | 9 TS M0 | |||
64-bit Enabled HP-UX | 9 TS M0 | |||||
64-bit Enabled Solaris | 9 TS M0 | |||||
AIX | 9 TS M0 | |||||
HP-UX | 9 TS M0 | |||||
HP-UX IPF | 9 TS M0 | |||||
Linux | 9 TS M0 | |||||
OpenVMS Alpha | 9 TS M0 | |||||
Solaris | 9 TS M0 | |||||
Tru64 UNIX | 9 TS M0 |