How do I export a SAS dataset to an external file?

Exporting a SAS dataset to an external file involves using the PROC EXPORT procedure to write the SAS dataset to a file in the desired format. The file can then be opened with an external program such as Microsoft Excel or SPSS. The PROC EXPORT procedure requires the user to specify the path and filename of the external file, the SAS dataset to be exported, and the data format of the external file. Once the PROC EXPORT procedure is executed, the SAS dataset is written to the external file and can be used with another program.


You can use the PROC EXPORT statement to export datasets in SAS to external files.

This statement uses the following basic syntax:

proc export data=my_data
    outfile="/home/u13181/my_data.csv"
    dbms=csv
    replace;
run;

Here’s what each line does:

  • data: Name of dataset to export
  • outfile: Location to export file to
  • dbms: Format to use for exported file
  • replace: Replace the file if it already exists

You can use this general syntax to export SAS datasets to various file types. You will only need to change the value for the dbms argument depending on the format you’d like to use for the exported file.

For example:

  • To export to a CSV file, specify dbms=csv
  • To export to an Excel file, specify dbms=xlsx
  • To export to a Text file, specify dbms=tab

The following examples show how to use PROC EXPORT to export SAS datasets to each of these filr formats.

Example 1: Use PROC Export with CSV File

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data;
    input A B C;
    datalines;
1 4 76
2 3 49
2 3 85
4 5 88
2 2 90
4 6 78
5 9 80
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following code to export this dataset to a CSV file called data.csv:

/*export dataset*/
proc export data=my_data
    outfile="/home/u13181/data.csv"
    dbms=csv
    replace;
run;

I can then navigate to the location on my computer where I exported the file and view it:

Example 2: Use PROC Export with Excel File

Suppose we have the following dataset in SAS:

/*create dataset*/
data my_data;
    input A B C;
    datalines;
1 4 76
2 3 49
2 3 85
4 5 88
2 2 90
4 6 78
5 9 80
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following code to export this dataset to an Excel file called my_data.xlsx:

/*export dataset*/
proc export data=my_data
    outfile="/home/u13181/my_data.xlsx"
    dbms=xlsx
    replace;
    sheet="First Data";
run;

I can then navigate to the location on my computer where I exported the file and view it in Excel:

The data in Excel matches the dataset from SAS and the sheet in the Excel workbook is called “First Data” just like I specified in the proc export statement.

Example 3: Use PROC Export with Text File

Suppose we have the following dataset in SAS that contains information about various basketball players:

/*create dataset*/
data my_data;
    input rating points assists rebounds;
    datalines;
90 25 5 11
85 20 7 8
82 14 7 10
88 16 8 6
94 27 5 6
90 20 7 9
76 12 6 6
75 15 9 10
87 14 9 10
86 19 5 7
;
run;

/*view dataset*/
proc print data=my_data;

We can use the following code to export this dataset to a text file called my_data.txt:

/*export dataset*/
proc export data=my_data
    outfile="/home/u13181/my_data.txt"
    dbms=tab
    replace;
run;

I can then navigate to the location on my computer where I exported the file and view it:

The data in the text file matches the dataset from SAS.

Note: Refer to the SAS for a complete list of optional arguments you can use when exporting files.

The following tutorials explain how to perform other common tasks in SAS:

x