Hi Haikuo, Thank you very much for this code which worked very well. I am trying to understand what this code is doing and could you please see if I have understood correctly, time permitting. This is what “ifn” function does based on literature I have reviewed. /*In the following code, we assign a value of 1 or 0 to flag variable*/ data test; set test; if sex= ‘Male’ then flag =1; else flag=0; run; /*Some programmers use the following code to do the same task above*/ data test; set test; flag=ifn(sex=’Male’,1,0); run; This is your code data want; do until (last.account_number); set have; by bank_number account_number current_date; flag=ifn(Arrears_Band not in ('NPNA','writoff') and lag(Arrears_Band)='NPNA',1,flag); if first.account_number then flag=0; end; do until (last.account_number); set have; by bank_number account_number current_date; if flag then output; end; run; • Do until starts to read all the records pertinent to first account found in our data set called “this”. So, these are the records. Bank_number Account_number Current_date Arrears_Band 10 111 30SEP2010 NPNA 10 111 31OCT2010 Current 10 111 30NOV2010 NPNA 10 111 30JUN2011 NPNA 10 111 01JAN2012 writoff when do until executes below statement, I think this is what happening. flag=ifn(Arrears_Band not in ('NPNA','writoff') and lag(Arrears_Band)='NPNA',1,flag); Above statement says: If Arrears_Band not in ('NPNA','writoff') and lag(Arrears_Band)='NPNA’ then a new variable called “flag” is created and its value is returned as 1. If this condition is not satisfied, then then the value for the variable “flag” is returned as "flag". (I am not too sure). Then comes to the next statement: if first.account_number then flag=0; Above statement returns zero for the first account_number. Now this should be the answer after processing the account_number that do until first hits. Bank_number Account_number Current_date Arrears_Band lag(Arrears_Band ) Flag 10 111 30SEP2010 NPNA 0 10 111 31OCT2010 Current NPNA 1 10 111 30NOV2010 NPNA Current flag 10 111 30JUN2011 NPNA NPNA flag 10 111 01JAN2012 writoff NPNA flag Then in the next do until pass, all the records whose flag = 1 are outputted. But then only the last 3 records of the above table should be outputted which is not our intended answer. But your code generated the intended answer. How come? Would appreciate if you could shed some light. Thank you for your time. Mirisage
... View more