Are Your SAS Programs Running You?

Similar documents
Are Your SAS Programs Running You? Marje Fecht, Prowerk Consulting, Cape Coral, FL Larry Stewart, SAS Institute Inc., Cary, NC

All About SAS Dates. Marje Fecht Senior Partner, Prowerk Consulting. Copyright 2017 Prowerk Consulting

Getting the Right DATES

SAS System Powers Web Measurement Solution at U S WEST

SAS 9 Programming Enhancements Marje Fecht, Prowerk Consulting Ltd Mississauga, Ontario, Canada

SAS Macro Programming for Beginners

Marketing Benchmark Survey 2004

Objectives Reading SAS Data Sets and Creating Variables Reading a SAS Data Set Reading a SAS Data Set onboard ia.dfwlax FirstClass Economy

Easing into Data Exploration, Reporting, and Analytics Using SAS Enterprise Guide

Once Written, Twice Applied: The Basics of Array Processing In SAS Elizabeth A. Roth, RAND Corporation, Santa Monica, CA

Substitute Quick Reference (SmartFindExpress Substitute Calling System and Web Center)

%Addval: A SAS Macro Which Completes the Cartesian Product of Dataset Observations for All Values of a Selected Set of Variables

Useful Tips When Deploying SAS Code in a Production Environment

SAS Scalable Performance Data Server 4.3

Journey to the center of the earth Deep understanding of SAS language processing mechanism Di Chen, SAS Beijing R&D, Beijing, China

You deserve ARRAYs; How to be more efficient using SAS!

Useful Tips from my Favorite SAS Resources

Plan Smart: Don t Leave Your End of Year Campaigns to Chance Convio, Inc. Page 1

Extending Ninox with NX

Payflow Implementer's Guide FAQs

Batch Scheduler. Version: 16.0

ERROR: The following columns were not found in the contributing table: vacation_allowed

View a Students Schedule Through Student Services Trigger:

Beginning Tutorials DATE HANDLING IN THE SAS SYSTEM. Bruce Gilsen, Federal Reserve Board

2017 ASSOCIATION MARKETING BENCHMARK REPORT

I KNOW HOW TO PROGRAM IN SAS HOW DO I NAVIGATE SAS ENTERPRISE GUIDE?

The Ins and Outs of %IF

Tips for Reducing Formula Size

WebEx Integration to Outlook. User Guide

ACT! Calendar to Excel

Paper SAS Managing Large Data with SAS Dynamic Cluster Table Transactions Guy Simpson, SAS Institute Inc., Cary, NC

SAS coding for those who like to be control

Scheduling WebEx Meetings with Microsoft Outlook

Billing PracticeMaster Financial. Tabs3 Connect Quick Guide

Quicker Than Merge? Kirby Cossey, Texas State Auditor s Office, Austin, Texas

PROGRAMMING ROLLING REGRESSIONS IN SAS MICHAEL D. BOLDIN, UNIVERSITY OF PENNSYLVANIA, PHILADELPHIA, PA

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

Modifying IPM Components

While You Were Sleeping - Scheduling SAS Jobs to Run Automatically Faron Kincheloe, Baylor University, Waco, TX

Claims Management - Delay analysis Online Workshop. Week No. 1: concept of claims

GreenThumb Garden Registration

SAS Application to Automate a Comprehensive Review of DEFINE and All of its Components

A Practical Introduction to SAS Data Integration Studio

Crystal Reports. Overview. Contents. Cross-Tab Capabilities & Limitations in Crystal Reports (CR) 6.x

Sudoku s resolution Using SAS Arrays

Make it a Date! Setting up a Master Date View in SAS

How Will the New Presort Data Update Schedule Impact Your Business?

From Manual to Automatic with Overdrive - Using SAS to Automate Report Generation Faron Kincheloe, Baylor University, Waco, TX

SUBSTITUTE EMPLOYEE WEB TIME INSTRUCTIONS

PROC FORMAT: USE OF THE CNTLIN OPTION FOR EFFICIENT PROGRAMMING

Exelon Career Opportunity System User Guide for External Applicants

PharmaSUG Paper PO12

Caterease User Training Guide

Holiday Messaging Trends and Best Practices

Sorting big datasets. Do we really need it? Daniil Shliakhov, Experis Clinical, Kharkiv, Ukraine

Calendar PPF Production Cycles Non-Production Activities and Events

PREREQUISITES FOR EXAMPLES

Nights & Weekend ROB & PHIL DECEMBER 12, 2008

ADVANCED ALGORITHMS TABLE OF CONTENTS

Store Locator for Magento 2. User Guide

Know What You Are Missing: How to Catalogue and Manage Missing Pieces of Historical Data

What Is SAS? CHAPTER 1 Essential Concepts of Base SAS Software

Webinar Benchmarks Report

Pathway Net User s Guide

AIMMS Function Reference - Date Time Related Identifiers

T.I.P.S. (Techniques and Information for Programming in SAS )

INFORMED CUSTOMERS: CUSTOMER EXPERIENCE DELIVERED

Lab Preparing for the ICND1 Exam

SAS Macro. SAS Training Courses. Amadeus Software Ltd

DEALER APPLICATION MAIL: DL WHOLESALE, INC LAS POSITAS RD, UNIT A LIVERMORE, CA 94551

Aitoc. Smart Reports User Manual for Magento

Customer Maintenance

Cheat sheet: Data Processing Optimization - for Pharma Analysts & Statisticians

What you learned so far. Loops & Arrays efficiency for statements while statements. Assignment Plan. Required Reading. Objective 2/3/2018

Calendar Excel Template User Guide

Guide Users along Information Pathways and Surf through the Data

Solar Eclipse Scheduler. Release 9.0

ADAMS USER GUIDE FOR CRICKETERS

August/September 2008

Different Methods for Accessing Non-SAS Data to Build and Incrementally Update That Data Warehouse

Example. Section: PS 709 Examples of Calculations of Reduced Hours of Work Last Revised: February 2017 Last Reviewed: February 2017 Next Review:

Macros I Use Every Day (And You Can, Too!)

San Diego Unified School District Substitute Reference Guide

Scheduling. Scheduling Tasks At Creation Time CHAPTER

WRITING RIGHT FOR THE WEB: SOCIAL MEDIA, MOBILE, AND TRADITIONAL SITES

DEC Computer Technology LESSON 6: DATABASES AND WEB SEARCH ENGINES

What to Expect When You Need to Make a Data Delivery... Helpful Tips and Techniques

Speed Dating: Looping Through a Table Using Dates

Title for SAS Global Forum 2014 Sample Paper

Title. Syntax. stata.com. datetime business calendars creation Business calendars creation

Full file at

Going Under the Hood: How Does the Macro Processor Really Work?

Paper HOW-06. Tricia Aanderud, And Data Inc, Raleigh, NC

Scheduling WebEx Meetings with Microsoft Outlook

Copyright/Trademark Information. Using This Guide

Registration Page your registration will provide you a link to this page along with your

Daily Math Week 8 ( ) Mon. October 7, 2013 Tues. October 8, 2013 Wed. October 9, 2013 Thurs. October 10, 2013 Fri.

WebEx Integration to Outlook for the Mac

1 Million. About. TMCnet Webinars Mean Business. Website Banner Ads Campaign. Article Viewers. Ideal Marketing Partner

Private Swimming Lessons

Transcription:

Overview Are Your SAS Programs Running You? Have you ever QUICKLY written some code assuming it will never be used again?? Is it now 5 years later and the SPAGHETTI CODE is still in production? Worse still does the code require YOUR time to tweak every time you run it? Marje Fecht Larry Stewart Senior Partner Sr. Director, Education Prowerk Consulting SAS Institute SAS is a registered trademark or trademark of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are registered trademarks or Trademarks of their respective companies. This tutorial focuses on maintenance free, efficient coding techniques so that you can spend your work time being more productive. Topics Part 1: Reduce your coding time and run time! -Remove Inefficient and Wordy Coding Part 2: Reduce Programmer Intervention -Use Macro Variables to avoid repetitive changes -Write Code that "thinks for itself" Part 3: Speed up Delivery Time -Focus on reusable code modules so you don't play the "copy and paste" game Part 4: Testing Tip Part 1: Reduce your coding time and run time! Business Task: You need to generate a report that requires subsetting a SAS data set sorting the resulting data set. Remove Inefficient Coding Subset the data prior to sorting data compare; set report; if ProductCode in ('XER', 'REF', 'CRS') and Date gt '01MAY2007'd; keep ProductCode Usage Date Location; proc sort data=compare; by ProductCode Date; All observations in the REPORT data set are read but only observations with specific ProductCode and Date values are required Use WHERE instead Data set is read and written at least twice Use one step instead of two

Remove Inefficient Coding Subset and sort the data in one step proc sort data=report (keep=productcode Usage Date Location) out=compare ; where ProductCode in ('XER','REF','CRS') and Date gt '01MAY2007'd ; by ProductCode Date; Part 1: Reduce your coding time and run time! Business Task: You need to create customer invoices. This means merging 3 SAS data sets that do not have a common BY variable. sug.orders PartNumber Quantity CustomerID sug.prices PartNumber UnitPrice sug.customers Company Address City State CustomerID Complicated proc sort data=sug.orders out=orders; Merge no by PartNumber; data step1; common BY merge Orders(in=inOrders) sug.prices; value. by PartNumber; if inorders; proc sort data=step1; proc sort data=sug.customers out=customers; data step2; merge step1(in=instep1) Customers; if instep1; InvoiceAmt=Quantity*UnitPrice; proc sort data=step2; by Company; proc print data=step2; var CustomerID Company Address City State PartNumber Quantity InvoiceAmt; Step 1: Merge the ORDERS and PRICES data sets by PartNumber and Complicated create the STEP1 data set. Merge no common BY value. proc sort data=sug.orders out=orders; by PartNumber; data step1; merge orders(in=inorders) sug.prices; by PartNumber; if inorders; Step 2: Merge the STEP1 data set with the CUSTOMERS data set by CustomerID. proc sort data=step1; proc sort data=sug.customers out=customers; data step2; merge step1(in=instep1) Customers; if instep1; InvoiceAmt=Quantity*UnitPrice; Complicated Merge no common BY value. Step 3: Sort the final data set by Company and print the data set. Complicated Merge no common BY value. proc sort data=step2; by Company; proc print data=step2; var CustomerID Company Address City State PartNumber Quantity InvoiceAmt;

Reduce Complex Coding Reduce Complex Coding VERY wordy and resource intensive Multiple steps to read and combine data Multiple SORTS of the same data Use SQL instead for fewer steps and less complex coding Solution: use SQL instead proc sql; select a.customerid, Company, Address, State, b.partnumber, Quantity, Quantity*UnitPrice as InvoiceAmt from sug.customers as a, sug.prices as b, sug.orders as c where a.customerid=c.customerid and b.partnumber=c.partnumber order by Company ; quit; Business Task You need to compute and compare revenue figures by day of the week. data work.revenue; DayOfWeek=weekday(date); proc format; value DayWk 1='Sunday' 2='Monday' 3='Tuesday' 4='Wednesday' 5='Thursday' 6='Friday' 7='Saturday'; proc tabulate data=work.revenue; class DayOfweek; var Revenue; tables DayOfWeek, Revenue; format DayOfWeek DayWk.; Compute total revenue by Day of Week Step 1: Create a variable that contains the day of the week. data work.revenue; DayOfWeek=weekday(Date); Step 2: Create a format that substitutes the name of the day for the day number. proc format; value DayWk 1='Sunday' 2='Monday' 3='Tuesday' 4='Wednesday' 5='Thursday' 6='Friday' 7='Saturday';

Step 3: Use the format when computing the totals. proc tabulate data=work.revenue; class DayOfweek; var Revenue; tables DayOfWeek, Revenue; format DayOfWeek DayWk.; Reduce Inefficient Coding Multiple steps when one is enough Creating a format when one already exists Use WEEKDATE9. format Original Date : 14MAY2007 SAS Date : 17300 WEEKDATE Format : Monday, May 14, 2007 WEEKDATE9 Format : Monday Reduce Inefficient Coding Part 2: Reduce Programmer Intervention Solution: Use WEEKDATE9. format proc tabulate data=work.revenue; class Date; var Revenue; tables Date, Revenue; format Date weekdate9.; Some programs are written too specifically to the task, and require constant intervention to specify dates / timeframes titles inclusion criteria. In extreme cases, this means scanning 1000's of lines of code. Part 2: Reduce Programmer Intervention Business Task: You need to run a daily report that compares current month to date revenue with prior month revenue. data currentmonth; if month(date) = 5 and year(date) = 2007; MonthYear = " 5/2007"; data lastmonth; if month(date) = 4 and year(date) = 2007; MonthYear = " 4/2007"; data comparemonths; set lastmonth currentmonth; proc means data=comparemonths; class MonthYear; var Revenue; title "MTD Revenue vs Last Month";

Reduce Intervention and Remove Inefficient Coding Dataset is read multiple times when only one read is required Use one step instead of two (or three) Monthly intervention required to change values Generalize the program so it is data driven by using macro variables and SAS functions Remove Inefficient Coding Solution 1: Reduce 3 DATA steps to just 1 data comparemonths; if month(date)=5 and year(date)=2007 then do; MonthYear = " 5/2007"; else if month(date)=4 and year(date)=2007 then do; MonthYear = " 4/2007"; *** proc means step unchanged; Remove Intervention Solution 2: Use SAS functions to make date decisoins data comparemonths; if month(date) = month(today()) and year(date) = year(today()) then do; MonthYear = put(today(), mmyys7.); else do; lastmonth=intnx('month', today(), -1); if month(date) = month(lastmonth) and year(date) = year(lastmonth) then do; MonthYear = put(lastmonth, mmyys7.); *** proc means step unchanged; Solution 3: Reduce function calls and handle "over midnight" runtime *** PART ONE OF SOLUTION 3 *** ; ** ONE TIME computation of Date at time program starts; %let DateToday = %sysfunc(today()); /* sas date value */ data comparemonths; *** create static values first time through the DATA step; retain mon_today yr_today MonthYear_today lastmonth mon_lastmonth yr_lastmonth MonthYear_lastmonth ; if _n_ = 1 then do; *** current month; mon_today = month(&datetoday); yr_today = year(&datetoday); MonthYear_today = put(&datetoday, mmyys7.); *** last month; lastmonth=intnx('month',&datetoday, -1); mon_lastmonth = month(lastmonth); yr_lastmonth = year(lastmonth); MonthYear_lastmonth =put(lastmonth,mmyys7.); *** CONTINUED ***; Solution 3: FOCUS on Key components ** ONE TIME computation of Date at time program starts; %let DateToday = %sysfunc(today()); /* sas date value */ < SNIP PART OF DATA STEP > if _n_ = 1 then do; *** current month; mon_today = month(&datetoday); yr_today = year(&datetoday); MonthYear_today = put(&datetoday, mmyys7.); *** last month; lastmonth =intnx('month',&datetoday, - 1); mon_lastmonth = month(lastmonth); yr_lastmonth = year(lastmonth); MonthYear_lastmonth =put(lastmonth,mmyys7.); Solution 3: continued... *** PART TWO OF SOLUTION 3 *** ; *** DATA STEP CONTINUED ***; *** Process Current Month Revenue; if month(date)= mon_today and year(date)= yr_today then do; MonthYear= MonthYear_today; *** Process Last Month Revenue; else do; if month(date)= mon_lastmonth and year(date)= yr_lastmonth then do; MonthYear = MonthYear_lastmonth; *** proc means step unchanged;

Two final suggestions Part 3: Speed up Delivery Time Consider two final improvements: Since only two months of data are required, consider a WHERE in the data step to reduce the amount of data processed Consider creating the 7 variables (at the top of the data step) as macro variables so that you can use them throughout the program. Business Task: You need to use the same query logic and reporting formats on a regular basis for each new product / campaign / region. The program is always the same, except for changes to date ranges inclusion codes titles and footnotes search logic. You make a copy of the most recent version you can find of the query / reporting program You step through the program and make all the changes that apply for your current use You run the program and OOPS you must have over-typed some quotes and semicolons You apply corrections, and try again. Reduce Intervention It is difficult to locate the most current version of the query/reporting program Store a single dated copy of the main program / Modules Updating values in the program often leads to errors, or missed parameters Use a driver program with macro variables as input and call the Modules Reduce Intervention Solution: A driver program specifies parameters and runs the standard source programs. *** Driver Program specify correct parameters; %let prestart = 01OCT2006; %let prestop = 31DEC2006; %let poststart = 01JAN2007; %let poststop = 30JUN2007; %let title = "January 2007 Introduction of Low Interest Rates"; %let products = ('VRS', 'GQL', 'BGO', 'DLA'); %let codes = ('015', '119', '214'); *** run standard reporting programs; %include 'CampaignReportExtract_20070110.sas'; %include 'CampaignReportOutput_20070113.sas'; Reduce Intervention Success! The Old Way Hours to locate / QA / etc The New Way Campaign reporting is ready with less than ½ hour of effort

Focus on Production Code! Only one problem remains when the source code changes, you have to find ALL the drivers that call it to change the version. Solution: Create a program that calls the latest source! CampaignReportExtract_CurrentPgm.sas *** call latest version of source program; %include 'CampaignReportExtract_20070110.sas'; Focus on Production Code! When source changes, your drivers always call the most current version. *** Driver Program specify correct parameters; %let prestart = 01OCT2006; %let prestop = 31DEC2006; %let poststart = 01JAN2007; %let poststop = 30JUN2007; %let title = "January 2007 Introduction of Low Interest Rates"; %let products = ('VRS', 'GQL', 'BGO', 'DLA'); %let codes = ('015', '119', '214'); *** run standard reporting programs; %include 'CampaignReportExtract_CurrentPgm.sas'; %include 'CampaignReportOutput_CurrentPgm.sas'; Business Task You need to run a daily revenue report that includes a monthly recap on the 1st of each month You have a reminder on the 1 st of each month to submit your monthly reporting programs You manually combine the reports from the daily and monthly output, and provide them to the viewers Reduce Intervention Reduce Intervention you must remember to submit the monthly program, unless you use a batch scheduler you are producing two separate reports, and the viewers may prefer a single report Use a single program with macro code to decide when to run the monthly logic and report Solution: Reduce intervention *** Run monthly report on first of each month; %macro dayone; %if %sysfunc(day( %sysfunc(today()) )) = 1 %then %do; %include 'monthly_report.sas'; % %m %dayone *** Run daily report every day; %include 'daily_report.sas';

Part 4: Testing Tip Conclusion Remember: COMPARE the before and after data sets to confirm comparability of solutions! *** Useful to compare 2 solutions; proc compare data=sales compare=sales2; As you learn more about SAS, you will find that there are numerous features that enable you to accomplish tasks easily and efficiently. In this presentation, you have seen tips to reduce how many times data is processed code reduction techniques suggestions to reduce or remove manual intervention to enable maintenance-free jobs. Marje Fecht marje.fecht@prowerk.com Senior Partner Prowerk Consulting