Hello all,
I've come across a data analysis issue and don't know how best to address the following problem.
For simplicity, I have the following dataset:
I would like to identify the values for dates that are within 30 days of event_date. For example, in the table above, I would like code to generate an event_value column that would equal 1000 for id=1, 2000 for id=2, and 3000 for id=3.
I know I could generate a counter column that would represent which date value fits within a 30-day window (i.e.
.
However, in my real dataset, I have 50 date columns, and would prefer to automate a solution rather than run 50 replace commands. Secondly, I don't know an automated way to take take the results of the counter column and have it pull the specific value corresponding to the date (i.e., pull value2 if date2 is in desired range).
Any thoughts?
Thank you.
I've come across a data analysis issue and don't know how best to address the following problem.
For simplicity, I have the following dataset:
id | event_date | date1 | value1 | date2 | value2 | date3 | value3 | event_value |
1 | 1/1/2020 | 1/2/2020 | 1000 | 3/2/2020 | 2000 | 5/2/2020 | 3000 | |
2 | 2/1/2020 | 2/2/2019 | 1000 | 2/2/2020 | 2000 | 2/2/2021 | 3000 | |
3 | 3/1/2020 | 3/2/2018 | 1000 | 3/2/2019 | 2000 | 3/2/2020 | 3000 |
I would like to identify the values for dates that are within 30 days of event_date. For example, in the table above, I would like code to generate an event_value column that would equal 1000 for id=1, 2000 for id=2, and 3000 for id=3.
I know I could generate a counter column that would represent which date value fits within a 30-day window (i.e.
Code:
replace counter=1 if date1 <event_date+ 30 & date1 >event_date
However, in my real dataset, I have 50 date columns, and would prefer to automate a solution rather than run 50 replace commands. Secondly, I don't know an automated way to take take the results of the counter column and have it pull the specific value corresponding to the date (i.e., pull value2 if date2 is in desired range).
Any thoughts?
Thank you.
Comment