SlideShare uma empresa 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!

Mais conteĂșdo relacionado

Último

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Christopher Logan Kennedy
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxRemote DBA Services
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...apidays
 

Último (20)

Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
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
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
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
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 

Destaque

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
 

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