Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stata function equivalent to group_concat

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
like image 504
giordano Avatar asked Aug 10 '26 22:08

giordano


1 Answers

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
like image 96
Nick Cox Avatar answered Aug 13 '26 13:08

Nick Cox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!