*Copyright @ www.mycsg.in;
What does PROC EXPORT do
`proc export` writes a SAS dataset to an external file format such as Excel or delimited text
This is useful when SAS output must be shared with users who work outside SAS
In this lesson we focus on exporting datasets to Excel workbooks
Export males dataset from WORK library to an Excel workbook named males.xlsx
The `data=` option names the SAS dataset to export
The `outfile=` option gives the full output file path
The `replace` option allows SAS to overwrite the file if it already exists
proc export data=males outfile="&root2._males.xlsx" replace; run;
Copy Code
View Log
SAS Log
proc export data=males outfile="&root2._males.xlsx" replace; run; NOTE: "MALES" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 1.21 seconds cpu time 0.37 seconds
The workbook `males.xlsx` is created at the specified location
Open the file outside SAS if needed and confirm that the sheet contains the rows from `males`
Export females dataset from WORK library to an Excel workbook named females.xlsx
This example repeats the same logic for a different input dataset
Notice that only the `data=` dataset name and output file name change
proc export data=females outfile="&root2._females.xlsx" replace; run;
Copy Code
View Log
SAS Log
proc export data=females outfile="&root2._females.xlsx" replace; run; NOTE: "FEMALES" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.17 seconds cpu time 0.10 seconds
Export class dataset from WORK library to an Excel workbook named class.xlsx and set the sheet name to Students
The `sheet=` option lets us control the worksheet name inside the workbook
This is helpful when the default sheet name is not descriptive enough
proc export data=class outfile="&root2._class.xlsx" replace; sheet="Students"; run;
Copy Code
View Log
SAS Log
proc export data=class outfile="&root2._class.xlsx" replace; sheet="Students"; run; NOTE: "Students" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.18 seconds cpu time 0.10 seconds
Export the datasets males, females, and class into separate sheets of one workbook named mydata.xlsx
When exporting multiple datasets to one workbook, it is important to manage worksheet names clearly
The first export creates or replaces the workbook
Subsequent exports target the same workbook and specify different sheet names
proc export data=males outfile="&root2._mydata.xlsx" replace; sheet="Males"; run; proc export data=females outfile="&root2._mydata.xlsx"; sheet="Females"; run; proc export data=class outfile="&root2._mydata.xlsx"; sheet="Class"; run;
Copy Code
View Log
SAS Log
proc export data=males outfile="&root2._mydata.xlsx" replace; sheet="Males"; run; NOTE: "Males" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.17 seconds cpu time 0.10 seconds proc export data=females outfile="&root2._mydata.xlsx"; sheet="Females"; run; NOTE: Export cancelled. Output table already exists. Specify REPLACE option to overwrite it. NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.12 seconds cpu time 0.07 seconds proc export data=class outfile="&root2._mydata.xlsx"; sheet="Class"; run; NOTE: Export cancelled. Output table already exists. Specify REPLACE option to overwrite it. NOTE: The SAS System stopped processing this step because of errors. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.14 seconds cpu time 0.09 seconds
The workbook `mydata.xlsx` should now contain separate sheets for each dataset
Confirm that each sheet name and row set matches the intended input dataset
Export males dataset with variable labels instead of variable names
We first assign labels to selected variables in the `males` dataset
Using the `label` option on `proc export` tells SAS to write labels as column headings when supported by the destination format
This is useful when the external audience prefers descriptive headings rather than SAS variable names
data males; set males; label name="Name of the student" height="Height (Inches)" weight="Weight (Pounds)"; run; proc export data=males outfile="&root2._males_label.xlsx" replace label; sheet="Males - With variable labels"; run; proc export data=males outfile="&root2._males_names.xlsx" replace; sheet="Males - With variable names"; run;
Copy Code
View Log
SAS Log
data males; set males; label name="Name of the student" height="Height (Inches)" weight="Weight (Pounds)"; run; NOTE: There were 10 observations read from the data set WORK.MALES. NOTE: The data set WORK.MALES has 10 observations and 5 variables. NOTE: DATA statement used (Total process time): real time 0.00 seconds cpu time 0.00 seconds proc export data=males outfile="&root2._males_label.xlsx" replace label; sheet="Males - With variable labels"; run; NOTE: "Males___With_variable_labels" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.15 seconds cpu time 0.11 seconds proc export data=males outfile="&root2._males_names.xlsx" replace; sheet="Males - With variable names"; run; NOTE: "Males___With_variable_names" range/sheet was successfully created. NOTE: PROCEDURE EXPORT used (Total process time): real time 0.12 seconds cpu time 0.12 seconds
Compare the two exported workbooks and notice the difference between label-based headings and variable-name-based headings
This example shows how metadata stored in SAS can influence exported output
Key points to remember
`proc export` moves SAS data to external files
`outfile=` gives the path of the external file
`replace` allows an existing file to be overwritten
`sheet=` controls worksheet naming for Excel exports
The `label` option can write variable labels as column headings