Hi, I have two datasets. Dataset 1 is at the person level and has ID, a date variable, and a series of variables named for year/month combinations. A value of 0 for a year/month variable means that the person was absent in that year/month; a value of 1 means that they were present: ID Date1 Jan2010 Feb2010....etc through Mar2015 1 1/4/12 0 1 0 2 5/9/12 1 0 0 3 4/12/13 1 1 1 Dataset 2 is at the person/date level; that is, each row is one person on one date. Each row also has a number associated with it (number of events): ID Date2 Num_events 1 2/3/2014 5 1 10/12/2014 1 1 11/5/2014 2 2 1/1/2012 2 2 5/7/2013 3 I have joined the datasets on ID, and I need two different things: 1. I need every person's total num_events by month/year combo--however, this needs to be cross-referenced with the year/month variables, because if they have a value of 1 for that year/month but no events, they should have a value of 0 rather than missing. For example, ID 1 was present in Feb 2010 because they have Feb2010=1. In Dataset 2, they have no events in Feb 2010 (because they have no record for Feb 2010). Therefore, ID 1's total events for Feb 2010 should=0. However, they were absent in Jan 2010 (Jan2010 var=0), so their event total for Jan 2010 will be null, not 0. 2. Even more complicated--ultimately what I want is to have everyone's event total relative to their own Date1 (the date variable in Dataset 1). That is, the event total for ID 1 in Dec 2011 would be -1 month from their Date1 var, but for ID 2 it would be -5 months from their Date1 var. Basically, I can't figure out how to reconcile all of these elements. I was able to create a dataset of total events by relative month by calculating a new variable which counted the number of months between Date1 and Date2 (mth_diff), then doing proc sql; create table mthly_sums as select ID, mth_diff, sum(num_events) as tot from have group by ID, mth_diff; quit; But that doesn't incorporate the mth/year vars and the fact that people who were present but had no events should be 0 and not missing.
... View more