Hi all,
I am trying to implement Mata code that creates an index within subgroups formed by the columns of a matrix. I have a matrix of the following form:
The code should add a third column with the within-group index:
In Stata, I would run the following code to create the index in a new variable.
I hope I made myself clear. Sorry if the topic came up somewhere else before.
I am trying to implement Mata code that creates an index within subgroups formed by the columns of a matrix. I have a matrix of the following form:
Code:
1 2
+---------+
1 | 2 3 |
2 | 2 3 |
3 | 2 4 |
4 | 2 4 |
5 | 3 5 |
6 | 3 6 |
+---------+
Code:
1 2 3
+-------------+
1 | 2 3 1 |
2 | 2 3 2 |
3 | 2 4 1 |
4 | 2 4 2 |
5 | 3 5 1 |
6 | 3 6 1 |
+-------------+
Code:
sort g1 g2 by g1 g2: gen id = _n

Comment