Working with Code Snippets

Why Use Code Snippets?

Code snippets enable you to quickly insert SAS code into your program and customize it to meet your needs. SAS Studio is shipped with several code snippets. You can also create your own snippets and add snippets to your list of favorites.
Snippet Name
Description
Data
Import CSV File
The Import CSV File snippet enables you to import a comma-separated file and write the output to a SAS data set.
Import XLS File
The Import XLS File snippet enables you to import a Microsoft XLS file and write the output to a SAS data set.
DS2 Package
The DS2 Package snippet provides a template for a DS2 package. A package is similar to a DS2 program. The package body consists of a set of global declarations and a list of methods. The main syntactical differences are the PACKAGE and ENDPACKAGE statements. These statements define a block with global scope. For more information, see SAS DS2 Language Reference.
DS2 Code
The DS2 Code snippet provides a template for a DS2 program. DS2 is a SAS programming language that is appropriate for advanced data manipulation. DS2 is included with Base SAS and shares core features with the SAS DATA step. DS2 exceeds the DATA step by adding variable scoping, user-defined methods, ANSI SQL data types, and user-defined packages. The DS2 SET statement accepts embedded FedSQL syntax, and the run-time-generated queries can exchange data interactively between DS2 and any supported database. This allows SQL preprocessing of input tables, which effectively combines the power of the two languages. For more information, see SAS DS2 Language Reference.
DS2 Thread
The DS2 Thread snippet provides a template for a DS2 threaded program. Typically, DS2 code runs sequentially. That is, one process runs to completion before the next process begins. It is possible to run more than one process concurrently, using threaded processing. In threaded processing, each concurrently executing section of code is said to be running in a thread. For more information, see SAS DS2 Language Reference.
Generate CSV File
The Generate CSV File snippet enables you to export SAS data as a comma-separated text file.
Generate PowerPoint Slide
The Generate PowerPoint Slide snippet enables you to stream Microsoft PowerPoint output to your web browser.
Generate XML File
The Generate XML File snippet enables you to export SAS data as an XML file that you can view in your web browser.
Simulate Linear Regression Data
The Simulate Linear Regression Data snippet creates an input data source that you can use for linear regression analysis. Linear regression analysis tries to assign a linear function to your data by using the least squares method.
Simulate One-Way ANOVA Data
The Simulate One-Way ANOVA Data snippet creates an input data source that considers one treatment factor with three treatment levels. When you analyze this data by using the One-Way ANOVA task, the goal is to test for differences among the means of the levels and to quantify these differences.
Descriptive
PROC SQL
The PROC SQL snippet provides a template for writing SQL queries. For more information, see SAS SQL Procedure User's Guide.
Custom ODS Output
The Custom ODS Output snippet provides a template for creating HTML, PDF, and RTF output by using the SAS Output Delivery System. For more information, see SAS Output Delivery System: User's Guide.
Graph
Note: For more information about the SGPLOT, SGPANEL, and SGSCATTER procedures, see SAS ODS Graphics: Procedures Guide.
Bar Panel
The Bar Panel snippet uses the VBAR statement in the SGPANEL procedure and enables you to create multiple bar charts.
Box Panel
The Box Panel snippet uses the VBOX statement in the SGPANEL procedure and enables you to create multiple box plots.
Comparative Scatter Plot
The Comparative Scatter Plot snippet uses the COMPARE statement in the SGSCATTER procedure. This code snippet creates a comparative panel of scatter plots with shared axes.
Dot Plot
The Dot Plot snippet uses the DOT statement in the SGPLOT procedure. Dot plots summarize horizontally the values of a category variable. By default, each dot represents the frequency for each value of the category variable.
Fit Plot
The Fit Plot snippet uses the REG statement in the SGPLOT procedure. This code snippet produces a regression plot with a quadratic fit and includes confidence limits.
HBar Plot
The HBar Plot snippet uses the HBAR statement in the SGPLOT procedure. This code snippet creates a horizontal bar chart that summarizes the values of a category variable.
HighLow Plot
The HighLow Plot snippet uses the HIGHLOW statement in the SGPLOT procedure. High-low charts show how several values of one variable relate to one value of another variable. Typically, each variable value on the horizontal axis has several corresponding values on the vertical axis.
Histogram Plot
The Histogram Plot snippet uses the HISTOGRAM statement in the SGPLOT procedure. This code snippet produces a histogram with two density plots. In this snippet, one density plot uses a normal density estimate and the other density plot uses a kernel density estimate.
Scatter Plot Matrix
The Scatter Plot Matrix snippet uses the MATRIX statement in the SGSCATTER procedure. This code snippet creates a scatter plot matrix.
VBox Plot
The VBox Plot snippet uses the VBOX statement in the SGPLOT procedure. A box plot summarizes the data and indicates the median, upper and lower quartiles, and minimum and maximum values. The plot provides a quick visual summary that easily shows center, spread, range, and any outliers. The SGPLOT and the SGPANEL procedures have separate statements for creating horizontal and vertical box plots.
Macro
SAS Macro
The SAS Macro snippet provides a template for creating a SAS macro program. For more information, see SAS Macro Language: Reference.
IML
Find Roots of Nonlinear Equation
The Find Roots of Nonlinear Equation snippet enables you to find the roots of a function of one variable. Finding the root (or zero) of a function enables you to solve nonlinear equations.
Integrate a Function
The Integrate a Function snippet enables you to numerically integrate a one-dimensional function by using the QUAD subroutine in SAS/IML software. Use the QUAD subroutine to numerically find the definite integral of a function on a finite, semi-infinite, or infinite domain.
Generate a Bootstrap Distribution
The Generate a Bootstrap Distribution snippet uses the IML procedure to create and analyze a bootstrap distribution of the sample mean.
Fit by using Maximum Likelihood
The Fit by using Maximum Likelihood snippet uses maximum likelihood estimation to estimate parameters for the normal density estimate.
Simulate Multivariate Normal Data
The Simulate Multivariate Normal Data snippet simulates data from a multivariate normal distribution with a specified mean and covariance.
To add a snippet to your list of favorites, select the snippet name and click Favorite button.

Create a Code Snippet

To create your own snippet:
  1. Open your .sas file in SAS Studio and select the code that you want to save as a snippet.
  2. On the Code tab, click Add to My Snippets button. The Add to My Snippets dialog box appears.
  3. Enter a name for the snippet and click Save.
This snippet is now available from the My Snippets folder.

How to Insert a Code Snippet

To include a code snippet in your program:
  1. Click the location in your program where you want to insert the snippet.
  2. In the navigation pane, open the Snippets section.
  3. You can add the snippet to your program in these ways:
    • use a drag-and-drop operation to move the snippet.
    • double-click the name of the snippet.
    • right-click the name of the snippet and select Insert. To select multiple snippets, use the Ctrl key. Then right-click and select Insert.
    Code for Importing an Excel File Inserted into a Program