SAS Macros of Performing Look-Ahead and Look-Back Reads

Similar documents
Please Don't Lag Behind LAG!

Let SAS Help You Easily Find and Access Your Folders and Files

A Macro that can Search and Replace String in your SAS Programs

Paper Leads and Lags: Static and Dynamic Queues in the SAS DATA STEP, 2 nd ed. Mark Keintz, Wharton Research Data Services

A Macro that Creates U.S Census Tracts Keyhole Markup Language Files for Google Map Use

Key Terms and Concepts. Introduction

A Macro that can Search and Replace String in your SAS Programs

Chapter 6: Modifying and Combining Data Sets

Customizing a Multi-Cell Graph Created with SAS ODS Graphics Designer Yanhong Liu, Cincinnati Children s Hospital Medical Center, Cincinnati, OH

Out of Control! A SAS Macro to Recalculate QC Statistics

IF there is a Better Way than IF-THEN

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

Importing CSV Data to All Character Variables Arthur L. Carpenter California Occidental Consultants, Anchorage, AK

A Tool to Compare Different Data Transfers Jun Wang, FMD K&L, Inc., Nanjing, China

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

Regaining Some Control Over ODS RTF Pagination When Using Proc Report Gary E. Moore, Moore Computing Services, Inc., Little Rock, Arkansas

Macro Method to use Google Maps and SAS to Geocode a Location by Name or Address

PharmaSUG Paper SP04

PharmaSUG China Paper 70

A Lazy Programmer s Macro for Descriptive Statistics Tables

The Proc Transpose Cookbook

Data Set Options. Specify a data set option in parentheses after a SAS data set name. To specify several data set options, separate them with spaces.

9 Ways to Join Two Datasets David Franklin, Independent Consultant, New Hampshire, USA

Amie Bissonett, inventiv Health Clinical, Minneapolis, MN

PharmaSUG Paper TT11

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

A Side of Hash for You To Dig Into

Breaking up (Axes) Isn t Hard to Do: An Updated Macro for Choosing Axis Breaks

PharmaSUG 2013 CC26 Automating the Labeling of X- Axis Sanjiv Ramalingam, Vertex Pharmaceuticals, Inc., Cambridge, MA

Using PROC SQL to Calculate FIRSTOBS David C. Tabano, Kaiser Permanente, Denver, CO

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

Countdown of the Top 10 Ways to Merge Data David Franklin, Independent Consultant, Litchfield, NH

Benchmark Macro %COMPARE Sreekanth Reddy Middela, MaxisIT Inc., Edison, NJ Venkata Sekhar Bhamidipati, Merck & Co., Inc.

Not Just Merge - Complex Derivation Made Easy by Hash Object

Plot Your Custom Regions on SAS Visual Analytics Geo Maps

Using PROC REPORT to Cross-Tabulate Multiple Response Items Patrick Thornton, SRI International, Menlo Park, CA

Remember to always check your simple SAS function code! Yingqiu Yvette Liu, Merck & Co. Inc., North Wales, PA

Combining Contiguous Events and Calculating Duration in Kaplan-Meier Analysis Using a Single Data Step

KEYWORDS Metadata, macro language, CALL EXECUTE, %NRSTR, %TSLIT

Files Arriving at an Inconvenient Time? Let SAS Process Your Files with FILEEXIST While You Sleep

Using PROC PLAN for Randomization Assignments

Merging Data Eight Different Ways

Tackling Unique Problems Using TWO SET Statements in ONE DATA Step. Ben Cochran, The Bedford Group, Raleigh, NC

Using SAS to Control and Automate a Multi SAS Program Process Patrick Halpin, dunnhumby USA, Cincinnati, OH

Beyond IF THEN ELSE: Techniques for Conditional Execution of SAS Code Joshua M. Horstman, Nested Loop Consulting, Indianapolis, IN

Gary L. Katsanis, Blue Cross and Blue Shield of the Rochester Area, Rochester, NY

Get Started Writing SAS Macros Luisa Hartman, Jane Liao, Merck Sharp & Dohme Corp.

Effects of PROC EXPAND Data Interpolation on Time Series Modeling When the Data are Volatile or Complex

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

Using SAS/SCL to Create Flexible Programs... A Super-Sized Macro Ellen Michaliszyn, College of American Pathologists, Northfield, IL

Wonderful Unix SAS Scripts: SAS Quickies for Dummies Raoul Bernal, Amgen Inc., Thousand Oaks, CA

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

A SAS Macro Utility to Modify and Validate RTF Outputs for Regional Analyses Jagan Mohan Achi, PPD, Austin, TX Joshua N. Winters, PPD, Rochester, NY

What Do You Mean My CSV Doesn t Match My SAS Dataset?

Optimizing System Performance

Simplifying Your %DO Loop with CALL EXECUTE Arthur Li, City of Hope National Medical Center, Duarte, CA

ABSTRACT INTRODUCTION MACRO. Paper RF

Interleaving a Dataset with Itself: How and Why

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

A Simple Framework for Sequentially Processing Hierarchical Data Sets for Large Surveys

Anatomy of a Merge Gone Wrong James Lew, Compu-Stat Consulting, Scarborough, ON, Canada Joshua Horstman, Nested Loop Consulting, Indianapolis, IN, USA

A SAS Solution to Create a Weekly Format Susan Bakken, Aimia, Plymouth, MN

Using a Control Dataset to Manage Production Compiled Macro Library Curtis E. Reid, Bureau of Labor Statistics, Washington, DC

CMISS the SAS Function You May Have Been MISSING Mira Shapiro, Analytic Designers LLC, Bethesda, MD

If You Need These OBS and These VARS, Then Drop IF, and Keep WHERE Jay Iyengar, Data Systems Consultants LLC

PharmaSUG Paper PO12

PharmaSUG Paper AD06

Using GSUBMIT command to customize the interface in SAS Xin Wang, Fountain Medical Technology Co., ltd, Nanjing, China

Tips for Mastering Relational Databases Using SAS/ACCESS

Using SAS Macros to Extract P-values from PROC FREQ

Are you Still Afraid of Using Arrays? Let s Explore their Advantages

Creating Macro Calls using Proc Freq

Ditch the Data Memo: Using Macro Variables and Outer Union Corresponding in PROC SQL to Create Data Set Summary Tables Andrea Shane MDRC, Oakland, CA

PharmaSUG Paper PO10

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

Oh Quartile, Where Art Thou? David Franklin, TheProgrammersCabin.com, Litchfield, NH

Essentials of PDV: Directing the Aim to Understanding the DATA Step! Arthur Xuejun Li, City of Hope National Medical Center, Duarte, CA

Cleaning Duplicate Observations on a Chessboard of Missing Values Mayrita Vitvitska, ClinOps, LLC, San Francisco, CA

Submitting SAS Code On The Side

SAS/ETS 14.2 User s Guide. The EXPAND Procedure

Automated Checking Of Multiple Files Kathyayini Tappeta, Percept Pharma Services, Bridgewater, NJ

3. Almost always use system options options compress =yes nocenter; /* mostly use */ options ps=9999 ls=200;

Chaining Logic in One Data Step Libing Shi, Ginny Rego Blue Cross Blue Shield of Massachusetts, Boston, MA

SAS IT Resource Management Forecasting. Setup Specification Document. A SAS White Paper

Creating and Executing Stored Compiled DATA Step Programs

SAS File Management. Improving Performance CHAPTER 37

Why choose between SAS Data Step and PROC SQL when you can have both?

David Beam, Systems Seminar Consultants, Inc., Madison, WI

One-Step Change from Baseline Calculations

Creating Code writing algorithms for producing n-lagged variables. Matt Bates, J.P. Morgan Chase, Columbus, OH

LST in Comparison Sanket Kale, Parexel International Inc., Durham, NC Sajin Johnny, Parexel International Inc., Durham, NC

A SAS/AF Application for Linking Demographic & Laboratory Data For Participants in Clinical & Epidemiologic Research Studies

Create a Format from a SAS Data Set Ruth Marisol Rivera, i3 Statprobe, Mexico City, Mexico

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

Validation Summary using SYSINFO

Quick and Efficient Way to Check the Transferred Data Divyaja Padamati, Eliassen Group Inc., North Carolina.

Streamline Table Lookup by Embedding HASH in FCMP Qing Liu, Eli Lilly & Company, Shanghai, China

Paper CC16. William E Benjamin Jr, Owl Computer Consultancy LLC, Phoenix, AZ

How to Use ARRAYs and DO Loops: Do I DO OVER or Do I DO i? Jennifer L Waller, Medical College of Georgia, Augusta, GA

2. Don t forget semicolons and RUN statements The two most common programming errors.

Transcription:

PharmaSUG 2018 - Paper QT-05 ABSTRACT SAS Macros of Performing Look-Ahead and Look-Back Reads Yanhong Liu, Cincinnati Children s Hospital Medical Center, Cincinnati, OH When working with the time series data, it is generally required use of previous observation(s) (Look- Back) or next observation(s) (Look-Ahead) for certain variable, in order to do some comparison or calculation. The SAS Lag function can be used to get the Look-Back reads, and we can use the Double SET statement to perform the Look-Ahead reads. If you have SAS/ETS installed, you can use PROC EXPAND as an alternative. In this paper, a few of macros are introduced to obtain one or more Look-back or Look-ahead observations with by-groups by implementing the above techniques. Additionally, this paper briefly compares the cost of the DATA STEP Macros to the PROC EXPAND Macros. INTRODUCTION There is often interest in creating variables based on how data for a given time period compares to the periods before and after when working with data across consistent units of time (years, quarters, months, days). If you have time series or longitudinal data, you wish to look across units of time within a single subject. The SAS LAG function can be used in the data step to perform computations across observations. It will return the values of a variable from previous observations. This is known as a lookback read. Even though there is no corresponding function to look ahead the next or subsequent observations, we can use the technique called Double SET statement to perform the Look-Ahead reads. When using those techniques to process across observations with data in groups, especially, if want to read more than one back or ahead values, we must carefully program how to detect and handle the group boundaries in order to avoid producing unexpected results. In this paper, a few of macros are introduced to retrieve one or more Look-back or Look-ahead observations with or without by-groups by using the Lag function or Double SET statement. However, PROC EXPAND provides an easy-to-use alternative to the data step if you have SAS/ETS installed. This paper also includes the macros by using the PROC EXPAND to generate the LAG and LEAD reads, but the result of the comparison for the cost of DATA STEP to PROC EXPAND may surprise you. MACRO PARAMETERS These macros all have 4 parameters. DATASET: Name of the input data set. GROUPBY: List of BY variables. The input data set is sorted by these variables. If there are no GROUPBY variables specified then input data set is in its given observation ordering. (At the beginning of each macro, a new data set will be created by adding 2 temporary variables _temp1 and _temp2 to the input data set, _temp1 is a dummy key, and _temp2 is a key of the original observation sequencing.) VAR: LOOKN: Name of the variable to be read ahead or read back The number of look- ahead or look-back reads 1

A SAMPLE DATA SET (SAMPLE. SAS7BDAT) Display 1. A Sample data set to be used for testing the Macros DATA STEP MACROS THE LOOKBACK MACRO SAS code listing: %macro lookback(dataset=, groupby=, LookN=, var= ); data lookback; proc sort data=lookback; by &groupby _temp1 _temp2; /*** Set up BYVAR: The by variable to determine the group boundary ***/ %if &groupby ne %then %do; %let byvar=%scan(&groupby,-1); %else %if &groupby= %then %do; %let byvar=_temp1; data &dataset._lookback(drop=i count _temp1 _temp2); do count = 1 by 1 until (last.&byvar); set lookback(keep=&groupby _temp1 _temp2 ); by &groupby _temp1 _temp2 ; /**Count the observations in the group specified by GROUPBY**/ do i = 1 to count; set lookback; &var._prev&j = lag&j( &var ); /**Function LAG is used for the look-back read**/ 2

if i-&j<=0 then &var._prev&j=.; /**Reset the value to missing when necessary**/ output; delete lookback; %m Display 2. The Macro LookBack SAS Code listing Example of calling the Macro %lookback: %lookback(dataset=sample, groupby=country product, lookn=3, var=sales); Display 3. The results of calling %lookback THE LOOKAHEAD MACRO SAS code listing: %macro lookahead(dataset=, groupby=, lookn=, var= ); data lookahead; proc sort data=lookahead; by &groupby _temp1 _temp2; 3

%if &groupby ne %then %do; %let byvar=%scan(&groupby,-1); %else %if &groupby= %then %do; %let byvar=_temp1; data &dataset._lookahead(drop=i count _temp1 _temp2); do count = 1 by 1 until (last.&byvar); set lookahead(keep=&groupby _temp1 _temp2); by &groupby _temp1 _temp2; /**Count the observations in the group specified by GROUPBY**/ do i = 1 to count; set lookahead; set lookahead ( firstobs = %eval(%eval(&j)+1) keep = &var rename = (&var = &var._next&j) ) lookahead ( obs = %eval(%eval(&j)) drop = _all_); /**Double SET statement is used for the look-ahead read**/ &var._next&j = ifn((count - i)<&j, (.), &var._next&j); /**Reset the value to missing when necessary**/ output; delete lookahead; %m Display 4. The Macro LookAhead SAS Code listing Example of calling the Macro %lookahead: %lookahead(dataset=sample, groupby=country product, lookn=3, var=sales); 4

Display 5. The results of calling %lookahead THE LOOKBOTH MACRO SAS code listing (combine LookBack and LookAhead): %macro lookboth(dataset=, groupby=, lookn=, var= ); data lookboth; proc sort data=lookboth; by &groupby _temp1 _temp2; %if &groupby ne %then %do; %let byvar=%scan(&groupby,-1); %else %if &groupby= %then %do; %let byvar=_temp1; data &dataset._lookboth(drop=i count _temp1 _temp2); do count = 1 by 1 until (last.&byvar); set lookboth(keep=&groupby _temp1 _temp2); by &groupby _temp1 _temp2; do i = 1 to count; set lookboth; /**Look Back**/ &var._prev&j = lag&j( &var ); if i-&j<=0 then &var._prev&j=.; /**Look Ahead**/ 5

set lookboth ( firstobs = %eval(%eval(&j)+1) keep = &var rename = (&var = &var._next&j) ) lookboth ( obs = %eval(%eval(&j)) drop = _all_); &var._next&j = ifn( (count - i) < &j, (.), &var._next&j ); output; delete lookboth; %m Display 6. The Macro LookBoth SAS Code listing Example of calling the Macro %lookboth: %lookboth(dataset=sample, groupby=country product, lookn=2, var=sales); Display 7. The results of calling %lookboth THE PROC EXPAND PROC EXPAND is one of the most useful procedures of SAS/ETS (Econometric Time Series). It has multiple functions includes creating lag, lead. PROC EXPAND is designed to obtain LAG and LEAD values by using the CONVERT statement. The METHOD=NONE option is used to suppress interpolation (SAS default to interpolating missing values using a cubic spline function). The general syntax for PROC EXPAND uses the following format: PROC EXPAND DATA=dataset OUT=out_dataset METHOD=NONE; CONVERT existing_variable = derived_variable / TRANSFORMOUT=(transformation); 6

BY Panel_id_variable; RUN; A simple example of using LAG or LEAD transformation: proc expand data=sample out = Sample_out(drop=time) method=none; by country product; convert sales; convert sales = sales_prev1 / transformout= (lag 1); convert sales =sales_next1 / transformout= (lead 1); Display 8. An example by using Proc Expand to get the LAG and LEAD reads THE LOOKBACK AND LOOKAHEAD MACROS BY USING PROC EXPAND %LOOKBACK_EXPAND %macro lookback_expand(dataset=, groupby=, lookn=, var=); data lookback; proc sort data=lookback; by &groupby _temp1 _temp2; proc expand data = lookback out = &dataset._lookback_expand(drop=time _temp1 _temp2) method=none; by &groupby _temp1; convert &var ; convert &var =&var._prev&j / transformout= (lag &j); 7

delete lookback; %m %LOOKAHEAD_EXPAND %macro lookahead_expand(dataset=, groupby=, lookn=, var=); data lookahead; proc sort data=lookahead; by &groupby _temp1 _temp2; proc expand data = lookahead out = &dataset._lookahead_expand(drop=time _temp1 _temp2) method=none; by &groupby _temp1; convert &var ; convert &var =&var._next&j / transformout= (lead &j); delete lookahead; %m %LOOKBOTH_EXPAND %macro lookboth_expand(dataset=, groupby=, lookn=, var=); data lookboth; proc sort data=lookboth; by &groupby _temp1 _temp2; proc expand data = lookboth out = &dataset._lookboth_expand(drop=time _temp1 _temp2) method=none; by &groupby _temp1; convert &var ; convert &var =&var._prev&j / transformout= (lag &j); convert &var =&var._next&j / transformout= (lead &j); delete lookboth; 8

%m Display 9. SAS Code listings of Look-Back or Look_Ahead Macros by using PROC EXPAND DATA STEP MACRO OR PROC EXPAND MACRO? ---The PROC EXPAND Macro Works only if you have a license of SAS ETS package ---The DATA STEP Macros have more codes to type, but process speed are much faster than the PROC EXPAND. SAS Code to compare the cost of both macros: options fullstimer; /**Samplex.sas7bdat has 100,000 observations***/ %let t1 = %sysfunc(time()); %LOOKBOTH_EXPAND(dataset=samplex, groupby=id drugname, lookn=3, var=dayofservice); %let t2 = %sysfunc(time()); %lookboth(dataset=samplex, groupby=id drugname, lookn=3, var=dayofservice); %let t3 = %sysfunc(time()); %let d2 = %sysfunc(round( %sysevalf(&t2 - &t1), 0.001)); %let d3 = %sysfunc(round( %sysevalf(&t3 - &t2), 0.001)); %put EXPAND: &d2, DataStep: &d3; Results: Display 10. Comparing the cost of Data Step to Proc Expand CONCLUSION The Macros introduced in this paper are very useful for the Look-Back or Look-Ahead reads, which can reduce the programming effort to avoid the unexpected results when performing across observations with data in groups. Especially, if want to retrieve more than one LAGs or LEADs. REFERENCES Matlapudi, Anjan, and J. Daniel Knapp (2010) Please Don t Lag Behind LAG. In the Proceedings of the 9

North East SAS Users Group.SAS sascommunity.org Look-Ahead and Look-Back http://www.sascommunity.org/wiki/look- Ahead_and_Look-Back Leonard Landry (2010) Look Before You Leap A Technique to Simulate a Look-Ahead Function. In the proceedings of the SAS Global Forum. ACKNOWLEDGMENTS The author wishes to thank the Division of Biostatistics and Epidemiology at Cincinnati Children's Hospital Medical Center for its support. CONTACT INFORMATION Your comments and questions are valued and encouraged. Contact the author at: Yanhong Liu Cincinnati Children s Hospital Medical Center 3333 Burnet Ave. Cincinnati, OH 45255 (513) 803-2614 E-mail: Yanhong.Liu@cchmc.org SAS and all other SAS Institute Inc. product or service names are registered trademarks or trademarks of SAS Institute Inc. in the USA and other countries. indicates USA registration. Other brand and product names are trademarks of their respective companies. 10