Okay, here we go! It's quite a few steps, but each step is fairly simple. That's one of the big differences between Enterprise Guide and writing SAS code, it's easier to use many small steps. (I'm giving the first date in your first table the name "InvoiceDate", since you didn't name it). Step 1 ------ Create at query on your first table (with payments). The main thing you want to do here is to create a new computed column, let's call it BigDelay. In a CASE function, set it to 1 if PaymentDate - InvoiceDate > 60, else 0. Also, filter by BigDelay = 1. This should i) figure out which records have a big delay, and ii) only keep those records. Step 2 ------ Run a rank task on the result. Use InvoiceDate as your "column to rank", and IDClient as your "Rank by" column. This will give the resulting rank 1 to the minimum (earliest) value of InvoiceDate. Step 3 ------ Run a query on this output, selecting only the records where the rank equal 1. This should give you the earliest big delay for every client. Step 4 ------ Another query. Use the result of the above step, but add your second table (I'll call it Table2) to the query. Make sure the two tables are joined on IDClient. Add a filter of "where PurchaseDate < InvoiceDate". I can't tell, but you might also need an "AND PurchaseAmount NOT IS MISSING". This will give you all of the Table2 records with a date before the cutoff date from Table 1. Step 5 ------ Run a rank task on the result. Use PurchaseDate as your "column to rank", and IDClient as your "Rank by" column. This time, in the "Options" tab, turn on "Reverse ranking...", which will rank the LATEST PurchaseDate as number 1. Step 6 ------ And finally, another query on the result, again selecting the records with rank equal to 1. This should give you a table with the record for each IDClient that meets your criteria. I know it sounds like a lot of work, but once your familiar with these tasks it would only take about ten minutes. Let us know how it goes, Tom
... View more