Hi Ksharp,
Thank you very much for all the help. However, these days I am still struggling with how to code the unique id part. See if I can explain more clearly.
The general logic is "The id is first time appears within -30 days and -7 days from the same account. If so, the flag is 1, else the flag is 0". I want to produce the last column flag.
obs | account | date | test | id | flag | |
1 | 384000 | 6/30/2010 | 1 | 13.15 | 1 | |
2 | 384000 | 6/15/2010 | 1 | 13.14 | 1 | |
3 | 384000 | 6/10/2010 | 1 | 13.14 | 1 | |
4 | 384000 | 6/1/2010 | 1 | 13.13 | 0 | |
5 | 386000 | 5/30/2010 | 1 | 12.12 | 0 | |
6 | 386000 | 5/15/2010 | 1 | 12.12 | 1 | |
7 | 386000 | 4/25/2010 | 1 | 12.13 | 0 |
For example, for obs 1, the id is 13.15, between 30 and 7 days, there is no same id is 13.15. So the flag is 1.
however, for obs 2, the id is 13.14, even though there is another 13.14 appears in obs 3, but the flag is still 1, since between 06/15 and 06/10, there is only 5 days difference, not more than 7 days difference. We will ignore the first 7 days observation. Therefore, this obs 3 will not count. And again, after we ignore the first 7 days, between -7 and -30 days, there is no same 13.14 id, so the flag will be 1.
Obs 4 will be 0, since this is no history to compare.
Obs 5 will be 0, since there is another id 12.12 in obs 6, and the date is between 7 days and 30 days difference.
I am struggling how to code to ignore the first 7 days, and only count between 7 days and 30 days range.
I really appreciate if you could help me how to resolve this puzzle.
Thank you very much!
OK.Cartesian Product of DataStep is more powerful than Cartesian Product of Sql. So I used it for your a little complicated problem.
data temp; input account $ date : mmddyy10. test id; format date mmddyy10.; cards; 384000 6/30/2010 1 13.15 384000 6/15/2010 1 13.14 384000 6/10/2010 1 13.14 384000 6/1/2010 1 13.13 386000 5/30/2010 1 12.12 386000 5/15/2010 1 12.12 386000 4/25/2010 1 12.13 ; run; data want; set temp; found=0; matched=0;flag=0; do i=1 to _nobs; set temp(drop=test rename=(account=_account date=_date id=_id)) nobs=_nobs point=i; if account=_account and _date ge date-30 and _date le date-7 and id=_id then found=1; if account=_account and _date ge date-30 and _date le date-7 then matched=1; if i=_nobs then do; if not found and matched then flag=1; output; end; end; drop found matched _:; run;
Ksharp
Hi Ksharp,
Sincerely appreciate all your help. This is really something. Definitely learned a lot from you.
Thank you very much!!!
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.
Ready to level-up your skills? Choose your own adventure.