SlideShare ist ein Scribd-Unternehmen logo
1 von 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!

Weitere ähnliche Inhalte

KĂźrzlich hochgeladen

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Miguel AraĂşjo
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 

KĂźrzlich hochgeladen (20)

Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 

Empfohlen

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
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data ScienceChristy Abraham Joy
 
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
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slidesAlireza Esmikhani
 

Empfohlen (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!