Guidelines for Organizing SAS Code and Project Files

Similar documents
Suggestions for Organizing SAS Code and Project Files

Using Recursion for More Convenient Macros

Maintaining Formats when Exporting Data from SAS into Microsoft Excel

PROC SQL: Why and How

Reducing Credit Union Member Attrition with Predictive Analytics

The JMP Journal: An Analyst s Best Friend

Pros and Cons of Interactive SAS Mode vs. Batch Mode Irina Walsh, ClinOps, LLC, San Francisco, CA

Getting Correct Results from PROC REG

Purchase this book at

A Mass Symphony: Directing the Program Logs, Lists, and Outputs

How to Create Data-Driven Lists

Intermediate SAS: Working with Data

How to use UNIX commands in SAS code to read SAS logs

Clinical Data Visualization using TIBCO Spotfire and SAS

Electricity Forecasting Full Circle

Arthur L. Carpenter California Occidental Consultants, Oceanside, California

Efficient Processing of Long Lists of Variable Names

Procedure for Stamping Source File Information on SAS Output Elizabeth Molloy & Breda O'Connor, ICON Clinical Research

SAS Macro Programming Tips and Techniques

Acknowledgments xi Preface xiii About the Author xv About This Book xvii New in the Macro Language xxi

Building Sequential Programs for a Routine Task with Five SAS Techniques

Functions vs. Macros: A Comparison and Summary

Essential ODS Techniques for Creating Reports in PDF Patrick Thornton, SRI International, Menlo Park, CA

Writing Programs in SAS Data I/O in SAS

Implementing User-Friendly Macro Systems Ed Heaton Data & Analytic Solutions, Inc. Sarah Woodruff Westat

SAS Application Development Using Windows RAD Software for Front End

USING SAS HASH OBJECTS TO CUT DOWN PROCESSING TIME Girish Narayandas, Optum, Eden Prairie, MN

AN INTRODUCTION TO MACRO VARIABLES AND MACRO PROGRAMS Mike Zdeb, School of Public Health

Getting Up to Speed with PROC REPORT Kimberly LeBouton, K.J.L. Computing, Rossmoor, CA

Data Quality Review for Missing Values and Outliers

Better Metadata Through SAS II: %SYSFUNC, PROC DATASETS, and Dictionary Tables

WHAT ARE SASHELP VIEWS?

AP13 Why Write a Macro? Then Again, Why Not? John W. Davison, Jr., Factotum Inc., Leesburg, VA

Storing and Reusing Macros

PharmaSUG Paper AD09

Introduction to the SAS Macro Facility

Tales from the Help Desk 6: Solutions to Common SAS Tasks

Review of PC-SAS Batch Programming

ABSTRACT INTRODUCTION THE GENERAL FORM AND SIMPLE CODE

Open Problem for SUAVe User Group Meeting, November 26, 2013 (UVic)

Guidelines for Coding of SAS Programs Thomas J. Winn, Jr. Texas State Auditor s Office

Overview of Data Management Tasks (command file=datamgt.sas)

Utilizing SAS for Cross- Report Verification in a Clinical Trials Setting

Missing Pages Report. David Gray, PPD, Austin, TX Zhuo Chen, PPD, Austin, TX

Unlock SAS Code Automation with the Power of Macros

Introductory Guide to SAS:

A SAS Macro for Producing Benchmarks for Interpreting School Effect Sizes

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

Using SAS software to fulfil an FDA request for database documentation

More About SAS Macros

Out of Control! A SAS Macro to Recalculate QC Statistics

Dictionary.coumns is your friend while appending or moving data

CC13 An Automatic Process to Compare Files. Simon Lin, Merck & Co., Inc., Rahway, NJ Huei-Ling Chen, Merck & Co., Inc., Rahway, NJ

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

STATION

Integrating Large Datasets from Multiple Sources Calgary SAS Users Group (CSUG)

Chapter 1: Introduction to SAS

The Output Bundle: A Solution for a Fully Documented Program Run

SAS Drug Development Program Portability

PROC SQL VS. DATA STEP PROCESSING JEFF SIMPSON SAS CUSTOMER LOYALTY

Using UNIX Shell Scripting to Enhance Your SAS Programming Experience

Useful Tips from my Favorite SAS Resources

Creating Your Own Worksheet Formats in exporttoxl

PROC CATALOG, the Wish Book SAS Procedure Louise Hadden, Abt Associates Inc., Cambridge, MA

Prevent Pane - Moving from MENU & PROGRAM Entries to FRAME Entries Loretta Golby, ISM Alberta Serge Dupuis, BKD Software Consultants Edmonton, Alberta

An exercise in separating client-specific parameters from your program

User Activities. These reports give an overview of how your servers are being used, how many users connect, how many sessions you have etc.

Get Going with PROC SQL Richard Severino, Convergence CT, Honolulu, HI

Document and Enhance Your SAS Code, Data Sets, and Catalogs with SAS Functions, Macros, and SAS Metadata. Louise S. Hadden. Abt Associates Inc.

TLFs: Replaying Rather than Appending William Coar, Axio Research, Seattle, WA

So Much Data, So Little Time: Splitting Datasets For More Efficient Run Times and Meeting FDA Submission Guidelines

... ) city (city, cntyid, area, pop,.. )

Run your reports through that last loop to standardize the presentation attributes

SAS Macro Dynamics - From Simple Basics to Powerful Invocations Rick Andrews, Office of the Actuary, CMS, Baltimore, MD

Copy That! Using SAS to Create Directories and Duplicate Files

An Introduction to SAS/SHARE, By Example

Populating an ALFRED-type Database

Best Practices for Using the SAS Scalable Performance Data Server in a SAS Grid environment

Macro to compute best transform variable for the model

%MISSING: A SAS Macro to Report Missing Value Percentages for a Multi-Year Multi-File Information System

2. Referencing Files and Sepng Op+ons. GIORGIO RUSSOLILLO - Cours de prépara+on à la cer+fica+on SAS «Base Programming» 47

%check_codelist: A SAS macro to check SDTM domains against controlled terminology

Advanced Visualization using TIBCO Spotfire and SAS

Paper AP14 Modifying The LogParse PassInfo Macro to Provide a Link between Product Usage in Rtrace Log and Time Used in Job Log

Characteristics of a "Successful" Application.

An Introduction to Macros Deb Cassidy

PROC SQL vs. DATA Step Processing. T Winand, Customer Success Technical Team

Dynamic Macro Invocation Utility Paul D Sherman, San Jose, CA

The correct bibliographic citation for this manual is as follows: SAS Institute Inc Proc EXPLODE. Cary, NC: SAS Institute Inc.

SAS Macro Language: Reference

Useful Tips When Deploying SAS Code in a Production Environment

Autocall Macros A Quick Overview

Extending the Scope of Custom Transformations

SAS Display Manager Windows. For Windows

Base and Advance SAS

Working the System: Our Best SAS Options Patrick Thornton, SRI International, Menlo Park, CA Iuliana Barbalau, Adecco, Pleasanton, CA

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

ODS DOCUMENT, a practical example. Ruurd Bennink, OCS Consulting B.V., s-hertogenbosch, the Netherlands

Data movement issues: Explicit SQL Pass-Through can do the trick

Top Coding Tips. Neil Merchant Technical Specialist - SAS

Transcription:

Basic Organizational Ideas Guidelines for Organizing SAS Code and Project Files Nate Derby Stakana Analytics Seattle, WA Club des Utilisateurs SAS de Québec 11/1/16 Nate Derby Organizing SAS Files 1 / 26

Outline Basic Organizational Ideas 1 Basic Organizational Ideas 2 3 4 Why Use Recursion? Implementation in Three Steps Two Parameters 5 Nate Derby Organizing SAS Files 2 / 26

Basic Organizational Ideas Basic Organizational Ideas Never throw anything away. Know where to find everything. Make the code reusable. Automate as much as possible. Nate Derby Organizing SAS Files 3 / 26

Basic Organizational Ideas Files should be organized by Company Projects Reports by Name by Date Given, plus title or description Nate Derby Organizing SAS Files 4 / 26

Basic Organizational Ideas Report vs. Project Report = relatively minor, one-time ad hoc. Project = repeatedly updated with new data. If it has a name, it s a project. Date rules: The date given (not assigned). If two turned in on same day, add a letter (10.07.22a, 10.07.22b). To keep chronological order: Use YY.MM.DD. Nate Derby Organizing SAS Files 5 / 26

Basic Organizational Ideas Within a Report Directory The calling program Subdirectories: Input Macros Output SASData Various other subdirectories? Nate Derby Organizing SAS Files 6 / 26

Basic Organizational Ideas Within a Project Directory The calling program Subdirectories: Input Macros Output SASData Various other subdirectories? Nate Derby Organizing SAS Files 7 / 26

Basic Organizational Ideas Calling Program for a Report (Figure 2, page 4) DM 'output' clear; DM 'log' clear; %LET root = C:\Files\RMS\Reports\09.10.17 Sales Forecasts; *where the root directory is located; OPTIONS SASAUTOS=( "&root\macros" ) MAUTOSOURCE; %makesetup; *makes the setup structures. FURTHER FUNCTIONALITY WILL NOT WORK IF COMMENTED OUT; *%readdata; %analyzedata; *%exportoutput; Nate Derby Organizing SAS Files 8 / 26

Basic Organizational Ideas Calling Program for a Project (Figure 2, page 4) DM 'output' clear; DM 'log' clear; %LET root = C:\Files\RMS\Projects\Forecasts; %LET exroot = C:\SAS\ExportToXL %LET orig = SEA; %LET dest = YQB; %LET datecut = 7/25/16; *cutoff date for the data; %LET hzn = 15; *forecast horizon; OPTIONS SASAUTOS=( "&root\macros", "&exroot" ) MAUTOSOURCE; %makesetup; *makes the setup structures. FURTHER FUNCTIONALITY WILL NOT WORK IF COMMENTED OUT; *%readformatdata; *reads and segments the input data set; %makeforecasts( fnumber=1542 ); *produces the forecasts, cycling through various forecasting methods; *%exportforecasts; *exports the forecasts onto Excel spreadsheets; %PUT DONE!; Nate Derby Organizing SAS Files 9 / 26

Basic Organizational Ideas The %makesetup Macro (Figure 3, page 5) %MACRO makesetup; OPTIONS ORIENTATION=landscape LINESIZE=150 PAGESIZE=60 NODATE NONUMBER MCOMPILENOTE=NONE NOTES SOURCE; %LOCAL dir1 dir2; %*these macro variables are only used here; %GLOBAL outputroot enddate; %*these macro variables ar used in other macros; %LET outputroot = &root\output; DATA _NULL_; dir1 = "'" 'mkdir' ' "' "&outputroot" '"' "'"; %*creates directory commands; dir2 = "'" 'mkdir' ' "' "&root\data" '"' "'"; CALL SYMPUTX( 'dir1', dir1 ); CALL SYMPUTX( 'dir2', dir2 ); CALL SYMPUTX( 'enddate', INPUT( "&datecut", mmddyy8. ) ); RUN; DATA _NULL_; %*makes the directories for the graphics; SYSTASK COMMAND &dir1 WAIT; SYSTASK COMMAND &dir2 WAIT; RUN; LIBNAME rms "&root\data"; %MEND makesetup; Nate Derby Organizing SAS Files 10 / 26

Basic Organizational Ideas What s Really Happening? The %makesetup Macro (Figure 3, page 5) %MACRO makesetup; OPTIONS ORIENTATION=landscape LINESIZE=150 PAGESIZE=60 NODATE NONUMBER MCOMPILENOTE=NONE NOTES SOURCE; %LOCAL dir1 dir2; %*these macro variables are only used here; %GLOBAL outputroot enddate; %*these macro variables ar used in other macros; %LET outputroot = &root\output; DATA _NULL_; dir1 = "'" 'mkdir' ' "' "&outputroot" '"' "'"; %*creates directory commands; dir2 = "'" 'mkdir' ' "' "&root\data" '"' "'"; CALL SYMPUTX( 'dir1', dir1 ); CALL SYMPUTX( 'dir2', dir2 ); CALL SYMPUTX( 'enddate', INPUT( "&datecut", mmddyy8. ) ); RUN; %PUT dir1=&dir1; %PUT dir2=&dir2; %PUT enddate=&enddate; DATA _NULL_; %*makes the directories for the graphics; SYSTASK COMMAND &dir1 WAIT; SYSTASK COMMAND &dir2 WAIT; RUN; Nate Derby Organizing SAS Files 11 / 26

Basic Organizational Ideas What s Really Happening? Calling Program for a Project (Figure 2, page 4) DM 'output' clear; DM 'log' clear; %LET root = C:\Files\RMS\Projects\Forecasts; %LET exroot = C:\SAS\ExportToXL %LET orig = SEA; %LET dest = YQB; %LET datecut = 7/25/16; *cutoff date for the data; %LET hzn = 15; *forecast horizon; OPTIONS SASAUTOS=( "&root\macros", "&exroot" ) MAUTOSOURCE; %makesetup; *makes the setup structures. FURTHER FUNCTIONALITY WILL NOT WORK IF COMMENTED OUT; Log Output dir1='mkdir "C:\Files\RMS\Projects\Forecast\Output"' dir2='mkdir "C:\Files\RMS\Projects\Forecast\Data"' enddate=20660 Nate Derby Organizing SAS Files 12 / 26

Basic Organizational Ideas What s Really Happening? SAS Code %LET root = C:\Files\RMS\Projects\Forecasts; %LET datecut = 7/25/16; %LET outputroot = &root\output; DATA _NULL_; dir1 = "'" 'mkdir' ' "' "&outputroot" '"' "'"; dir2 = "'" 'mkdir' ' "' "&root\data" '"' "'"; CALL SYMPUTX( 'dir1', dir1 ); CALL SYMPUTX( 'dir2', dir2 ); CALL SYMPUTX( 'enddate', INPUT( "&datecut", mmddyy8. ) ); RUN; DATA _NULL_; %*makes the directories for the graphics; SYSTASK COMMAND &dir1 WAIT; SYSTASK COMMAND &dir2 WAIT; RUN; Log Output dir1='mkdir "C:\Files\RMS\Projects\Forecast\Output"' dir2='mkdir "C:\Files\RMS\Projects\Forecast\Data"' enddate=20660 Nate Derby Organizing SAS Files 13 / 26

Basic Idea Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Suppose we want to make forecasts for flight 1542: %makeforecasts( fnumber=1542 ); Now we want to make forecasts for all flights: %makeforecasts( fnumber=1542 ); %makeforecasts( fnumber=1543 ); %makeforecasts( fnumber=1544 ); %makeforecasts( fnumber=1545 ); %makeforecasts( fnumber=1546 ); Problems with above: We have to list them out individually. We have to find the right flight numbers. Nate Derby Organizing SAS Files 14 / 26

Better Idea Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters To make forecasts for flight 1542: %makeforecasts( fnumber=1542 ); To make forecasts for all flights: %makeforecasts; Flight numbers are determined, listed automatically. Easy for testing (test for one before performing for all) Easy for drill-down (perform for all, then in depth for one) Nate Derby Organizing SAS Files 15 / 26

Step One Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Define the macro for parameter: %makeforecasts %MACRO makeforecasts( fnumber ); [Code for making forecasts] %MEND makeforecasts; Nate Derby Organizing SAS Files 16 / 26

Step One Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Really easy example: %makeforecasts %MACRO makeforecasts( fnumber ); %PUT fnumber=&fnumber; %MEND makeforecasts; Nate Derby Organizing SAS Files 17 / 26

Step Two Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Step 2: Create auxiliary macro for getting flight numbers: %getflightnumbers %MACRO getflightnumbers; PROC SQL NOPRINT; SELECT DISTINCT flightnumber INTO :fnumbers SEPARATED BY '' FROM datasource WHERE orig="&orig" AND dest="&dest" ORDER BY by flightnumber; QUIT; %MEND getflightnumbers; Nate Derby Organizing SAS Files 18 / 26

Basic Organizational Ideas Step Three: Putting It All Together %makeforecasts %MACRO makeforecasts( fnumber=all ); %LOCAL i n fnumbers; %IF &fnumber = all %THEN %DO; %getflightnumbers; Why Use Recursion? Implementation in Three Steps Two Parameters %LET i = 1; %DO %WHILE( %LENGTH( %SCAN( &fnumbers, &i ) ) > 0 ); %LOCAL fnumber&i; %LET fnumber&i = %SCAN( &fnumbers, &i ); %LET i = %EVAL( &i + 1 ); %END; %LET n = %EVAL( &i - 1 ); %DO i=1 %TO &n; %makeforecasts( fnumber=&&fnumber&i ); %END; %GOTO theend; %END; %PUT fnumber=&fnumber; %theend: Nate Derby Organizing SAS Files 19 / 26

Does It Work? Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Macro call Log output %makeforecasts; fnumber=1542 fnumber=1543 fnumber=1544 fnumber=1545 fnumber=1546 %makeforecasts( fnumber=1542 ); fnumber=1542 Nate Derby Organizing SAS Files 20 / 26

Two Parameters Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters %makeforecasts %MACRO makeforecasts( fnumber=all, method=all ); %LOCAL i n1 n2 fnumbers methods; %IF &fnumber = all %THEN %DO; %getflightnumbers; %LET i = 1; %DO %WHILE( %LENGTH( %SCAN( &fnumbers, &i ) ) > 0 ); %LOCAL fnumber&i; %LET fnumber&i = %SCAN( &fnumbers, &i ); %LET i = %EVAL( &i + 1 ); %END; %LET n1 = %EVAL( &i - 1 ); %DO i=1 %TO &n1; %makeforecasts( fnumber=&&fnumber&i, method=&method ); %END; %GOTO theend; %END;... Nate Derby Organizing SAS Files 21 / 26

Two Parameters Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters %makeforecasts... %IF &method = all %THEN %DO; %LET methods = AddPick ExSm ARIMA; %LET i = 1; %DO %WHILE( %LENGTH( %SCAN( &methods, &i ) ) > 0 ); %LOCAL method&i; %LET method&i = %SCAN( &methods, &i ); %LET i = %EVAL( &i + 1 ); %END; %LET n2 = %EVAL( &i - 1 ); %DO i=1 %TO &n2; %makeforecasts( fnumber=&fnumber, method=&&method&i ); %END; %GOTO theend; %END; %PUT fnumber=&fnumber; %theend: %MEND makeforecasts; Nate Derby Organizing SAS Files 22 / 26

Two Parameters Macro call Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Log output %makeforecasts; fnumber=1542, method=addpick fnumber=1542, method=exsm fnumber=1542, method=arima fnumber=1543, method=addpick fnumber=1543, method=exsm fnumber=1543, method=arima fnumber=1544, method=addpick fnumber=1544, method=exsm fnumber=1544, method=arima fnumber=1545, method=addpick fnumber=1545, method=exsm fnumber=1545, method=arima fnumber=1546, method=addpick fnumber=1546, method=exsm fnumber=1546, method=arima Nate Derby Organizing SAS Files 23 / 26

Two Parameters Basic Organizational Ideas Why Use Recursion? Implementation in Three Steps Two Parameters Macro call Log output %makeforecasts( method=arima ); fnumber=1542, method=arima fnumber=1543, method=arima fnumber=1544, method=arima fnumber=1545, method=arima fnumber=1546, method=arima %makeforecasts( fnumber=1542 ); fnumber=1542, method=addpick fnumber=1542, method=exsm fnumber=1542, method=arima %makeforecasts( fnumber=1542, method=arima ); fnumber=1542, method=arima Nate Derby Organizing SAS Files 24 / 26

Basic Organizational Ideas Effective code organization incorporates reusability and automation. A recursive definition can make a macro more convenient. Recursion can be applied to one or more parameters. It can work especially well within a larger framework. Nate Derby Organizing SAS Files 25 / 26

Appendix Further Resources Kirk Paul Lafler. Efficient SAS Programming Techniques. Proceedings of the 25th SUGI Conference, 146-25, 2000. Thomas J. Winn Jr. Guidelines for Coding of SAS Programs. Proceedings of the 29th SUGI Conference, 258-29, 2004. Art Carpenter. Carpenter s Complete Guide to the SAS Macro Language, 3rd Edition. SAS Press, 2016. nderby@stakana.com Nate Derby Organizing SAS Files 26 / 26