Presentation is loading. Please wait.

Presentation is loading. Please wait.

Please Don't Lag Behind LAG! Anjan Matlapudi and J. Daniel Knapp Pharmacy Informatics and Finance PerformRx.

Similar presentations


Presentation on theme: "Please Don't Lag Behind LAG! Anjan Matlapudi and J. Daniel Knapp Pharmacy Informatics and Finance PerformRx."— Presentation transcript:

1 Please Don't Lag Behind LAG! Anjan Matlapudi and J. Daniel Knapp Pharmacy Informatics and Finance PerformRx

2 Introduction Lag is a common word meaning to fail to keep up or to fall behind. Lag is a common word meaning to fail to keep up or to fall behind. - From Wikipedia - From Wikipedia Lag: fail to maintain a desired pace or to keep up; fall or stay behind: After five minutes of hard running, some of them began to lag. Lag: fail to maintain a desired pace or to keep up; fall or stay behind: After five minutes of hard running, some of them began to lag. A person who lags behind, is the last to arrive, etc. - From Dictionary.com A person who lags behind, is the last to arrive, etc. - From Dictionary.com

3 Simple Example DayTempLagLead Yesterday70 Today Tomorrow80

4 Why We need the Lag Function ? Useful function while computing across observations. Useful function while computing across observations. Compute observations with reference to previous date and time functions. Compute observations with reference to previous date and time functions. Lag is the powerful function when we use with other SAS functions such as DIF,INTCK, RETAIN etc.. Lag is the powerful function when we use with other SAS functions such as DIF,INTCK, RETAIN etc.. Simplify our code and easy to manipulate DATA. Simplify our code and easy to manipulate DATA.

5 Syntax data TempTbl; set Dailytemp; lag lag_qty = lag(qty); run;

6 Table 1 Member ID Drug Fill Date Drug Name Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC40

7 Example 1 data Example1; set Table1; lag lag_qty = lag(qty); run;

8 Example 1 Member ID Drug Fill Date Drug Name Qty Lag Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC4030

9 Example 1 data Example1; set Table1; lag lag_qty = lag(qty); by MemberId; run;

10 Table 1 Member ID Drug Fill Date Drug Name Qty Lag Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC4030

11 Example 1 data Example1; set Table1; lag lag_qty = lag(qty); by MemberId; lag If not first.MemberId then lag_qty = lag(qty); run;

12 Wrong Results Wrong Results Member ID Drug Fill Date Drug Name Qty Lag Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC4020

13 Question What is the Reason Behind ?

14 Memory Allocation in Queue line 1 line. line 1 line 2 line 3 line 2 line 3 line 4 line 5 line 4 Observation 1 Observation 2 Observation 3 Observation 4 Observation 5 Observation. Observation 1 Observation 2 Observation 3 Observation 4 line 1 line. line 1 line 2 line. line 1 line 2 line 3 line 4 line 2 line 3 line 5 line 4 line 3 Observation 1 Observation 2 Observation 3 Observation 4 Observation 5 Observation. Observation 1 Observation 2 Observation 3 LAG 1 LAG 2 Input data set output data set

15 Example 1 data Example1; set Table1; lag lag_qty = lag(qty); by MemberId; If first.MemberId then Qty = lag_qty; run;. ;

16 Correct Results Member ID Drug Fill Date Drug Name Qty Lag Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC4030

17 Iterations data Example1; set Table1; lag prevQty = lag(qty); lag prevQty1 = lag(qty); lag prevQty2 = lag(qty); lag prevQty3 = lag(qty); lag prevQty4 = lag(qty); lag prevQty5 = lag(qty); lag prevQty6 = lag(qty); lag prevQty7 = lag(qty); lag prevQty8 = lag(qty); run;

18 Output Results Mem ber ID Drug Fill Date Drug Name QtyPrevQty PrevQty 1 PrevQty 2 PrevQty 3 PrevQty 4 PrevQty 5 PrevQty 6 PrevQty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC

19 Best Practice To understand data and make sure to deal with missing values. To understand data and make sure to deal with missing values. Proc Freq is the best way to check missing values in the data. Proc Freq is the best way to check missing values in the data. Freq Proc Freq data = Example1; Table MemberId*FillDate/Missing List; Run;

20 Question Does LEAD Functions Exists in SAS ® ?

21 LEAD Example data LeadTbl; merge Table1 Table1(firstobs=2 keep=qty rename=(qty=lead_qty)); run;

22 LEAD Output LEAD Output Member ID Drug Fill Date Drug Name Qty Lead Qty /01/2010ABILIFY /16/2010ABILIFY /16/2010ABILIFY /30/2010ABILIFY /04/2010GLEEVEC /10/2010GLEEVEC /15/2010GLEEVEC /01/2010GLEEVEC40

23 Question Does LAG/LEAD Functions Works in PROC SQL ?

24 LAG in Proc SQL proc sql; select DrugName,qty, LAG(qty,1) as prevQty from Example1; quit;

25 Log Output 520 proc sql; 521 select drug, qty, 522 LAG(qty,1) as prevQty 523 from demo; ERROR: The LAG function is not supported in PROC SQL, it is only valid within the DATA step. 524 quit; NOTE: The SAS System stopped processing this step because of errors.*;

26 LEAD in Proc SQL proc sql; select DrugName,qty, LEAD(qty,1) as LeadQty from Example1; quit;

27 Log Output 520 proc sql; 521 select drug, qty, 522 LEAD(qty,1) as NextQty 523 from Example1; ERROR: The LEAD Could not be Located. 524 quit; NOTE: The SAS System stopped processing this step because of errors.*;

28 Proc Expand proc expand data = Table1 out = Leadtbl method=none; by MemberId; convert Qty =lead_qty / transformout= (lead); convert Qty = lag_qty / transformout= (lag); convert Qty =lead2_qty / transformout= (lead 2); convert Qty =lag2_qty / transformout= (lag 2); run;

29 LAG/Lead Output Results LAG/Lead Output Results Member ID Drug Name QtyLag1Lag2LeadLead2 1111ABILIFY ABILIFY ABILIFY ABILIFY GLEEVEC GLEEVEC GLEEVEC GLEEVEC

30 Table -2 LAG With INTCK function Example Table -2 LAG With INTCK function Example Member ID Drug Fill Date Drug Name Qty /01/2010ABILIFY /15/2010AMBIEN /16/2010ALORA /01/2010ASPRIN /04/2010TYLENOL /10/2010ABILIFY /15/2010AMOXIL /01/2010DIXOL20

31 INTCK Function data singlelPrescriptions DualPrescriptions; set Table2; by MemberID DrugFill_Date; *----Output single prescriptions----*; if first.MemberID and last.MemberID then output singlelPrescriptions; else do; lag_memberid = lag (MemberID); lag_memberid = lag (MemberID); lag_drugFill_date = lag (DrugFill_Date); lag_drugFill_date = lag (DrugFill_Date); if lag_memberid = MemberID then do; intckag_drugFill_dat DrugFill_Date numdays= intck ('DAY', lag_drugFill_date, DrugFill_Date ); if numdays < 30 then star = "*";-Output with '*' flag; end; output DualPrescriptions; end; run;

32 LAG with INTCK Output LAG with INTCK Output Member ID Drug Fill Date Drug Name Qty Num Days Star /01/2010ABILIFY /15/2010AMBIEN2014* /16/2010ALORA /10/2010ABILIFY /15/2010AMOXIL205* /01/2010DOXIL20136

33 Table -3 LAG With DIFF function Example Member ID Drug Fill Date Drug Name Price /01/2007ABILIFY$ /15/2008ABILIFY$ /16/2009ABILIFY$ /01/2010ABILIFY$ /04/2007GLEEVEC$ /10/2008GLEEVEC$ /15/2009GLEEVEC$ /01/2010GLEEVEC$90

34 DIF Function data DifTbl; set Table3; by MemberID DrugFill_Date; diff_drugFill_date = dif(DrugFill_Date); lag_price = lag(price); lag_price = lag(price); dif_price = dif(price); dif_price = dif(price); if first.memberid then do; diff_drugFill_date =.; lag_price =.; dif_price =.; end; if diff_drugFill_date > 365 then do; dif_price/lag_price percent_increase = round(( dif_price/lag_price )*100) ; end; run;

35 LAG with DIF Output LAG with DIF Output Member ID Drug Name Price Fill Date Lag Price Dif Price Per Increase 1111ABILIFY$ ABILIFY$ % 1111ABILIFY$ % 1111ABILIFY$ GLEEVEC$ GLEEVEC$ % 2222GLEEVEC$ % 2222GLEEVEC$

36 Table – 4 LAG With RETAIN function Example Member ID Drug Fill Date Drug Name Qty Lag Price /01/2007ABILIFY10$ /15/2008ABILIFY /16/2009ABILIFY /01/2010ABILIFY /04/2007GLEEVEC10$ /10/2008GLEEVEC /15/2009GLEEVEC /01/2010GLEEVEC40

37 RETAIN Function data RetainTbl(rename=(Old_price=price)); set Table4; by MemberID ; if first.MemberID then old_price=price; retain retain old_price; if price EQ. then old_price=old_price; else old_price=price; lag_qty = lag (qty); lag_qty if first.MemberID then lag_qty =.; lag_qty else avgprice = mean(old_price*lag_qty); run;

38 LAG with RETAIN Output LAG with RETAIN Output Member ID Drug Fill Date Drug Name QTY Lag Price Price Var Lag Qty Avg Price /01/2007ABILIFY10$6.25$ /15/2008ABILIFY20$6.2510$ /16/2009ABILIFY30$6.2520$ /01/2010ABILIFY40$6.2530$ /04/2007GLEEVEC10$5.55$ /10/2008GLEEVEC20$5.5510$ /15/2009GLEEVEC30$5.5520$ /01/2010GLEEVEC40$5.5530$166.50

39 Conclusions We demonstrated the basic use the of LAG function. We hope The LAG Code example with other functions in this paper will be useful to the Base SAS ® programmers. We demonstrated the basic use the of LAG function. We hope The LAG Code example with other functions in this paper will be useful to the Base SAS ® programmers. We suggest careful look at the data structure and make sure to deal with null values and assign correctly when you use LAG function. We suggest careful look at the data structure and make sure to deal with null values and assign correctly when you use LAG function.

40 Kennett Square, PA Mushroom Capital of the World

41 Kennett Square, PA Mushroom Festival Mushroom Festival

42 Growing Mushrooms

43 Longwood Gardens

44 Thank you

45 Acknowledgements Mr. Shimels Afework, Sr. Director of our company for his support. Mr. Shimels Afework, Sr. Director of our company for his support. SAS ® indicates USA registration. Other brand and product names are registered trademarks or trademarks of their respective companies. SAS ® indicates USA registration. Other brand and product names are registered trademarks or trademarks of their respective companies.

46 Questions ? Anjan Matlapudi and J. Daniel Knapp PerformRx The Next Generation PBM, 200 Stevens Drive, Philadelphia, PA 19113

47 A Way to Work with Invoice Flat Files in SAS® A Way to Work with Invoice Flat Files in SAS® Tips to Use Character String Functions in Record Lookup Tips to Use Character String Functions in Record Lookup


Download ppt "Please Don't Lag Behind LAG! Anjan Matlapudi and J. Daniel Knapp Pharmacy Informatics and Finance PerformRx."

Similar presentations


Ads by Google