I have a csv file with a field called activity that is stored as long in stata. I want to take this field and split it into 3 new variables (activity1, activity2, activity3)each with length=3 so the data will look something like this.
Here's the code I'm using:
gen activity1= substr(activity,-9,3);
gen activity2= substr(activity,-6,3);
gen activity3= substr(activity,-3,3);
My results end up looking like this
because the leading zeros in the activity field are dropped when the file is brought into stata.
How do I preserve the leading zeros? I know I need to do this prior to generate the 3 new variables but I'm not sure how.
activity | activity1 | activity2 | activity3 |
001003060 | 001 | 003 | 060 |
100400345 | 100 | 400 | 345 |
345234100 | 345 | 234 | 100 |
gen activity1= substr(activity,-9,3);
gen activity2= substr(activity,-6,3);
gen activity3= substr(activity,-3,3);
My results end up looking like this
activity | activity1 | activity2 | activity3 |
1003060 | 003 | 060 | |
100400345 | 100 | 400 | 345 |
345234100 | 345 | 234 | 100 |
How do I preserve the leading zeros? I know I need to do this prior to generate the 3 new variables but I'm not sure how.
Comment