Hi Statalists,
I apologize if my question seems basic, but I have tried several approaches without success. I am trying to repeat a set of observations in a specific order and group them accordingly. Below is a simplified version of the data I am working with:
Ideally, I would like to repeat this group of observations in the same order 3000 times and assign a label to each group. The desired output should look like this:
I used the expand function to repeat the observations and created a grouping variable like this:
However, I am encountering an issue where the data is not ordered as I want it to be, and each group ends up containing the same observations instead of following the specific order.
I would really appreciate if anyone in the community has any advice or suggestions on how to achieve this.
Thanks a ton!
K
I apologize if my question seems basic, but I have tried several approaches without success. I am trying to repeat a set of observations in a specific order and group them accordingly. Below is a simplified version of the data I am working with:
Code:
*ssc install dataex input str10 Type str10 Subtype int Year int id "A" "ABC" 2016 1 "A" "ABC" 2017 2 "B" "LOL" 2019 3 "B" "MOM" 2020 4 "C" "EDI" 2013 5 "C" "KII" 2015 6
Type | Subtype | Year | id | Group |
A | ABC | 2016 | 1 | 1 |
A | ABC | 2017 | 2 | 1 |
B | LOL | 2019 | 3 | 1 |
B | MOM | 2020 | 4 | 1 |
C | EDI | 2013 | 5 | 1 |
C | KII | 2015 | 6 | 1 |
A | ABC | 2016 | 1 | 2 |
A | ABC | 2017 | 2 | 2 |
B | LOL | 2019 | 3 | 2 |
B | MOM | 2020 | 4 | 2 |
C | EDI | 2013 | 5 | 2 |
C | KII | 2015 | 6 | 2 |
... | ... | ... | ... | ... |
A | ABC | 2016 | 1 | 3000 |
A | ABC | 2017 | 2 | 3000 |
B | LOL | 2019 | 3 | 3000 |
B | MOM | 2020 | 4 | 3000 |
C | EDI | 2013 | 5 | 3000 |
C | KII | 2015 | 6 | 3000 |
Code:
input str10 Type str10 Subtype int Year int id "A" "ABC" 2016 1 "A" "ABC" 2017 2 "B" "LOL" 2019 3 "B" "MOM" 2020 4 "C" "EDI" 2013 5 "C" "KII" 2015 6 gen repeat = 5 expand repeat gen group = ceil(_n /6)
I would really appreciate if anyone in the community has any advice or suggestions on how to achieve this.
Thanks a ton!
K
Comment