SlideShare une entreprise Scribd logo
1  sur  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!

Contenu connexe

Dernier

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
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
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
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
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
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
[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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 

Dernier (20)

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...
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
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
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
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...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
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...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
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
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
[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
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

En vedette

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
 

En vedette (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!