You may be able to speed up your SAS programs and programming by considering some simple changes to the way you think about your programming tasks. This presentation will share coding tricks, considerations regarding your data sources, and ways to automate processes to help you improve the efficiency of your SAS programs!
For more information, visit http://ockham.com/
More than Just Lines on a Map: Best Practices for U.S Bike Routes
Â
How To Write Efficient SAS Programs: Ten Handy Tips!
1. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
HOW TO WRITE EFFICIENT SAS PROGRAMS:
TEN HANDY TIPS!
PRESENTATION TO THE OCKHAM SAS USERS GROUP
APRIL 16, 2013
2. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
THE BASIC RULES FOR EFFICIENT SAS PROGRAMS:
⢠Work with as little data as possible
⢠Process as few instructions as possible
⢠Make the programs as reusable and flexible as possible to minimize
programmer effort.
LUCKILY, THE SAS PROGRAMMING LANGUAGE OFFERS MANY WAYS TO WRITE
EFFICIENT PROGRAMS.
3. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
THE BASIC RULES FOR EFFICIENT SAS PROGRAMS:
⢠Work with as little data as possible
⢠Process as few instructions as possible
⢠Make the programs as reusable and flexible as possible to minimize
programmer effort.
4. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
WHEN READING A SAS DATA SET, USE THE WHERE
STATEMENT TO FILTER YOUR DATA
⢠Less efficient: ⢠More efficient:
data new;
set old;
more statements here;
run;
data new;
set old;
where condition;
more statements here;
run;
Added efficiency: when using SAS/Access engines, SAS attempts to send the WHERE clause to
the RDBMS for evaluation rather than to SAS; with the IF statement, SAS must do the processing.
5. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
IF YOUâRE GOING TO RUN A PROCEDURE ON THE DATA,
USE THE WHERE STATEMENT IN THE PROCEDURE.
Less efficient: ⢠More efficient:
data new;
set old;
where city=âRaleigh';
run;
proc means data=new;
more statements here;
run;
proc means data=old;
where city=âRaleighâ;
more statements here;
run;
6. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
CREATING AN INDEX TO USE WITH THE WHERE
STATEMENT CAN SPEED THINGS UP EVEN MORE.
⢠Indexes can be created in the DATA step, in PROC CONTENTS or PROC
DATASETS, or in PROC SQL
⢠If feasible, sort the data on the indexed field
⢠Indexes do take up additional space, however.
7. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
1. usually bench mark: a mark on a permanent object indicating elevation and
serving as a reference in topographic surveys and tidal observations
2. a: a point of reference from which measurements may be made
b: something that serves as a standard by which others may be measured
or judged
c: a standardized problem or test that serves as a basis for evaluation or
comparison (as of computer system performance)
bench¡mark
noun Ëbench-Ëmärk
8. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
BENCHMARKS
⢠The programs were run on a laptop with Windows 7 Enterprise (64-bit)
⢠I turned on option FULLSTIMER;
⢠The programs were run 3x each (with SAS shut down between each run),
and I used averages for comparison
9. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
BENCHMARKS
⢠Results: User CPU Time (SAS processing time)
Program 1 Program 2
WHERE stmt WHERE with Index
(sorted)
.14 second .12 second
⢠Results: System CPU Time (peripheral activities - memory, I/O, etc.)
Program 1 Program 2
WHERE stmt WHERE with Index
(sorted)
.21 second .09 second
10. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
SELECT ONLY THE COLUMNS YOU NEED WHEN WORKING
WITH SAS DATA.
⢠Less efficient: ⢠More efficient:
data new;
set old;
more statements here;
run;
data new;
set old (drop=category
type value ...);
more statements here;
run;
Variations:
⢠Use the keep= option if you need to keep more variables than you need to drop!
⢠Use both keep= and drop= options to control variables on both the incoming and outgoing
sides!
⢠Keep= and drop= options can be used in PROC steps, too!
11. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
BENCHMARKS
Kept one out of eleven variables
Results: User CPU Time
Program 1 Program 2
With KEEP= option Without KEEP= option
.12 second .20 second
Results: System CPU Time
Program 1 Program 2
With KEEP= option Without KEEP= option
.12 second .28 second
12. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
THE BASIC RULES FOR EFFICIENT SAS PROGRAMS:
⢠Work with as little data as possible
⢠Process as few instructions as possible
⢠Make the programs as reusable and flexible as possible to minimize
programmer effort.
13. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
USE IF-THEN-ELSE INSTEAD OF IF-IF-IF
Less efficient:
More efficient:
data new;
set old;
if condition then
some action;
if condition then
some other action;
if condition then
some other action;
run;
data new;
set old;
if condition then
some action;
else if condition then
some other action;
else if condition then
some other action;
run;
Added efficiency: rank the order in which condition takes place and order the if / else-if statements
accordingly!
14. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
MINIMIZE THE NUMBER OF TIMES YOU READ YOUR DATA.
⢠Less efficient: ⢠More efficient:
data a;
set old;
[more code]
run;
data b;
set old;
[more code]
run;
data c;
set old;
[more code]
run;
data a b c;
set old;
if condition then
output a;
else if condition then
output b;
else if condition then
output c;
run;
15. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
BENCHMARKS
⢠Results: User CPU Time
Program 1 Program 2
Read data once Read data multiple times
.30 second .92 second
ď§ Results: System CPU Time
Program 1 Program 2
Read data once Read data multiple times
.32 second .73 second
16. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
NO NEED TO âWAKE UPâ THE DATA â JUST USE IT!
⢠Less inefficient: ⢠More efficient:
data new;
set old;
run;
proc means data=new;
more statements here;
run;
proc means data=old;
more statements here;
run;
17. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
LIMIT THE NUMBER OF TIMES YOU SORT YOUR DATA
⢠SAS will check to see if the dataset is already sorted
⢠You can use the PRESORTED option to ensure the check is done.
or:
18. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
THE SAS LOG:
19. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
SOME DATA STEP AND PROCEDURE STATEMENTS
REQUIRE THE DATA TO BE SORTED; OTHERS DO NOT.
⢠DATA step with SET or MERGE
and BY statements
⢠BY statement in PROC MEANS,
PROC FREQ, etc.
⢠others
Requires sorting Does not require sorting
⢠PROC SQL joins
⢠CLASS statement in PROC
MEANS, PROC FREQ, etc.
⢠others
20. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
THE BASIC RULES FOR EFFICIENT SAS PROGRAMS:
⢠Work with as little data as possible
⢠Process as few instructions as possible
⢠Make the programs as reusable and flexible as possible to minimize
programmer effort.
21. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
COMMENT YOUR PROGRAMS
⢠You think youâll remember what the program does, but you wonât.
⢠Someone else may inherit your programs, and comments will make the
process of interpreting what they do, a lot easier.
⢠Method #1:
/* your comment here */
⢠Method #2:
* your comment here;
22. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
EXAMPLE OF A COMMENT
23. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
EXAMPLE OF A COMMENT
24. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
MAKE THINGS EASIER FOR YOURSELF: PUT ALL
âGLOBALâ STATEMENTS AT THE BEGINNING OF YOUR
CODE, AND ALL âDEFINITIONSâ OUTSIDE OF YOUR CODE
⢠Libname statements, system options, and title statements are easier to find
(and change, if necessary) if they are all in one place.
⢠Macro definitions and format definitions should not be included within your
SAS programs. If they are stored as separate programs (or in macro libraries)
they will be easier to find and easier to change if necessary.
25. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
⢠Be âGREENâ â save code and reuse it later!
⢠Collaborate with your co-workers to share tips and suggestions
⢠Meet regularly to share ideas
⢠Some ways SAS code fosters reusability:
⢠Format library
⢠Macro library
⢠Stored processes
⢠User-written functions and procedures.
MAKE THINGS EASIER FOR YOURSELF: EFFICIENCY ALSO
MEANS WORKING SMARTER!
26. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
WHAT OTHER IDEAS DO YOU HAVE?
27. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
ADDITIONAL RESOURCES
⢠SAS Communities
⢠Your peers and coworkers
⢠Your in-house SAS User Group!
28. Copyr ight Š 2013, SAS Institute Inc. All rights reser ved.
sas.com
THANK YOU FOR BEING A SAS CUSTOMER!