How To Write SAS Code in SAS Enterprise Guide
How To Write SAS Code in SAS Enterprise Guide
INTRODUCTION
Using SAS Enterprise Guide, you can manipulate data and run reports without ever writing a single line of SAS code.
So its not surprising that many SAS programmers believe that SAS Enterprise Guide is only useful to nonprogrammers. If you love the SAS language, why would you ever want to use SAS Enterprise Guide?
It turns out that there are several reasons why you might want to do just that. SAS Enterprise Guide offers
programmers a variety of ways to run code. You can type a program like you do in Display Manager, but you can also
use the point-and-click features of SAS Enterprise Guide to generate programs that you can then modify. In addition,
SAS Enterprise Guide organizes your work into projects making it easy to find your programs, logs, and results; and
the process flow diagrams show at a glance how everything in your project fits together.
However, writing programs in SAS Enterprise Guide does require learning a new environment with new windows and
a new system for organizing your work. As with any new skill, there is a learning curve. The goal of this paper is to
ease that transition by explaining SAS Enterprise Guide from a programmers perspective.
In SAS Enterprise Guide there are often three or four (or even more) ways to do the same thing. This paper makes no
attempt to show all ways, but instead shows the easiest ways. By exploring the menus and function keys, and by
right-clicking, you can easily find other ways.
This paper was written using SAS Enterprise Guide 6.1, but also applies to versions 4.3 and 5.1 with only slight
differences. If you are using SAS Enterprise Guide 4.2, you will find that the windows and menus are similar, but
some of the more advanced programming features are not available.
Select
Program New Program
and type your program
Starting with SAS Enterprise Guide 4.3, the editor offers autocompletion. As you type, the editor tries to guess what
you want. A pop-up list will appear with possible keywords. You can click the list to select the word you want, or press
the Spacebar, Enter or Tab keys to select the highlighted word, or just ignore it and keep typing.
In addition to turning autocompletion on or off, you can customize many other features of the editor such as the font
size, color scheme, indention, and line numbering.
Click Run or
select Run Selection
If you have more than one SAS server, then you can change the server that SAS Enterprise Guide will use by clicking
the down-arrow next to Selected Server at the top of the Program window. SAS Enterprise Guide will submit your
program to SAS on the currently selected server, and display the results.
Results display
automatically
REPLACING RESULTS
Every time you rerun your program, a window will open asking you whether you want to keep the results from your
previous run, or replace the results. Generally speaking, you will want to click Yes to replace the results. Otherwise,
your project will quickly fill up with nearly identical results.
SAS log
Icon indicating
program with error
, this indicates that the program produced errors (either error messages
or data errors such as invalid dates). A yellow caution sign on your program icon
contains warnings (but no errors). You can click the down-arrow
to go to the previous error or warning. If you click
arrow
display a list of all the errors, warnings and notes.
Project
Tree
Workspace displaying
Process Flow
The project is displayed in the Project Tree window in the upper-left corner and in the Process Flow window on the
right. The space where the Process Flow appears is called the workspace. The Process Flow shares the workspace
with many other windows including the Program window and results windows. You can switch between these windows
by double-clicking their names in the Project Tree. The Project Tree displays your project in a hierarchical tree
diagram while the Process Flow displays your project in a process flow diagram. Icons representing your code and
results appear in both the Project Tree and Process Flow.
To run your entire project (which can contain multiple process flows), click the down-arrow next to Run at the top of a
Process Flow window and select Run Run Project. To run a single process flow, click the down-arrow next to Run
at the top of that particular Process Flow window and select Run Run Process Flow.
You can have as many projects as you wish, but only one can be open at a time. However, if you want to see more
than one project at a time, there is a simple work around. Simply open a new SAS Enterprise Guide session to display
a second project.
Select File
Save Project
As
Right-click icon
Select Rename
Using the Program window, you can edit programs that were written in Display Manager and run them just the same
as programs that have been written in SAS Enterprise Guide. At this point, this project contains two programs
(RegionFreq and SplitData) and their results.
project. Because RegionFreq was written in SAS Enterprise Guide, its icon does not include an arrow.
RegionFreq is saved inside the project, but SplitData is not.
Code that is saved as part of a project is called embedded code; code that is saved outside of a project is
unembedded. You can change whether or not a file is embedded. To do this, right-click the program icon in the
Project Tree or Process Flow, and select Properties from the pop-up menu. A Properties window will open for the
program. In the General page, click the Embed button to embed an external program in your project, or the Save As
button to save the program in an external file.
Click Embed or
Save As button
Whether its better to embed programs in your project or save them as separate files is your choice. One point to
consider is that programs that are not embedded can be shared by more than one project while embedded programs
can only be used by the project that contains them. Either way, its important that you understand where your
programs are stored so you dont accidentally end up with multiple copies of a program when you only want one (or
vice versa).
When you open a task, it will use the active data set. The active data set is the one you have accessed most recently.
You can change the active data set by viewing it in a Data Grid or by clicking the icon of the data set you want in the
Project Tree or the Process Flow before you open the task.
This example uses the Summary Tables task, but the process for copying programs is similar for all tasks. The
Summary Tables task writes PROC TABULATE code. To open Summary Tables, select Tasks Describe
Summary Tables from the menu bar (or Describe Summary Tables from above a Data Grid). A Summary Tables
window, like the one pictured below, will open. The first thing you do in any task is assign variables to task roles by
dragging variable names to the list of task roles. In this window, the variables Region and PopGroup have been
assigned to serve as classification variables.
Then Click
Summary Tables
The second thing you do in the Summary Tables task, is arrange your table. To do that, click the Summary Tables
Drag variables
to Preview area
Once you are satisfied with the arrangement of your table, click the Run button. SAS Enterprise Guide will run the
task and display the results.
To see the program generated by the task, click the Code tab above the results. You cannot edit the program
generated by a task, but you can make a copy of the program and edit the copy. The quickest way to do this is to type
something (anything, including a space or Enter) in the Program window.
10
Type something in
Program window to
make a copy
A window will open asking if you want to create a copy of the code. Click the Yes button, and SAS Enterprise Guide
will open a new Program window containing a copy of the code.
The Process Flow now includes an icon labeled Code for Summary Tables. This is the copy of the code. You can
edit the copy of the code. You can also use this code in Display Manager by cutting and pasting, or saving this code
as an unembedded code file and opening it in Display Manager.
11
Click
Preview code
button
A Code Preview window will open for that task. Click the Insert Code button.
Click
Insert code
A User Code window will open displaying the code written by SAS Enterprise Guide. This window will show all the
locations in the program where you can insert your own code. Find the place where you want to add code, and
double-click the words <double-click to insert code>.
12
Double-click
<double-click to insert code>
An Enter User Code window will open. Type the code you want to insert and click OK.
In the task window, click the Run button. SAS Enterprise Guide will run the task using the code you inserted.
13
You can add links to make relationships between data sets and programs more clear. To add a link, right-click the
icon that you want at the beginning of the link (in this case, the World data set), and select Link item to from the popup menu.
14
A Link window will open showing all the items to which you can link. Select one, and click OK.
To add more links, repeat the process. Here is the process flow diagram with the World data set linked to each of the
program icons. Notice that links you add use a dashed line instead of a solid line.
15
There is another reason why you may want to add links to your process flow. When you run a process flow, SAS
Enterprise Guide executes it from upper-left to lower-right, following the branches of the process flow. You can use
links to explicitly control the order in which items run. You can also rearrange icons by dragging and dropping them in
the Process Flow window.
ADDING NOTES
As a programmer, you already know that it is important to use comments to document your work. Notes are text files
that you can use to document your work. To add a note for an entire process flow, click the background of the
Process Flow window and select File New Note from the menu bar. To add a note for a particular item in your
project (such as a program or task), click its icon in the Project Tree or Process Flow window and select File New
Note from the menu bar. A Note window will open. You can type any text you want in this window.
16
17
In the Edit window, type the code you want to be run automatically. This could be a LIBNAME statement, an
OPTIONS statement, or any code that you want to be submitted every time you run anything in SAS Enterprise
Guide.
18
Once you have created a new process flow and named it Autoexec, then you can put anything that you want in it. For
example, you can open a Program window and type LIBNAME or OPTIONS statements. Then the next time you open
that project, a window will open asking if you want to run the Autoexec process flow.
FORMATTING CODE
If you open a program and find that it is messy and hard to read, SAS Enterprise Guide can help with that. Just rightclick in the Program window and select Format Code from the pop-up window, and your program will be tidied up.
19
the Begin analysis button. SAS Enterprise Guide will identify each DATA and PROC step and the relationships
between them. To create a process flow, click the Create process flow button.
CONCLUSIONS
SAS Enterprise Guide offers programmers a new way to write and run code. If you have never used SAS Enterprise
Guide, it will take you a while to get used to the new environment. But once you learn how to use it, you may well
decide to make SAS Enterprise Guide your primary SAS environment.
REFERENCES
Central Intelligence Agency (2007). The World Factbook. http://www.cia.gov/cia/publications/factbook/index.html.
Delwiche, L. D. & Slaughter, S. J. (2012). The Little SAS Book: A Primer, Fifth Edition. SAS Institute, Cary, NC.
Slaughter, S. J. & Delwiche, L.D. (2013). Introduction to Summary Tables in SAS Enterprise Guide. Toronto Area
SAS Society, March 2013, http://susanslaughter.files.wordpress.com/2013/03/tass-2013-summarytables.pdf.
Slaughter, S. J. & Delwiche, L. D. (2010). The Little SAS Book for Enterprise Guide 4.2. SAS Institute, Cary, NC.
Lora D. Delwiche
(530) 752-9321
llddelwiche@ucdavis.edu
SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS
Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are
trademarks of their respective companies.
20