2026 Stata Conference 1–2 October Boston, MA
| Title | Saving stats (means, standard deviations, etc.) into a dataset or matrices | |
| Author | Ronna Cong, StataCorp |
collapse converts the data in memory into a dataset of means, medians, etc.
Example using collapse:
. use auto, clear
(1978 Automobile Data)
. by foreign: sum price mpg weight [aweight=rep78]
_______________________________________________________________________________
-> foreign = Domestic
Variable | Obs Weight Mean Std. Dev. Min Max
-------------+-----------------------------------------------------------------
price | 48 145.0000 6162.517 3106.007 3291 15906
mpg | 48 145.0000 19.8 5.205471 12 34
weight | 48 145.0000 3347.862 740.8696 1800 4840
_______________________________________________________________________________
-> foreign = Foreign
Variable | Obs Weight Mean Std. Dev. Min Max
-------------+-----------------------------------------------------------------
price | 21 90.0000 6133.778 2286.096 3748 11995
mpg | 21 90.0000 25.45556 6.719655 17 41
weight | 21 90.0000 2285.778 371.6942 1760 3170
. collapse (mean) price_mean = price (median) mpg_med = mpg (sd) weight_sd =
weight [aweight=rep], by(foreign)
. list
+------------------------------------------+
| foreign price_~n mpg_med weight~d |
|------------------------------------------|
1. | Domestic 6,162.5 19 740.87 |
2. | Foreign 6,133.8 25 371.694 |
+------------------------------------------+
matrix accum, with the means() and deviations options, can be used to obtain means matrices and covariance matrices.
Example using matrix accum:
. use auto, clear
(1978 Automobile Data)
. correlate price mpg weight [aweight=rep78], means cov
(sum of wgt is 2.3500e+02)
(obs=69)
Variable | Mean Std. Dev. Min Max
-------------+----------------------------------------------------
price | 6,151.51 2,801.56 3,291 15,906
mpg | 21.96596 6.402573 12 41
weight | 2,941.11 811.2383 1,760 4,840
| price mpg weight
-------------+---------------------------
price | 7.8e+06
mpg | -8225.94 40.9929
weight | 1.2e+06 -4123.77 658108
. mat accum Cov = price mpg weight [aweight=rep78], noc means(M) deviations
(sum of wgt is 2.3500e+02)
(obs=69)
. mat list M
M[1,3]
price mpg weight
_cons 6151.5106 21.965957 2941.1064
. mat Cov = Cov/(r(N)-1)
. mat list Cov
symmetric Cov[3,3]
price mpg weight
price 7848734.6
mpg -8225.9352 40.992942
weight 1222727.7 -4123.7697 658107.63
Learn
Free webinars
NetCourses
Web training
Organizational training
Video tutorials
Third-party courses
Web resources
Teaching with Stata
© Copyright 1996–2026 StataCorp LLC. All rights reserved.
×
We use cookies to ensure that we give you the best experience on our website—to enhance site navigation, to analyze usage, and to assist in our marketing efforts. By continuing to use our site, you consent to the storing of cookies on your device and agree to delivery of content, including web fonts and JavaScript, from third party web services.
Cookie Settings
Last updated: 16 November 2022
StataCorp LLC (StataCorp) strives to provide our users with exceptional products and services. To do so, we must collect personal information from you. This information is necessary to conduct business with our existing and potential customers. We collect and use this information only where we may legally do so. This policy explains what personal information we collect, how we use it, and what rights you have to that information.
These cookies are essential for our website to function and do not store any personally identifiable information. These cookies cannot be disabled.
This website uses cookies to provide you with a better user experience. A cookie is a small piece of data our website stores on a site visitor's hard drive and accesses each time you visit so we can improve your access to our site, better understand how you use our site, and serve you content that may be of interest to you. For instance, we store a cookie when you log in to our shopping cart so that we can maintain your shopping cart should you not complete checkout. These cookies do not directly store your personal information, but they do support the ability to uniquely identify your internet browser and device.
Please note: Clearing your browser cookies at any time will undo preferences saved here. The option selected here will apply only to the device you are currently using.