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
Catalogs
Edit a SOURCE Entry
enables you to specify the content for the SOURCE entry type in a catalog. The SOURCE entry can contain the same code as SAS programs.
List Catalogs
lists in a SAS table all of the catalogs in the specified library. By default, this table is saved in the Work library and with catalogs as the filename.
List Catalog Entries
lists all the catalog types in the specified catalog.
Print GRSEG Entry
prints the GRSEG entry to the open destinations specified by the SAS Output Delivery System (ODS output).
Data
DS2 Code
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 Package
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 Thread
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
enables you to export SAS data as a comma-separated text file.
Generate PowerPoint Slide
enables you to stream Microsoft PowerPoint output to your web browser.
Generate XML File
enables you to export SAS data as an XML file that you can view in your web browser.
Import CSV File
enables you to import a comma-separated file and write the output to a SAS data set.
Import XLSX File
enables you to import a Microsoft XLSX file and write the output to a SAS data set.
Simulate Linear Regression Data
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
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
Custom ODS Output
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.
PROC SQL
provides a template for writing SQL queries. For more information, see SAS SQL Procedure User's Guide.
Graph
Note: For more information about the SGPLOT, SGPANEL, and SGSCATTER procedures, see SAS ODS Graphics: Procedures Guide.
Bar Panel
uses the VBAR statement in the SGPANEL procedure and enables you to create multiple bar charts.
Box Panel
uses the VBOX statement in the SGPANEL procedure and enables you to create multiple box plots.
Comparative Scatter Plot
uses the COMPARE statement in the SGSCATTER procedure. This code snippet creates a comparative panel of scatter plots with shared axes.
Dot Plot
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
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
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
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
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
uses the MATRIX statement in the SGSCATTER procedure. This code snippet creates a scatter plot matrix.
VBox Plot
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
Note: For more information about SAS macros, see SAS Macro Language: Reference.
SAS Macro Char Functions
provides several examples of these SAS macros that work with character values:
  • The %EVAL function evaluates arithmetic and logical expressions by using integer arithmetic. This function operates by converting its argument from a character value to a numeric or logical expression. After the expression is evaluated, the result is converted back to a character value.
    This function is useful because the SAS Macro Facility is basically a text generator. As a result, an arithmetic expression is first converted to a numeric expression. After this numeric expression is evaluated, it is converted back to an arithmetic expression.
  • The %INDEX function returns the position of the first character of a string.
  • The %LENGTH function returns the length of a string.
  • The %SCAN function searches for a word that is specified by its position in a string.
  • The %SUBSTR function produces a substring of a character string.
  • The %UPCASE function converts values to uppercase.
SAS Macro Do Statement
designates the beginning of a section of a macro definition that is treated as a unit until a matching %END statement is encountered. This macro section is called a %DO group.
A simple %DO statement often appears in conjunction with %IF-%THEN-%ELSE statements to designate a section of the macro to be processed depending on whether the %IF condition is true or false.
Note: SAS also provides a %DO iterative statement, which is different from the code that is generated by this snippet. For more information, see SAS Macro Language: Reference.
SAS Macro If Statement
conditionally processes a portion of a macro. The expression that is the condition for the %IF-%THEN-%ELSE statement can contain only operands that are constant text or text expressions that generate text.
SAS Macro Parameters
names one or more local macro variables whose values you specify when you invoke the macro. There are two types of macro variables: positional and keyword. Parameters are local to the macro that defines them. You must supply each parameter name. You cannot use a text expression to generate it. A parameter list can contain any number of macro parameters separated by commas. The macro variables in the parameter list are usually referenced in the macro.
SAS Macro
provides a basic template for working with SAS macros. Macros enable you to perform many tasks, including substituting text in a program. A SAS program can contain any number of macros, and you can invoke a macro multiple times in a single program. For more information, see SAS Macro Language: Reference.
SAS Macro Quoting
provides examples of macro functions that tell the macro processor to interpret special characters and mnemonics as text rather than as part of the macro language.
  • The %STR function masks special characters and mnemonic operators in constant text at macro compilation. This function masks these special characters and mnemonic operators:
    + - * / < > = ¬ ^ ~ ; , # blank
    AND OR NOT EQ NE LE LT GE GT IN
    This function also masks these characters when they occur in pairs and when they are not matched and are marked by a preceding %:
    ' " ()
  • The %NRSTR function masks special characters and mnemonic operators in constant text at macro compilation. This function masks all of the special characters and mnemonic operators listed for the %STR function. In addition, the %NRSTR function masks these characters:
    & %
  • The %BQUOTE function masks special characters and mnemonic operators in a resolved value at macro execution. This function masks these special characters and mnemonic operators:
    ' " () + - * / < > = ¬ ^ ~ ; , # blank
    AND OR NOT EQ NE LE LT GE GT IN
SAS Macro Quoting (continued)
  • The %SUPERQ function masks all special characters and mnemonic operators at macro execution but prevents further resolution of the value. This function masks these special characters and mnemonic operators:
    & % ' " () + - * / < > = ¬ ^ ~ ; , # blank
    AND OR NOT EQ NE LE LT GE GT IN
  • The %QSCAN function searches for a word and masks special characters and mnemonic operators.
  • The %QSUBSTR function produces a substring and masks special characters and mnemonic operators.
  • The %QUPCASE function converts a value to uppercase and returns a result that masks special characters and mnemonic operators.
  • The %UNQUOTE function unmasks a value during macro execution so that any special characters and mnemonic operators are interpreted as macro language elements instead of text.
For more information about macro complication and macro execution, see SAS Macro Language: Reference.
SAS Macro Variables
provides examples of how to create user-defined global and local macro variables. Macro variables are tools that enable you to dynamically modify the text in a SAS program through symbolic substitution. You can assign large or small amounts of text to macro variables. Then you can use that text by simply referencing the variable that contains the text.
Macro variables that are defined by the macro programmer are called user-defined macro variables. Macro variables that are defined by the macro processor are called automatic macro variables. You can define and use macro variables anywhere in SAS programs, except within data lines.
IML
Note: These snippets are available only if your site licenses SAS/IML.
Find Roots of Nonlinear Equation
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.
Fit by using Maximum Likelihood
uses maximum likelihood estimation to estimate parameters for the normal density estimate.
Generate a Bootstrap Distribution
uses the IML procedure to create and analyze a bootstrap distribution of the sample mean.
Integrate a Function
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.
Simulate Multivariate Normal Data
simulates data from a multivariate normal distribution with a specified mean and covariance.

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.
    The following example shows the Import XLSX File snippet inserted into a program.
    Code for Importing an Excel File Inserted into a Program