Hello! I am new to the forum, and greatly appreciate your help. This is what my physical activity data looks like for 2 IDs: (the xx are actual numbers in my data)
Each ID attended 6 visits and a screening visit, and each different physical activity has it's own row. The variables ID, visit and instance uniquely identify observations.
My issue is with the 'Changes_since_last_visit' column. Essentially, at their screening visit we would ask them what activity they do, then at the next visit we would ask them if they still do that activity. What I would like to do seems simple, but it's not working. If there were no changes in physical activity since their last visit, I want to copy the rows of their last visit, replace the 'visit' column with the correct visit number, and delete the original visit column. for ID=1; the screening data should be copied to visit=1, 2, 3, 4, and 5. For ID=3; the screening data should be expanded to visit 1 and the visit 3 data should be expanded to visit 4, 5, and 6.
Here is the code that is not working:
(then I would repeat this code for all other visits)
However, when I run this, nothing happens at all and I don't get any errors. Maybe I'm too tired and I've been looking at this problem for too long, but any help is greatly appreciated!
ID | visit | instance | Changes_since_last_visit | activity | time_per_month | mins | Mets |
1 | 1 | 1 | No | ||||
1 | 2 | 1 | No | ||||
1 | 3 | 1 | No | ||||
1 | 4 | 1 | No | ||||
1 | 5 | 1 | No | ||||
1 | 6 | 1 | Yes | running with dog | |||
1 | 6 | 2 | Yes | walking to school | |||
1 | 6 | 3 | Yes | rowing machine | |||
1 | 6 | 4 | Yes | spin class | |||
1 | screening | 1 | NA | running with dog | xx | xx | 3.4 |
1 | screening | 2 | NA | walking to school | xx | xx | 8 |
1 | screening | 3 | NA | rowing machine | xx | xx | 5 |
1 | screening | 4 | NA | spin class | xx | xx | 7 |
3 | screening | 1 | NA | walking to school | xx | xx | 2 |
3 | screening | 2 | NA | speed walking | xx | xx | 3.4 |
3 | screening | 3 | NA | Housekeeping | xx | xx | 2.9 |
3 | 1 | 1 | No | ||||
3 | 2 | 1 | Yes | walking to school | xx | xx | 2 |
3 | 2 | 2 | Yes | speed walking | xx | xx | 3.4 |
3 | 2 | 3 | Yes | Housekeeping | |||
3 | 3 | 1 | Yes | walking to school | xx | xx | 2 |
3 | 3 | 2 | Yes | Housekeeping | xx | xx | 2.9 |
3 | 3 | 3 | Yes | speed walking | |||
3 | 4 | 1 | No | ||||
3 | 5 | 1 | No | ||||
3 | 6 | 1 | No |
My issue is with the 'Changes_since_last_visit' column. Essentially, at their screening visit we would ask them what activity they do, then at the next visit we would ask them if they still do that activity. What I would like to do seems simple, but it's not working. If there were no changes in physical activity since their last visit, I want to copy the rows of their last visit, replace the 'visit' column with the correct visit number, and delete the original visit column. for ID=1; the screening data should be copied to visit=1, 2, 3, 4, and 5. For ID=3; the screening data should be expanded to visit 1 and the visit 3 data should be expanded to visit 4, 5, and 6.
Here is the code that is not working:
Code:
if visit==1 & Changes_since_last_visit=="No" { expand 2 if visit=="screening", generate (original_copy) sort BlindedID visit instance } replace visit = 1 if original_copy==1 drop if visit ==1 & original_copy==2 & Changes_since_last_visit=="No"
However, when I run this, nothing happens at all and I don't get any errors. Maybe I'm too tired and I've been looking at this problem for too long, but any help is greatly appreciated!
Comment