Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Table code to summarize

    Hello,

    could someone please help me how to combine similar commands into one table with the table command.

    e.g.
    I am looking at the development of the correlation coefficient for several years:
    pwcorr at_frau w08_1 if jahr==1993, obs
    pwcorr at_frau w08_1 if jahr==1994, obs
    pwcorr at_frau w08_1 if jahr==1995, obs
    pwcorr at_frau w08_1 if jahr==1996, obs

    How can these results be combined into one table with the table command?

    Thank you for your support!

  • #2
    If you have Stata 17 or newer, you can use collect to build a table of
    correlations.

    Instead of calling pwcorr manually for each level of a given variable,
    you can use the by prefix to do the computation and collection in one
    command.

    Here is a simple example using the publicly available auto data.
    Code:
    sysuse auto
    
    * sort data on variable that identifies your sample groups
    sort rep78
    * for each sample group compute a correlation and collect the sample
    * size and correlation
    by rep78 : collect r(N) r(rho) : pwcorr mpg turn, obs
    
    * verify the results of interested are in the autolevels
    collect query autolevels result
    * show the variable label in the table header
    collect style header rep78, title(label)
    * use a shorter label for the sample size result
    collect label levels result N "N", modify
    * use a fixed numeric format for the correlations
    collect style cell result[rho], nformat(%6.3f)
    * arrange the table
    collect layout (rep78) (result)
    Here is the resulting table.
    Code:
    ------------------------------
                       |  N      ρ
    -------------------+----------
    Repair record 1978 |
      1                |  2 -1.000
      2                |  8 -0.765
      3                | 30 -0.770
      4                | 18 -0.935
      5                | 11 -0.516
      .                |  5 -0.452
    ------------------------------

    Comment

    Working...
    X