sql - Finding the earliest date that a specific event occurred -


i trying find out whether user received coupon within 30 days of earliest date entered program. each user can potentially enter many programs, , trying mark earliest date program. example data:

userid  start_date  programid coupon_date   1     2003-02-05   83435    2003-03-01   1     2004-05-04   83435    2005-03-05   1     2002-01-30   82222    2001-02-24   2     2001-02-02   82222    2001-04-22   2     2000-04-03   22222    2004-12-13   2     1999-02-05   83435    1999-05-02   2     2005-05-28   83435    2008-02-02   3     1998-01-01   24853    1999-02-02   3     2006-03-02   44533    2006-05-02 

my desired output (for finding whether received coupon within 30 days of entering program 83435):

userid  start_date  programid coupon_date  match   1     2003-02-05   83435    2003-03-01     1   1     2004-05-04   83435    2005-03-05     0   1     2002-01-30   82222    2001-02-24     0   2     2001-02-02   82222    2001-04-22     0   2     2000-04-03   22222    2004-12-13     0   2     1999-02-05   83435    1999-05-02     0   2     2005-05-28   83435    2008-02-02     0   3     1998-01-01   24853    1999-02-02     0   3     2006-03-02   44533    2006-05-02     0 

my code far is:

proc sql; create table programmatch select users.*, case when (min((start_date) +30) >= coupon_date) 1 else 0 end match   users order userid; quit; 

this code marks whether received coupon within 30 days, unsure how make mark particular programid well.

is there way in proc sql?

sorry if confusing.

thanks,

can please try below query? not sure whether group works case.

select userid, programid, case when (min((start_date) +30) >= coupon_date) 1 else 0 end match users group userid, programid order userid 

please let me know.


Comments

Popular posts from this blog

c++ - OpenMP unpredictable overhead -

ruby on rails - RuntimeError: Circular dependency detected while autoloading constant - ActiveAdmin.register Role -

javascript - Wordpress slider, not displayed 100% width -