2 Quick Ways to Export SAS Data as CSV Files (Example Codes!)
SAS users need to know these 2 quick ways to export SAS data as CSV Files because CSV files can be easily opened in Excel and working in Excel is easier than in SAS. We can use PROC EXPORT to export SAS data to a CSV File. Advanced users can also find the SAS Help Center useful.
Find with Keyword:
How to Export SAS Data as CSV File, SAS Export to CSV, Proc Export SAS CSV, SAS Proc Exprot CSV
1. Generic PROC EXPORT Procedure
The generic PROC EXPORT Statement is as follows:
/* To Export SAS Data as CSV File */
PROC EXPORT data=Dataset_Name
outfile="Where to Save\FileName.csv"
DBMS=csv
REPLACE;
DELIMITER=" "
run;
The arguments:
- data: Name of the dataset to be exported as CSV File
- outfile: the location of the folder where the CSV File will be saved
- BDMS: Name of the File Format. [CSV=.cvs; TAB=.txt]
- REPLACE: Use replace if you already have the same CSV file
- DELIMITER: Specify the delimiter you want to use. By default, it is blank
2. Create SAS dataset to be exported as CSV File
To create SAS dataset, the DATA STEP is as follows:
* To Create SAS Data set */
data amazon2;
input year company$ ticker$ sales assets ni;
datalines;
2013 AMAZON.COM AMZN 74452 40159 274
2014 AMAZON.COM AMZN 88988 54505 -241
2015 AMAZON.COM AMZN 107006 65444 596
2016 AMAZON.COM AMZN 135987 83402 2371
2017 AMAZON.COM AMZN 177866 131310 3033
2018 AMAZON.COM AMZN 232887 162648 10073
2019 AMAZON.COM AMZN 280522 225248 11588
2020 AMAZON.COM AMZN 386064 321195 21331
2021 AMAZON.COM AMZN 469822 420549 33364
;
run;
/* To Print the dataset created in SAS */
proc print data=amazon2;
run;
The SAS Dataset is as follows:
3. Export SAS Dataset as CSV File
To Export the SAS Dataset as CSV File, the example code is as follows:
/*To Export SAS data to CSV File*/
PROC EXPORT data=amazon2
outfile="C:\wikitekkee\Learning SAS\amazon2.csv"
DBMS=csv
REPLACE;
run;
This is the quick way to export SAS dataset as CSV file.
The CSV File is as follows:
4. Generic PROC EXPORT Procedure
To export SAS dataset as Comma Delimited TXT File, the PROC EXPORT Statement is as follows:
/* To Export SAS Dataset as Comma Delimited TXT */
PROC EXPORT data=amazon2
outfile="C:\wikitekkee\Learning SAS\amazon2.txt"
DBMS=TAB
REPLACE;
DELIMITER=",";
run;
The Comma Delimited TEXT File is as follows:
5. Export SAS Dataset as Pipe Delimited Text File
To export SAS dataset as Pipe Delimited Text File, the PROC EXPORT Statement is as follows:
/* To Create SAS Data set */
data amazon4;
input year company$ ticker$ sales assets ni;
datalines;
2013 AMAZON.COM AMZN 74452 40159 274
2014 AMAZON.COM AMZN 88988 54505 -241
2015 AMAZON.COM AMZN 107006 65444 596
2016 AMAZON.COM AMZN 135987 83402 2371
2017 AMAZON.COM AMZN 177866 131310 3033
2018 AMAZON.COM AMZN 232887 162648 10073
2019 AMAZON.COM AMZN 280522 225248 11588
2020 AMAZON.COM AMZN 386064 321195 21331
2021 AMAZON.COM AMZN 469822 420549 33364
;
run;
/* To Print the dataset created in SAS */
proc print data=amazon4;
run;
/*TO EXPORT SAD Dataset as Pipe Delimited Text File */
PROC EXPORT data=amazon4
outfile="C:\wikitekkee\Learning SAS\amazon4.txt"
DBMS=TAB
REPLACE;
DELIMITER="|";
run;
The Pipe Delimited Text File:
6. How to Export SAS Dataset as Semicolon Delimited Text File
To export SAS dataset as semicolon delimited Text File, the PROC EXPORT Statement is as follows:
/* To Export SAS Dataset as Semicolon Delimited Text File */
PROC EXPORT
data=amazon4
outfile="C:\wikitekkee\Learning SAS\amazon4.txt"
DBMS=TAB
REPLACE;
DELIMITER=";";
run;
The semicolon delimited file from SAS:
Note:
To Export SAS dataset as semicolon delimited TXT file, the PROC EXPORT statement has the following arguements:
- data: The name of the dataset to be exported
- outfile: the location of the file where the exported file will be saved
- DBMS: The type of data format in which the dataset will be saved
- REPLACE: Use replace if you already have a dataset with the same name in the location
- DELIMITER: The delimiter that you want to use.
For a video lesson on the how to Export SAS dataset to Excel and Text File, you can watch a video here.
7. How to export and convers SAS dataset into .txt files with UTF8 encoding
Since the PROC EXPORT does not have the encoding option, users can utilize the following codes to export SAS dataset into .txt files with UTF8 encoding.
/* To export SAS dataset into .txt files with UTF8 encoding */
data _null_;
set sashelp.class;
file "C:\wikitekkee\Learning SAS\class.txt" encoding="utf-8";
put _all_;
run;
The Text File of the class data set from SASHELP center is:
Sum it up: We have discussed the 2 quick ways to export SAS data as CSV files. The first method, PROC EXPORT procedure, is easy to use; however, we cannot encode the data.
8. More related readings:
Import Excel, TEXT, STATA file into SAS
Excellent post!