# st: subscripting in groups

 From "Katsuhide Isa" <[email protected]> To <[email protected]> Subject st: subscripting in groups Date Wed, 22 Dec 2004 09:00:27 +0900

```Dear Statalisters,

I'm using Stata 8.
I have a data set describing intra-organizational mobility
patterns which contains variables such as code, year, dept,
etc., where 'dept' is a categorical variable which denotes
department code one belongs to.
(See the list at the end of this e-mail for detailed data structure)

To create a new variable which records the number of department
changes, I executed the following:

gen change = 0
sort code year
by code: gen dept_1 = dept[_n-1]
by code: replace change = 1 if dept - dept_1 ~= 0 & /*
*/dept ~= . & dept_1 ~= .
by code: egen num = sum(change)

It seems to work well, except that missings seem to produce
some odd results.

Further, I'd like to create another new variable named 'variety'
which records variety of departments that one experienced during
his/her career.
What makes things complicated is that 'dept' takes on more than
100 different values, so I can't come up with suitable codes for it..

The desired result would be as follows:

+----------------------------------------------------------------+
|   code    year     dept   depart_1   change     num   variety  |
|----------------------------------------------------------------|
1. | 100001      63       80          .        0       1        2   |
2. | 100001      68       70         80        1       1        2   |
3. | 100002      63        .          .        0       2        2   |
4. | 100002      68       60          .        0       2        2   |
5. | 100002      73       60         60        0       2        2   |
|----------------------------------------------------------------|
6. | 100002      78      160         60        1       2        2   |
7. | 100002      83        2        160        1       2        2   |
8. | 100003      63       40          .        0       1        3   |
9. | 100003      68        .         40        0       1        3   |
10. | 100003      73       60          .        0       1        3   |
|----------------------------------------------------------------|
11. | 100003      78        0         60        1       1        3   |
12. | 100004      63       10          .        0       1        2   |
13. | 100004      68       20         10        1       1        2   |
14. | 100004      73        .         20        0       1        2   |
15. | 100004      78      110          .        0       1        2   |
|----------------------------------------------------------------|
16. | 100004      83        .        110        0       1        2   |
17. | 100004      83        .          .        0       1        2   |
18. | 100005      63       60          .        0       2        3   |
19. | 100005      68      110         60        1       2        3   |
20. | 100005      73     7214        110        1       2        3   |
|----------------------------------------------------------------|
21. | 100005      78     7214       7214        0       2        3   |
22. | 100005      83     7214       7214        0       2        3   |
.....

Is it possible to create variables like these by (possibly)
using (or modifying) the codes above ?

Any suggestions welcome.