Saving a table in SAS is a fundamental task that ensures your data is permanently stored and accessible for future analysis, whether as a native SAS dataset or in various external formats.
Programmatically Saving a SAS Dataset (DATA Step)
The most common and robust method to save a temporary SAS dataset (often residing in the WORK
library) as a permanent SAS dataset is by using the DATA
step with a predefined library. This approach allows for full control over data manipulation and storage.
Steps to Save Using DATA Step:
-
Define a Library: A library is a logical name (libref) that points to a physical location (folder) on your computer or server where SAS datasets will be stored.
LIBNAME mydata 'C:\SAS_Data'; /* Replace with your desired path */
- Tip: For collaborative projects, consider using network paths or paths accessible to all users. Learn more about SAS Data Management.
-
Create or Copy the Dataset: Use a
DATA
step to create a new dataset in your defined library, often by setting an existing temporary dataset.LIBNAME mydata 'C:\SAS_Data'; /* Defines the library */ DATA mydata.PermanentTable; /* Creates a permanent table named 'PermanentTable' in 'mydata' library */ SET WORK.TemporaryTable; /* Copies data from a temporary table named 'TemporaryTable' in 'WORK' library */ /* You can add further data manipulation steps here, e.g., KEEP, DROP, WHERE, calculations */ RUN;
- In this example,
WORK.TemporaryTable
is a dataset that was previously created and exists only for the current SAS session.mydata.PermanentTable
will be saved to theC:\SAS_Data
folder and persist after your SAS session ends.
- In this example,
Saving a Table Using a Graphical User Interface (GUI)
SAS applications like SAS Studio and SAS Enterprise Guide offer user-friendly graphical interfaces for saving your data. The method you use can vary based on whether you're simply saving a table or saving it as part of a larger analytical plan or project.
General GUI Save for a Table:
For quick saves of a data table without an associated plan:
- Locate the Table: In the interface's data explorer or output window, find the table you wish to save.
- Right-Click and Save: Right-click on the table and select an option such as "Save As," "Export," or "Create Dataset."
- Specify Location and Name: Choose the target library (e.g.,
mydata
) and provide a clear, descriptive name for your new table. - Confirm: Click "Save" or "OK" to finalize the process.
- For more on SAS Studio, visit SAS Studio Overview.
- For SAS Enterprise Guide, see SAS Enterprise Guide Features.
Saving Tables with Analytical Plans (Specific Environments):
In certain SAS environments, particularly those designed for creating and saving analytical plans or processes that generate tables, a more structured "Save As" process is used. This method allows you to save both the logic of your analysis (the "plan") and its resulting data table.
- Initiate Save: Click the Save option, which will typically open a dedicated "Save As" window.
- Select Plan and Table: Within this window, locate and click the Save plan and table checkbox to ensure both components are saved.
- Specify Plan Details: Enter a descriptive Name and select the appropriate Type for your plan in the designated fields. This helps organize and identify your saved analytical work.
- Review Table Name: Check and, if necessary, change the name of the output table in the Table name field to ensure it is clear and unique.
- Complete Save: Confirm your selections to save the table along with its associated plan.
Saving SAS Tables to External Formats
Sometimes you need to share your data with users who don't have SAS or prefer other formats. SAS provides straightforward ways to export your tables to common formats like CSV, Excel, and more.
Using PROC EXPORT:
The PROC EXPORT
procedure is a powerful tool for converting SAS datasets into various external file formats.
-
Export to CSV (Comma Separated Values):
LIBNAME mydata 'C:\SAS_Data'; PROC EXPORT DATA=mydata.PermanentTable OUTFILE='C:\Exports\my_table.csv' /* Specify the full path and filename for the CSV */ DBMS=CSV REPLACE; /* Overwrites the file if it already exists */ RUN;
-
Export to Excel (XLSX):
LIBNAME mydata 'C:\SAS_Data'; PROC EXPORT DATA=mydata.PermanentTable OUTFILE='C:\Exports\my_table.xlsx' /* Specify the full path and filename for the Excel workbook */ DBMS=XLSX REPLACE; /* Overwrites the file if it already exists */ RUN;
- You can also specify a particular sheet name using
SHEET='SheetName'
withDBMS=XLSX
. - For more details, refer to the PROC EXPORT Documentation.
- You can also specify a particular sheet name using
Using ODS EXCEL (for enhanced formatting):
For more control over the appearance and formatting of your exported Excel file, including multiple sheets, titles, and styles, use the Output Delivery System (ODS) with the EXCEL
destination.
LIBNAME mydata 'C:\SAS_Data';
ODS EXCEL FILE='C:\Exports\my_formatted_table.xlsx'
OPTIONS(FROZEN_HEADERS='YES');
PROC PRINT DATA=mydata.PermanentTable;
TITLE 'My Formatted Data Table';
RUN;
ODS EXCEL CLOSE;
Summary of Saving Methods
Here's a quick overview of the different ways to save tables in SAS:
Method | Description | Use Case |
---|---|---|
DATA Step | Programmatic code to create or copy temporary data to permanent SAS datasets. | Automation, complex data transformations, permanent storage. |
GUI (General Save) | Interactive "Save As" or "Export" options within SAS applications (e.g., SAS Studio). | Quick saves, interactive analysis, user-friendly. |
GUI (Plan-Specific) | Structured "Save As" process for saving tables alongside analytical plans/processes. | Projects requiring a defined process and its output table. |
PROC EXPORT | Programmatic code to export SAS datasets to external formats (CSV, XLSX, etc.). | Sharing data with non-SAS users, integration with other software. |
ODS EXCEL | Programmatic output to Excel with advanced formatting capabilities. | Creating visually appealing reports in Excel. |
Best Practices for Data Management
- Meaningful Naming: Use clear, descriptive names for your libraries and datasets (e.g.,
sales_q1_2023
instead ofdata1
). - Organized Libraries: Group related datasets into specific libraries for easy retrieval and management.
- Version Control: If working on critical projects, consider implementing a version control strategy for your datasets, perhaps by including dates or version numbers in their names (e.g.,
sales_data_20230101_v1
). - Documentation: Always document your data sources, transformations, and saving locations.