Stata function equivalent to group_concat

giordano

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
Nick Cox

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

pandas equivalent of Stata's encode

From Dev

How do I write a group_concat function in sqlalchemy?

From Dev

Arrays / Vector equivalent in Stata

From Dev

MySQL's Group_Concat function miss the nulls. How can group the rows including NULLs.

From Dev

Invalid use of group function (group_concat and MySQL)

From Dev

Group_concat use?

From Dev

GROUP_CONCAT with limit

From Dev

is there an equivalent to Stata's egen function?

From Dev

Aggregate function GROUP_CONCAT(expr) in MySQL

From Dev

Group concat equivalent in queryDSL

From Dev

Equivalent function to STUFF in SQL (GROUP_CONCAT in MySSQL / LISTAGG in Oracle)

From Dev

Standardize a variable by group in Stata

From Dev

example GROUP_CONCAT

From Dev

R equivalent of Stata *

From Dev

Optimize MySQL query for group_concat function

From Dev

What is the equivalent of XML PATH and Stuff in Linq lambda expression (GROUP_CONCAT/STRING_AGG)?

From Dev

Spark SQL replacement for MySQL's GROUP_CONCAT aggregate function

From Dev

Sorting in group_concat

From Dev

GROUP_CONCAT numbering

From Dev

R: Is there an equivalent of Stata's ibn. function?

From Dev

group concat equivalent in pig?

From Dev

GROUP_CONCAT in SQLite

From Dev

Group_concat use?

From Dev

Aggregate function GROUP_CONCAT(expr) in MySQL

From Dev

Group concat equivalent in queryDSL

From Dev

How do I use JPA 2.1's CriteriaBuilder.function with MySQL's "GROUP_CONCAT"?

From Dev

example GROUP_CONCAT

From Dev

MySQL: update with join using GROUP_CONCAT: ERROR 1111 (HY000): Invalid use of group function

From Dev

Function not found: group_concat