I would like to group concatenate a categorical variable. Example:
pat x
1 a
1 b
1 b
2 a
2 a
The group concatenating should result in:
pat y
1 a-b-b
2 a-a
In Mysql this would be done using group_concat:
SELECT pat, GROUP_CONCAT(x SEPARATOR '-') y FROM tb GROUP BY pat
Also it would be nice if the function could concatenate distinct ordered values. With above example the output should be:
pat y
1 a-b
2 a
With MySQL:
SELECT pat, GROUP_CONCAT(DISTINCT x ORDER BY x SEPARATOR '-') y FROM tb GROUP BY pat
Note that this would reduce the data set to fewer observations.
bysort pat y: keep if _n == 1
by pat: gen Y = y[1]
by pat: replace Y = Y[_n-1] + "-" + y if _n > 1
by pat: keep if _n == _N
Collected from the Internet
Please contact debug[email protected] to delete if infringement.
Comments