Announcement

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

  • Easier way to get casewise statistics using tabstat?

    Hi,

    I need a command/code that help me to get casewise descriptive statistics using "tabstat" without needing to match variables with the same number of observations manually.

    Example:
    I have the following variables and number of observations in my data set:
    Variable Obs
    Weight-a 100
    Weight-b 90
    BMI-a 120
    BMI-b 110
    Hight-a 70
    Hight-b 72
    Right now, I type "tabstat Weight-a Weight-b, casewise stat(mean sd min max)" for the Weight variables and a similar one for the BMI variables etc.

    What I actually need is to compare Weight-a to Weight-b (90 pairwise obs), BMI-a to BMI-b (110 pairwise obs), and Hight-a to Hight-b (70 pairwise obs), respectively, using one code with the "tabstat" command.

    I heard about "loop" and "return list" commands for instance but I don't know how to use them.


  • #2
    To clarify what you want: It sounds like you want a listing in which variables are the rows, and rows are sorted so that variables with the same number of non-missing observations appear consecutively.
    If you want to learn about the return list, -help return list- will give some information.

    Comment


    • #3
      Hi Amanda,

      1) It's not quite clear to me what you want, but here is some sample data and commands to help you get going
      2) Do you want the avg change between weight_a and weight_b, the percent change, or the correlation?
      3) It would be *really* helpful if you could share a sample of your data using dataex. If you need help with dataex, I have a video tutorial here

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte id int(weight_a weight_b) float(BMI_a BMI_b) byte(height_a height_b)
       1 155 150 29.28 25.74 61 64
       2 174 195 29.86 30.54 64 67
       3 190 200 31.61 30.41 65 68
       4 158 170 24.74 24.39 67 70
       5 183 177 27.82 26.91 68 68
       6 184 194 27.17 28.65 69 69
       7 191 184  27.4     . 70  .
       8 205 199 27.04     . 73  .
       9 210 203 26.25 25.37 75 75
      10 235   . 27.86     . 77 77
      end
      ------------------ copy up to and including the previous line ------------------

      Code:
      gen weight_change = weight_b - weight_a
      gen weight_pct = (weight_b - weight_a ) / weight_a
      gen BMI_change = BMI_b - BMI_a
      
      
      . list id weight_a weight_b weight_ch weight_p BMI_a BMI_b BMI_change height_a height_b , noobs N( weight_a wei
      > ght_b weight_ch weight_p BMI_a BMI_b BMI_change height_a height_b ) mean( weight_a weight_b weight_ch weight_
      > p BMI_a BMI_b BMI_change height_a height_b )
      
            +----------------------------------------------------------------------------------------------------+
            | id   weight_a   weight_b   weight~h   weight_~t    BMI_a   BMI_b   BMI_cha~e   height_a   height_b |
            |----------------------------------------------------------------------------------------------------|
            |  1        155        150         -5   -.0322581    29.28   25.74   -3.540001         61         64 |
            |  2        174        195         21    .1206897    29.86   30.54    .6800003         64         67 |
            |  3        190        200         10    .0526316    31.61   30.41   -1.200001         65         68 |
            |  4        158        170         12    .0759494    24.74   24.39   -.3500004         67         70 |
            |  5        183        177         -6   -.0327869    27.82   26.91   -.9099998         68         68 |
            |----------------------------------------------------------------------------------------------------|
            |  6        184        194         10    .0543478    27.17   28.65        1.48         69         69 |
            |  7        191        184         -7   -.0366492     27.4       .           .         70          . |
            |  8        205        199         -6   -.0292683    27.04       .           .         73          . |
            |  9        210        203         -7   -.0333333    26.25   25.37   -.8799992         75         75 |
            | 10        235          .          .           .    27.86       .           .         77         77 |
            |----------------------------------------------------------------------------------------------------|
       Mean |         188.5    185.778   2.444444    .0154803   27.903   27.43   -.6742859       68.9      69.75 |
          N |            10          9          9           9       10       7           7         10          8 |
            +----------------------------------------------------------------------------------------------------+
      
      
      . tabstat weight_a weight_b weight_ch weight_pct BMI_a BMI_b BMI_change height_a height_b , stats(n mean median
      >  min max) col(stat)
      
          variable |         N      mean       p50       min       max
      -------------+--------------------------------------------------
          weight_a |        10     188.5       187       155       235
          weight_b |         9  185.7778       194       150       203
         weight_ch |         9  2.444444        -5        -7        21
        weight_pct |         9  .0154803 -.0292683 -.0366492  .1206897
             BMI_a |        10    27.903     27.61     24.74     31.61
             BMI_b |         7     27.43     26.91     24.39     30.54
        BMI_change |         7 -.6742859 -.8799992 -3.540001      1.48
          height_a |        10      68.9      68.5        61        77
          height_b |         8     69.75      68.5        64        77
      ----------------------------------------------------------------
      
      . summ weight_a weight_b weight_ch weight_pct BMI_a BMI_b BMI_change height_a height_b
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
          weight_a |         10       188.5    24.07973        155        235
          weight_b |          9    185.7778    17.44834        150        203
         weight_ch |          9    2.444444    10.76001         -7         21
        weight_pct |          9    .0154803    .0605473  -.0366492   .1206897
             BMI_a |         10      27.903    1.936452      24.74      31.61
      -------------+---------------------------------------------------------
             BMI_b |          7       27.43    2.471592      24.39      30.54
        BMI_change |          7   -.6742859    1.589904  -3.540001       1.48
          height_a |         10        68.9    5.021067         61         77
          height_b |          8       69.75     4.26782         64         77
      Last edited by David Benson; 20 Mar 2019, 19:09.

      Comment


      • #4
        Thank you for your answer David even if it is not what I really asked about. I think Mike's question is closer to what I really meant. I will post the question again with the same title and hopefully with better explanation.

        Comment

        Working...
        X