mercredi 29 juillet 2020

Determine differences between date stamps and reference dates

I’m working with a dataset of 191K obs that has individual records with date stamps throughout the period of 2014-2020. I have four reference dates (19 Sept 2014, 9 Sept 2016, 26 Oct 2017, 19 June 2019) that I need to determine the difference between each record’s date stamp. The rub is that I only need positive values for ‘difftime’: if the difference is <180 then the difference will be used; if the difference between dates is 180-365 days, then the value will be set to 180; if the difference >365 days, then NA; no negative values will be included.

Sample data

Date Difftime Notes: 11 Nov 2014 53 19 Sept 2014 used as reference 10 Jun 2015 180 19 Sept 2014 reference, but >180 5 Jan 2018 71 26 Oct 2017 reference 1 May 2019 NA No reference date within 365 days

In summary, a record’s date stamp needs to be compared to a relevant reference date (i.e., the closet, post-record date). I think I can do this in a number of individual ‘ifelse’ statements after creating separate variables for each of the reference dates, but I don’t want to clutter my dataset with more variables (even if I can remove them post-assessment). I’d appreciate any insights into how to code for this assessment. Thanks. Doug

Aucun commentaire:

Enregistrer un commentaire