Hi Astounding, I am using a similar approach (createing and using Lag variables) to create a sum for a rolling 6,12 and 24 months in a single data step. So I have created about 24 Lag variables. In my data the continuity of the periods is also not guranteed so i have also created a logic to handle that. But I am facing problems in resetting the Lag variables for each BY group. The data has 1 row per customerid per period. It is not guranteed that for each customer we will have all the periods. So the task now is to sum the DPD_30_flag variable for a rolling 6 ,12 and 24 months. using the following code I am able to do the sum, but Can't reset it for every by group (which is what is needed). Also i am trying to use a do loop so that i dont have to write the same lines of code 24 times, but that not working either. Here is the Code %macro lag_status(var=,day180=,day365=,day730=); Data &var.(keep=customerid accountingperiod In_default dpd30_flag dpd60_flag dpd90_flag DPD_30_180 DPD_30_365 DPD_30_730 DPD_60_180 DPD_60_365 DPD_60_730 DPD_90_180 DPD_90_365 DPD_90_730); Set DPD_flags; By Customerid; /*Creating 24 Lag variables for AccountingPeriod*/ Period1=lag1(accountingperiod); Period2=lag2(accountingperiod); Period3=lag3(accountingperiod); Period4=lag4(accountingperiod); Period5=lag5(accountingperiod); Period6=lag6(accountingperiod); Period7=lag7(accountingperiod); Period8=lag8(accountingperiod); Period9=lag9(accountingperiod); Period10=lag10(accountingperiod); Period11=lag11(accountingperiod); Period12=lag12(accountingperiod); Period13=lag13(accountingperiod); Period14=lag14(accountingperiod); Period15=lag15(accountingperiod); Period16=lag16(accountingperiod); Period17=lag17(accountingperiod); Period18=lag18(accountingperiod); Period19=lag19(accountingperiod); Period20=lag20(accountingperiod); Period21=lag21(accountingperiod); Period22=lag22(accountingperiod); Period23=lag23(accountingperiod); Period24=lag24(accountingperiod); /*Lag Variables created for 180,365 and 730 day flag*/ DPD_back24 = lag24(&var.); DPD_back23 = lag23(&var.); DPD_back22 = lag22(&var.); DPD_back21 = lag21(&var.); DPD_back20 = lag20(&var.); DPD_back19 = lag19(&var.); DPD_back18 = lag18(&var.); DPD_back17 = lag17(&var.); DPD_back16 = lag16(&var.); DPD_back15 = lag15(&var.); DPD_back14 = lag14(&var.); DPD_back13 = lag13(&var.); DPD_back12 = lag12(&var.); DPD_back11 = lag11(&var.); DPD_back10 = lag10(&var.); DPD_back9 = lag9(&var.); DPD_back8 = lag8(&var.); DPD_back7 = lag7(&var.); DPD_back6 = lag6(&var.); DPD_back5 = lag5(&var.); DPD_back4 = lag4(&var.); DPD_back3 = lag3(&var.); DPD_back2 = lag2(&var.); DPD_back1 = lag1(&var.); end; /*Creating the Flag count Variables after checking that they fall in the last continous 6,12 or 24 months*/ Need help here %do i = 1 %to 24; if Period&i. = . then DPD_&day180._flag&i. = 0; else if Period&i. >= AccountingPeriod_6 then DPD_&day180._flag&i. = DPD_back&i. ; else if DPD_&day180._flag&i. = 0; %end; /*Summary variables for 180,365 and 730 day flag*/ DPD_&day180.=sum(DPD_&day180._flag1,DPD_&day180._flag2,DPD_&day180._flag3,DPD_&day180._flag4,DPD_&day180._flag5,DPD_&day180._flag6); DPD_&day365.=sum(DPD_&day180._flag1,DPD_&day180._flag2,DPD_&day180._flag3,DPD_&day180._flag4,DPD_&day180._flag5,DPD_&day180._flag6, DPD_&day180._flag7,DPD_&day180._flag8,DPD_&day180._flag9,DPD_&day180._flag10,DPD_&day180._flag11,DPD_&day180._flag12); DPD_&day730.=sum(DPD_&day180._flag1,DPD_&day180._flag2,DPD_&day180._flag3,DPD_&day180._flag4,DPD_&day180._flag5,DPD_&day180._flag6, DPD_&day180._flag7,DPD_&day180._flag8,DPD_&day180._flag9,DPD_&day180._flag10,DPD_&day180._flag11,DPD_&day180._flag12, DPD_&day180._flag13,DPD_&day180._flag14,DPD_&day180._flag15,DPD_&day180._flag16,DPD_&day180._flag17,DPD_&day180._flag18, DPD_&day180._flag19,DPD_&day180._flag20,DPD_&day180._flag21,DPD_&day180._flag22,DPD_&day180._flag23,DPD_&day180._flag24); run; %mend lag_status; /*Calling the Macro 3 times once each for 30 DPD, 60 DPD and 90 DPD*/ %lag_status(var=DPD30_flag,day180=30_180,day365=30_365,day730=30_730); And here is the sample data CustomerID AccountingPeriod In_default DPD30_flag DPD60_flag DPD90_flag Accountingperiod_6 Accountingperiod_12 Accountingperiod_24 2180840 200912 No 0 0 0 200906 200812 200712 2180840 201001 No 0 0 0 200907 200901 200801 2180840 201002 No 0 0 0 200908 200902 200802 2180840 201003 No 0 0 0 200909 200903 200803 2180840 201004 No 0 0 0 200910 200904 200804 2180840 201005 No 0 0 0 200911 200905 200805 2180840 201006 No 0 0 0 200912 200906 200806 2180840 201007 No 0 0 0 201001 200907 200807 2180840 201008 No 0 0 0 201002 200908 200808 2180840 201009 No 0 0 0 201003 200909 200809 2180840 201010 No 0 0 0 201004 200910 200810 2180840 201011 No 0 0 0 201005 200911 200811 2180840 201012 No 0 0 0 201006 200912 200812 2180840 201101 No 0 0 0 201007 201001 200901 2180840 201102 No 0 0 0 201008 201002 200902 2180840 201103 No 0 0 0 201009 201003 200903 2180840 201104 No 0 0 0 201010 201004 200904 2180840 201105 No 0 0 0 201011 201005 200905 2180840 201106 No 0 0 0 201012 201006 200906 2180840 201107 No 0 0 0 201101 201007 200907 2180840 201108 No 0 0 0 201102 201008 200908 2180840 201109 No 0 0 0 201103 201009 200909 2180840 201110 No 0 0 0 201104 201010 200910 2180840 201111 No 0 0 0 201105 201011 200911 2180840 201112 No 0 0 0 201106 201012 200912 2180840 201201 Yes 1 0 0 201107 201101 201001 2180840 201202 Yes 0 0 0 201108 201102 201002 2180840 201203 No 0 0 0 201109 201103 201003 2180840 201204 No 0 0 0 201110 201104 201004 2180840 201205 No 0 0 0 201111 201105 201005 2180840 201206 Yes 0 0 0 201112 201106 201006 2180840 201207 No 0 0 0 201201 201107 201007 2180840 201208 No 0 0 0 201202 201108 201008 2180840 201209 No 0 0 0 201203 201109 201009 2180840 201210 No 0 0 0 201204 201110 201010 2180840 201211 No 1 0 0 201205 201111 201011 2180840 201212 No 1 1 0 201206 201112 201012 2180840 201301 Yes 1 1 1 201207 201201 201101 2180840 201302 Yes 1 1 1 201208 201202 201102 2180840 201303 Yes 1 1 0 201209 201203 201103 2180840 201304 Yes 1 1 1 201210 201204 201104 2180840 201305 Yes 1 1 0 201211 201205 201105 2180840 201306 Yes 1 1 1 201212 201206 201106 2180840 201307 Yes 1 1 1 201301 201207 201107 2180840 201308 Yes 1 1 1 201302 201208 201108 2180840 201309 Yes 1 1 1 201303 201209 201109 2180840 201310 Yes 0 0 0 201304 201210 201110 2180840 201311 Yes 0 0 0 201305 201211 201111 2180840 201312 Yes 0 0 0 201306 201212 201112 2180840 201401 No 0 0 0 201307 201301 201201 2180840 201402 No 0 0 0 201308 201302 201202 2180840 201403 No 0 0 0 201309 201303 201203 2180840 201404 Yes 0 0 0 201310 201304 201204 2180840 201405 Yes 1 1 1 201311 201305 201205 2180840 201406 Yes 1 1 1 201312 201306 201206 2180840 201407 Yes 0 0 0 201401 201307 201207 2180840 201408 Yes 0 0 0 201402 201308 201208 2259914 200801 No 0 0 0 200707 200701 200601 2259914 200802 No 0 0 0 200708 200702 200602 2259914 200803 No 0 0 0 200709 200703 200603 2259914 200804 No 0 0 0 200710 200704 200604 2259914 200805 No 0 0 0 200711 200705 200605 2259914 200806 No 0 0 0 200712 200706 200606 2259914 200807 No 0 0 0 200801 200707 200607 2259914 200808 No 0 0 0 200802 200708 200608 2259914 200809 No 0 0 0 200803 200709 200609 2259914 200810 No 0 0 0 200804 200710 200610 2259914 200811 No 0 0 0 200805 200711 200611 2259914 200812 No 0 0 0 200806 200712 200612 2259914 200901 No 0 0 0 200807 200801 200701 2259914 200902 No 1 0 0 200808 200802 200702 2259914 200903 No 0 0 0 200809 200803 200703 2259914 200904 No 1 0 0 200810 200804 200704 2259914 200905 No 0 0 0 200811 200805 200705 Can you help me? Thanks Gaurav
... View more