A couple of questions
In the first record of data set DB, it seems to me that 9 months have elapsed but you show this should have a value of 3M in variable FLAG. I don't understand this.
You don't make clear if there is a different way to handle things when REF_DATE is before DATE_EVENT or when REF_DATE is after DATE_EVENT.
In your rules describing separation of months into 3M, 6M, 9M, etc., a literal reading of these rules seems like 3 months would get a FLAG value of BOTH 3M and 6M.
Ignoring these issues, and guessing at the answers about how to handle things, this is easily handled by the INTCK function and a custom format. Naturally if I have guessed wrong about how you want to handle things, you definitely ought to try fixing this code by yourself.
proc format;
value monthf 0-3='3M' 4-6='6M' 7-9='9M' 10-12='12M' other='.';
run;
data db1;
set db;
delta_months=intck('month',ref_date,date_event);
format delta_months monthf.;
run;
Why use custom formats here? Because if you use custom formats, the 3M 6M 9M 12M sort properly. If you don't use custom formats, 12M sorts before 3M.
Also, thank you for providing rules rather than (or in addition to) examples, and thank you for providing example data in the desired format.
... View more