giordano
giordano

Reputation: 1

Grouping with Stata with two or more variables as in SQL GROUP BY

In SQL it is possible to group with several variables:

SELECT a, b, COUNT(*)
FROM t
GROUP BY a, b

What we get is a table with the levels of b nested in the level of a.

How can this performed with Stata a) output in windows? b) stored as file?

Upvotes: 0

Views: 4176

Answers (1)

user872324
user872324

Reputation:

If you have only two variables, and you don't need two write the ouptut to a file, you can do:

tabulate a b

If you want to write the output to a file or if you have more than two grouping variables, you can do as follows:

contract a b

Print the data on the screen:

sort a
list, sepby(a)

Save the data to a file in Stata format (.dta) ...

save results

... or to a tab-delimited ascii file

outsheet using results.csv

If you want to contract your data, list the result on the creen, and the return to the original data, you can use preserve and restore. The former "freezes" the data at a given point, and the latter allows to come back to that point.

preserve
contract a b
list
restore

Upvotes: 1

Related Questions