Presentation is loading. Please wait.

Presentation is loading. Please wait.

Please Don't Lag Behind LAG!

Similar presentations


Presentation on theme: "Please Don't Lag Behind LAG!"— 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. - 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. A person who lags behind, is the last to arrive, etc From Dictionary.com

3 Simple Example Day Temp Lag Lead Yesterday 70 Today 60 80 Tomorrow

4 Why We need the Lag Function ?
Useful function while computing across observations. 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.. Simplify our code and easy to manipulate DATA.

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

6 Table 1 Member ID Drug Fill Date Drug Name Qty 1111 01/01/2010 ABILIFY
01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

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

8 Example 1 Member ID Drug Fill Date Drug Name Qty Lag Qty 1111
01/01/2010 ABILIFY 10 . 01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

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

10 Table 1 Member ID Drug Fill Date Drug Name Qty Lag Qty 1111 01/01/2010
ABILIFY 10 . 01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

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

12 Wrong Results Member ID Drug Fill Date Drug Name Qty Lag Qty 1111
01/01/2010 ABILIFY 10 . 01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

13 What is the Reason Behind
Question What is the Reason Behind ?

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

15 Example 1 data Example1; set Table1; 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 1111
01/01/2010 ABILIFY 10 . 01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

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

18 Output Results Member ID Drug Fill Date Drug Name Qty PrevQty PrevQty1
7 1111 01/01/2010 ABILIFY 10 . 01/16/2010 20 03/16/2010 30 03/30/2010 40 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

19 Best Practice 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 data = Example1; Table MemberId*FillDate/Missing List; Run;

20 Does LEAD Functions Exists in SAS®
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 Member ID Drug Fill Date Drug Name Qty Lead Qty 1111
01/01/2010 ABILIFY 10 20 01/16/2010 30 03/16/2010 40 03/30/2010 2222 01/04/2010 GLEEVEC 02/10/2010 02/15/2010 07/01/2010

23 Does LAG/LEAD Functions Works in PROC SQL
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
Member ID Drug Name Qty Lag1 Lag2 Lead Lead2 1111 ABILIFY 10 . 20 30 40 2222 GLEEVEC

30 Table -2 LAG With INTCK function Example
Member ID Drug Fill Date Drug Name Qty 1111 01/01/2010 ABILIFY 10 01/15/2010 AMBIEN 20 03/16/2010 ALORA 5 2222 ASPRIN 1 3333 01/04/2010 TYLENOL 4444 02/10/2010 02/15/2010 AMOXIL 07/01/2010 DIXOL

31 INTCK Function lag_memberid = lag(MemberID);
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_drugFill_date = lag(DrugFill_Date); if lag_memberid = MemberID then numdays= intck('DAY',lag_drugFill_date, DrugFill_Date); if numdays < 30 then star = "*";-Output with '*' flag; end; output DualPrescriptions; run;

32 LAG with INTCK Output * Member ID Drug Fill Date Drug Name Qty
Num Days Star 1111 01/01/2010 ABILIFY 10 . 01/15/2010 AMBIEN 20 14 * 03/16/2010 ALORA 5 60 2222 02/10/2010 02/15/2010 AMOXIL 07/01/2010 DOXIL 136

33 Table -3 LAG With DIFF function Example
Member ID Drug Fill Date Drug Name Price 1111 01/01/2007 ABILIFY $30 01/15/2008 $60 10/16/2009 $80 07/01/2010 $90 2222 01/04/2007 GLEEVEC 02/10/2008 $40 02/15/2009

34 DIF Function lag_price = lag(price); dif_price = dif(price);
data DifTbl; set Table3; by MemberID DrugFill_Date; diff_drugFill_date = dif(DrugFill_Date); lag_price = lag(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; percent_increase = round((dif_price/lag_price)*100) ; run;

35 LAG with DIF Output Member ID Drug Name Price Fill Date Lag Price
Dif Price Per Increase 1111 ABILIFY $30 . $60 379 30 100% $80 640 60 20 33% $90 259 80 10 2222 GLEEVEC $40 402 674 40 50% 198

36 Table – 4 LAG With RETAIN function Example
Member ID Drug Fill Date Drug Name Qty Lag Price 1111 01/01/2007 ABILIFY 10 $6.25 01/15/2008 20 10/16/2009 30 07/01/2010 40 2222 01/04/2007 GLEEVEC $5.55 02/10/2008 02/15/2009

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

38 LAG with RETAIN Output Member ID Drug Fill Date Drug Name QTY
Lag Price Price Var Lag Qty Avg Price 1111 01/01/2007 ABILIFY 10 $6.25 . 01/15/2008 20 $62.50 10/16/2009 30 $125.00 07/01/2010 40 $187.50 2222 01/04/2007 GLEEVEC $5.55 02/10/2008 $55.55 02/15/2009 $111.00 $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 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. 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®
Tips to Use Character String Functions in Record Lookup


Download ppt "Please Don't Lag Behind LAG!"

Similar presentations


Ads by Google