SlideShare una empresa de Scribd logo
1 de 28
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
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.
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.
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.
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;
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.
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
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
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
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!
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
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.
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!
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;
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
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;
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:
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
THE SAS LOG:
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
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.
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;
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
EXAMPLE OF A COMMENT
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
EXAMPLE OF A COMMENT
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.
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!
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
WHAT OTHER IDEAS DO YOU HAVE?
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
ADDITIONAL RESOURCES
• SAS Communities
• Your peers and coworkers
• Your in-house SAS User Group!
Copyr ight © 2013, SAS Institute Inc. All rights reser ved.
sas.com
THANK YOU FOR BEING A SAS CUSTOMER!

Más contenido relacionado

Último

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfPrecisely
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsPixlogix Infotech
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 

Último (20)

Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdfHyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
Hyperautomation and AI/ML: A Strategy for Digital Transformation Success.pdf
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
The Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and ConsThe Ultimate Guide to Choosing WordPress Pros and Cons
The Ultimate Guide to Choosing WordPress Pros and Cons
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 

Destacado

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfmarketingartwork
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 

Destacado (20)

AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
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!