SAS Ka Basics of Programming My Way
SAS Ka Basics of Programming My Way
SAS Documentation
The correct bibliographic citation for this manual is as follows: SAS Institute Inc. 2015. Getting Started with Programming in
SAS Studio 3.4. Cary, NC: SAS Institute Inc.
Getting Started with Programming in SAS Studio 3.4
Copyright 2015, SAS Institute Inc., Cary, NC, USA
All rights reserved. Produced in the United States of America.
For a hard-copy book: No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any
form or by any means, electronic, mechanical, photocopying, or otherwise, without the prior written permission of the
publisher, SAS Institute Inc.
For a web download or e-book: Your use of this publication shall be governed by the terms established by the vendor at
the time you acquire this publication.
The scanning, uploading, and distribution of this book via the Internet or any other means without the permission of the
publisher is illegal and punishable by law. Please purchase only authorized electronic editions and do not participate in or
encourage electronic piracy of copyrighted materials. Your support of others' rights is appreciated.
U.S. Government License Rights; Restricted Rights: The Software and its documentation is commercial computer
software developed at private expense and is provided with RESTRICTED RIGHTS to the United States Government. Use,
duplication or disclosure of the Software by the United States Government is subject to the license terms of this Agreement
pursuant to, as applicable, FAR 12.212, DFAR 227.7202-1(a), DFAR 227.7202-3(a) and DFAR 227.7202-4 and, to the extent
required under U.S. federal law, the minimum restricted rights as set out in FAR 52.227-19 (DEC 2007). If FAR 52.227-19 is
applicable, this provision serves as notice under clause (c) thereof and no other notice is required to be affixed to the
Software or documentation. The Government's rights in Software and documentation shall be only those set forth in this
Agreement.
SAS Institute Inc., SAS Campus Drive, Cary, North Carolina 27513-2414.
July 2015
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.
Contents
Using This Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
Accessibility . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
Chapter 1 Introduction to SAS Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Write a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Save Your Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What If I Have an Error? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Chapter 3 Save Time with the Libraries Section . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
iv Contents
Requirements
To complete the examples in this book, you must have access to SAS Studio 3.4.
vii
Accessibility
For information about the accessibility of this product, see Accessibility Features of SAS
Studio 3.4 at support.sas.com.
1
Introduction to SAS Studio
Welcome to SAS Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
SAS Studio at a Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
SAS Studio supports multiple web browsers, such as Microsoft Internet Explorer, Apple
Safari, Mozilla Firefox, and Google Chrome.
As you work through this document, you will learn how to perform these tasks:
n
write a program
The main window of SAS Studio consists of a navigation pane on the left and a work
area on the right. The navigation pane provides access to files on your server and your
folder shortcuts, your tasks and snippets, the libraries that you have access to, and your
file shortcuts. The Server Files and Folders section is displayed by default.
The work area is used to display your data, code, logs, and results. When you first open
SAS Studio, the work area displays a new program window. When you open data and
run tasks, other windows open in the work area with a tabbed interface.
SAS Studio includes two different perspectives: the SAS Programmer perspective and
the Visual Programmer perspective. A perspective is a predetermined set of features
that is customized to meet the needs of a specific type of user. This document is about
programming in SAS, so you need to make sure that the SAS Programmer perspective
is selected on the toolbar at the top of the application. You can find more information
about both perspectives in SAS Studio: User's Guide.
There are several ways that you can get help with your work in SAS Studio. Pop-up
help is available for some options in the application, which you can access by clicking
next to the option. Comprehensive help for SAS Studio is available by clicking
above the work area and selecting SAS Studio Help.
2
First Steps in SAS Studio
Write a Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Save Your Results . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
What If I Have an Error? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Write a Program
It is easier than ever to program in SAS. All you need to do is open your browser, start
SAS Studio, and start writing your program. As you enter your code, youll notice that
SAS Studio has several features to help you reduce your programming time, including
autocomplete for hundreds of SAS statements and procedures as well as built-in syntax
help that includes links to the more extensive SAS Product Documentation.
To get started, lets write a very simple program that uses a sample SAS table from the
Sashelp library. Open SAS Studio and enter the following code in the Program 1
window that is created automatically for you:
proc print data=sashelp.class;
run;
HTML file
PDF file
RTF file
In the following example, the results have been downloaded as a PDF file and opened
in Adobe Acrobat Reader.
You can also print your results from SAS Studio by clicking
. The results open in a
separate browser window, and you can then use the default printer controls for that
browser.
Click
to run the program. This time, the Log tab opens automatically to show you
that you have an error.
In the Errors, Warnings, Notes section, expand Errors to view a description of the error.
Click the error message, and SAS Studio highlights it for you in the log so that you can
see exactly where the error occurred.
You can return to your program and correct the error. However, if you have a very long
and complicated program that has a lot of errors, you might want to return to an earlier
version of your program in which you knew all the code was correct. SAS Studio
maintains a log, or submission history, with entries for each time you run a program, so
you can easily return to an earlier version of a program.
To find an earlier version of your program, click the Code tab to view the current version
of your program. On the toolbar, click
The original version of your program opens in a new window from which you can copy
and paste the error-free code into your original program or into a new program.
11
3
Save Time with the Libraries Section
Add Column Names to Your Program . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Next, add the VAR statement to the program to specify which variables, or columns, to
include in the results. After the first line of code, add the following new line of code:
var
Now you can use the Libraries section to help complete the VAR statement. Click the
Libraries section in the navigation pane and expand the Sashelp library. Locate the
Class table and expand it to view the columns.
13
Hold down the Ctrl key and select the Name, Age, and Height columns and then drag
them to the end of the VAR statement in your program. A green check mark icon
indicates where you can drop the selected columns.
When you drop the selected columns, SAS Studio adds the column names to your
program. The SAS programming language requires that each statement end with a
semicolon. To avoid another error when you run your program, you must add a
semicolon to the end of the VAR statement.
By using the Libraries section, you can easily see the names of the columns in a table,
and you can save time by dragging table and column names to your programs instead
of entering them.
15
4
Use SAS Studio to Generate Your
Code
Let SAS Studio Do the Programming for You! . . . . . . . . . . . . . . . . . . . . . . 15
In the Columns area of the table viewer, all of the columns are selected by default.
Clear the Weight column and notice that it is immediately removed from the table
viewer.
17
Next, you can add a filter and sort the data. Right-click the Age column heading, and
select Add Filter. From the list of column values, hold down the Ctrl key and select
three values: 11, 12, 13.
Click Filter. The table viewer is updated and now displays only the rows for ages 11, 12,
and 13.
Note: The filter criteria are displayed at the top of the table viewer. You can click
edit the filter and to delete the filter.
to
Finally, right-click the Height column heading and select Sort Ascending. The table is
sorted by the values of the Height column from smallest to largest.
19
While youve been selecting options and customizing the table to get it just the way you
want it, SAS Studio has been generating SAS code that you can use. To view the code,
on the toolbar. A new program window appears with the code that was used to
click
create the view of the table in the table viewer.
This program is a copy of the code that SAS Studio created and is no longer associated
with the table viewer. Editing this program does not affect the data that is displayed in
the table viewer, and modifying the table viewer does not affect the contents of this
code. You can edit this code, or use it as the basis for another program.
21
5
Additional Information
For More Information . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
23
Recommended Reading
n
24 Recommended Reading