SAS to Excel: Save SAS data to Excel (Easy Example Codes!)
SAS users sometimes need to know “SAS to Excel: Save SAS data to Excel” because some analyses are more convenient in Excel than in SAS. If you want to filter data, for example, it is easy and handy in Excel than in SAS. SAS dataset can be saved or exported to Excel in two ways: (1) using PROC EXPORT and (2) using Libname. While the EXPORT procedure allows to include column labels and header names, the LIBNAME statement allows sub-setting the dataset. In addition to the SAS Communities post on how to export SAS dataset to Excel, this article provides easier examples.
1. PROC EXPORT to Export SAS dataset to Excel
The GENERIC code for exporting SAS dataset to Excel is as follows:
/* Generic PROC EXPORT Statement to export SAS data to Excel */
proc export
data=student_grade
outfile="Location where to export\FileName.xlsx"
DBMS=xlsx
REPLACE;
sheet=grade; /*Give the name of the sheet in which the data will be exporeted */
run;
Explanation of the Statement:
- PROC EXPORT: Start the Statement with the proc export.
- data: The name of the data set to be exported.
- outfile: This is the location of the folder where the Excel File will be exported.
- DBMS: This is the format of the file. Here we use the Excel file extension XLSX
- REPLACE: If users already have a file with the same name, use REPLACE to replace the existing file.
- sheet: Give a name of the sheet in Excel where the SAS data will be exported.
Now let’s start with creation of a new data set:
2. Create a dataset to be Exported to Excel
We can directly apply the PROC EXPORT statement to export SAS data in Excel; however, it is better to know how to create a new data set by using DATA STEP and then know how to export the data set in Excel.
Example: Create a new data set in SAS:
To create a new data set, use the following code:
/*To create data set using DATA STEP*/
data student_grade;
input Name$ Math Stat Science;
datalines;
Sanam 90 95 94
Alex 84 85 88
Rich 92 93 94
Craig 88 78 72
Robert 72 74 68
run;
/* To print the data set in SAS*/
proc print data=student_grade;
run;
The SAS data is as follows:
3. Example PROC EXPORT code to Export SAS dataset to Excel
To export SAS data set to Excel File, the code is as follows:
/* To export SAS Data set to Excel File */
proc export
data=student_grade
outfile="C:\wikitekkee\Learning SAS\student_grade.xlsx"
DBMS=xlsx REPLACE;
sheet=grade;
run;
Check the Excel File:
After running the above code, we want to check whether the data set has been properly exported to Excel. To do so, we go to the file location and find the following Excel file.
4. PROC EXPORT to Export Multiple SAS dataset to Multiple Excel Sheets
To export multiple SAS datasets to multiple Excel Sheets, users need to write the same PROC EXPORT statements. Users can save time by using multiple sheets.
Examples:
Let’s create three data sets and then we EXPORT them to Excel Sheets in one Excel Workbook.
/* To Create dataset Grade1*/
data grade1;
input Name$ accounting math stat;
datalines;
Alex 80 71 94
Bob 74 75 78
Rich 86 88 98
Craig 75 85 88
Ling 82 84 84
;
run;
/*To Create dataset Grade2*/
data grade2;
input Name$ finance calculus mgt;
datalines;
Sanam 80 71 94
Ruby 74 75 78
John 86 88 98
Pavel 75 85 88
Safa 82 84 84
;
run;
/*To Create dataset Grade3*/
data grade3;
input Name$ biology math computer;
datalines;
Alxe 80 71 94
Akash 74 75 78
Boby 86 88 98
Sofia 75 85 88
Yafi 82 84 84
;
run;
Export the New datasets to a single Excel Workbook but in different sheets:
/*To Export "grade1" dataset to first sheet in Excel*/
proc export data=grade1
outfile="C:\wikitekkee\Learning SAS\grade_data.xlsx"
dbms=xlsx
replace;
sheet="grade1";
run;
/*To Export "grade2" dataset to second sheet in Excel*/
proc export data=grade2
outfile="C:\wikitekkee\Learning SAS\grade_data.xlsx"
dbms=xlsx
replace;
sheet="grade2";
run;
/*To Export "grade3" dataset to third sheet in Excel*/
proc export data=grade3
outfile="C:\wikitekkee\Learning SAS\grade_data.xlsx"
dbms=xlsx
replace;
sheet="grade3";
run;
After executing the above codes, we can now check the specified folder whether the datasets have been properly exported.
Grade 1 in the first Excel sheet:
Grade2 in the second Excel Sheet:
Grade3 in the third Excel Sheet:
In this article, we have discussed SAS to Excel: Save SAS data to Excel. Users can also use LIBNAME statement. You can also watch a video on SAS export to Excel.
5. More related readings:
Getting Started with data in SAS
To start with SAS data analysis, you first have to have a dataset. This article explains how to create and import data in SAS.
Infile Statement to Import Data into SAS
I am not sure where you are getting your information, but great topic. I needs to spend some time learning more or understanding more. Thanks for great information I was looking for this info for my mission.
A collar tugs on one area in a single course, whereas a harness tugs their complete chest and back space making it laborious on your French bulldog pup to inform what you want him to do.
Or on-line jobs for Nigerian student?
He who has understood cannot help making use of the Teaching, as a result of realizing objective-health he accepts it as a solution of life.
3.0 Holy Fury allows the player to “design” pagan religions upon reformation, introduces new Crusade mechanics and occasions, mechanics for coronations, sainthood and bloodlines and likewise consists of shattered and random maps.
Great weblog here! Additionally your site a lot up very fast! What web host are you the use of? Can I am getting your affiliate link to your host? I wish my web site loaded up as fast as yours lol
Interesting blog post. Some tips i would like to bring up is that personal computer memory is required to be purchased when your computer still can’t cope with that which you do by using it. One can put in two good old ram boards having 1GB each, for example, but not one of 1GB and one with 2GB. One should always check the maker’s documentation for one’s PC to be certain what type of memory space is needed.