stata collapse
时间: 2023-10-26 21:08:01 浏览: 313
Stata collapse is a command used to aggregate data by collapsing (combining) multiple observations into a single observation for each group defined by one or more variables. This is useful for summarizing data and creating new variables based on the aggregated data. The collapse command can be used with several statistical functions such as mean, sum, min, max, etc.
Syntax:
collapse (statistic) varlist [if] [in] [, by(varlist) ]
Example:
collapse (mean) price weight mpg, by(foreign)
This command will collapse the dataset by the variable 'foreign' and calculate the mean of the variables 'price', 'weight', and 'mpg' for each group of foreign cars.
Another example:
collapse (sum) sales, by(region year)
This command will collapse the dataset by the variables 'region' and 'year' and calculate the sum of the variable 'sales' for each group of region-year combinations.
Note: The collapsed dataset will have one observation for each group defined by the 'by' variable(s).
阅读全文
相关推荐


















