Sunday, November 9, 2025

Customizable tables in Stata 17, half 4: Desk of statistical checks


In my final submit, I confirmed you the way to use the brand new and improved desk command with the statistic() choice to create a traditional desk 1. On this submit, I wish to present you the way to use the command() choice to create a desk of statistical checks. Our objective is to create the desk within the Microsoft Phrase doc under.

Create the essential desk

Let’s start by typing webuse nhanes2l to open the NHANES dataset, and let’s kind describe to look at among the variables.

. webuse nhanes2l
(Second Nationwide Well being and Diet Examination Survey)

. describe highbp age hgb hct iron albumin vitaminc zinc copper lead     
>          top weight bmi bpsystol bpdiast tcresult tgresult hdresult

Variable      Storage   Show    Worth
    identify         kind    format    label      Variable label
-------------------------------------------------------------------------------
highbp          byte    %8.0g               * Hypertension
age             byte    %9.0g                 Age (years)
hgb             float   %9.0g                 Hemoglobin (g/dL)
hct             float   %9.0g                 Hematocrit (%)
iron            int     %9.0g                 Serum iron (mcg/dL)
albumin         float   %9.0g                 Serum albumin (g/dL)
vitaminc        float   %9.0g                 Serum vitamin C (mg/dL)
zinc            int     %9.0g                 Serum zinc (mcg/dL)
copper          int     %9.0g                 Serum copper (mcg/dL)
lead            byte    %9.0g                 Lead (mcg/dL)
top          float   %9.0g                 Peak (cm)
weight          float   %9.0g                 Weight (kg)
bmi             float   %9.0g                 Physique mass index (BMI)
bpsystol        int     %9.0g                 Systolic blood stress
bpdiast         int     %9.0g                 Diastolic blood stress
tcresult        int     %9.0g                 Serum ldl cholesterol (mg/dL)
tgresult        int     %9.0g                 Serum triglycerides (mg/dL)
hdresult        int     %9.0g                 Excessive density lipids (mg/dL)

The dataset contains an indicator for hypertension (highbp), age, and lots of lab measurements. We want to take a look at the null speculation that the typical age and lab measurments are the identical within the teams with and with out, hypertension. We are able to do that with Stata’s ttest command. Let’s use ttest to check the null speculation that the typical age is identical within the two teams.

. ttest age, by(highbp)

Two-sample t take a look at with equal variances
------------------------------------------------------------------------------
   Group |     Obs        Imply    Std. err.   Std. dev.   [95% conf. interval]
---------+--------------------------------------------------------------------
       0 |   5,975    42.16502    .2169725    16.77157    41.73968    42.59037
       1 |   4,376    54.97281    .2253767    14.90897    54.53095    55.41466
---------+--------------------------------------------------------------------
Mixed |  10,351    47.57965    .1692044    17.21483    47.24798    47.91133
---------+--------------------------------------------------------------------
    diff |           -12.80779    .3185604               -13.43223   -12.18335
------------------------------------------------------------------------------
    diff = imply(0) - imply(1)                                      t = -40.2052
H0: diff = 0                                     Levels of freedom =    10349

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.0000         Pr(|T| > |t|) = 0.0000          Pr(T > t) = 1.0000

The output shows many statistics, together with the two-sided p-value for the t take a look at. A few of these statistics are briefly left in reminiscence and we are able to view them by typing return listing.

. return listing

scalars:
              r(stage) =  95
                 r(sd) =  17.21482923023818
               r(sd_2) =  14.9089715191102
               r(sd_1) =  16.77156676799842
                 r(se) =  .3185603831285
                r(p_u) =  1
                r(p_l) =  0
                  r(p) =  0
                  r(t) =  -40.20520433030012
               r(df_t) =  10349
               r(mu_2) =  54.97280621572212
                r(N_2) =  4376
               r(mu_1) =  42.16502092050209
                r(N_1) =  5975

We are able to use the command() possibility in desk to run a command, reminiscent of ttest, and put the ends in a desk. The instance under exhibits the essential syntax for making a desk of outcomes from ttest. The row dimension is command, the column dimension is outcome, and we place our ttest command within the command() possibility.

. desk (command) (outcome),                       
>       command(ttest age, by(highbp))
(output omitted)

I omitted the output as a result of desk tried to incorporate all of the ends in the desk and the output doesn’t match on the display screen. Let’s be extra particular in command() concerning the statistics we want to embody in our desk. Let’s add a column named Normotensive for the imply age of individuals with out hypertension, which is saved within the scalar r(mu_1) by ttest. We are able to additionally add a column named Hypertensive for the imply age of individuals with hypertension, which is saved within the scalar r(mu_2), a column named Diff for the distinction between the group means, and a column named pvalue that shows the p-value saved in r(p).

. desk (command) (outcome),                       
>       command(Normotensive = r(mu_1)            
>               Hypertensive = r(mu_2)            
>               Diff         = (r(mu_2)-r(mu_1))  
>               pvalue       =  r(p)              
>               : ttest age, by(highbp))

------------------------------------------------------------------------
                      |  Normotensive   Hypertensive       Diff   pvalue
----------------------+-------------------------------------------------
ttest age, by(highbp) |      42.16502       54.97281   12.80779        0
------------------------------------------------------------------------

The output shows the ttest command within the first column, adopted by the means, distinction, and p-value that we laid out in our desk command. I want to change the command identify within the desk with the label Age (years). Recall that dimensions have ranges and ranges can have labels. Let’s kind acquire label listing command, all to view the degrees and labels of the dimension command.

. acquire label listing command, all

  Assortment: Desk
   Dimension: command
       Label: Command possibility index
Degree labels:
           1  ttest age, by(highbp)

The output tells us that the dimension command has one stage, 1, which is labeled ttest age, by(highbp). We are able to change the label for stage 1 utilizing acquire label ranges. Then we are able to kind acquire preview to view our up to date desk.

. acquire label ranges command 1 "Age (years)", modify

. acquire preview

--------------------------------------------------------------
            |  Normotensive   Hypertensive       Diff   pvalue
------------+-------------------------------------------------
Age (years) |      42.16502       54.97281   12.80779        0
--------------------------------------------------------------

Subsequent we are able to use acquire fashion cell to vary the variety of decimals displayed in every cell and take away the best border from the primary column.

. acquire fashion cell outcome[Normotensive Hypertensive Diff], 
>         nformat(%6.1f)

. acquire fashion cell outcome[pvalue], nformat(%6.4f)

. acquire fashion cell border_block, border(proper, sample(nil))

. acquire preview

--------------------------------------------------------
             Normotensive   Hypertensive   Diff   pvalue
--------------------------------------------------------
Age (years)          42.2           55.0   12.8   0.0000
--------------------------------------------------------

We did it! Our desk seems good, however we’ve got solely included a take a look at for one variable. Let’s add extra.

Create a bigger desk

The apparent means so as to add extra checks to our desk could be so as to add extra command() choices. That may work as we are able to see under.

. desk (command) (outcome),                                   
>       command(Normotensive = r(mu_1) Hypertensive = r(mu_2) 
>               Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)        
>               : ttest age, by(highbp))                      
>       command(Normotensive = r(mu_1) Hypertensive = r(mu_2) 
>               Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)        
>               : ttest tcresult, by(highbp))                 
>       command(Normotensive = r(mu_1) Hypertensive = r(mu_2) 
>               Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)        
>               : ttest tgresult, by(highbp))                 
>       command(Normotensive = r(mu_1) Hypertensive = r(mu_2) 
>               Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)        
>               : ttest hdresult, by(highbp))                 
>       nformat(%6.2f  Normotensive Hypertensive Diff)        
>       nformat(%6.4f  pvalue)

--------------------------------------------------------------------------
                           |  Normotensive   Hypertensive    Diff   pvalue
---------------------------+----------------------------------------------
ttest age, by(highbp)      |         42.17          54.97   12.81   0.0000
ttest tcresult, by(highbp) |        208.73         229.88   21.15   0.0000
ttest tgresult, by(highbp) |        129.23         166.04   36.81   0.0000
ttest hdresult, by(highbp) |         49.94          49.22   -0.73   0.0195
--------------------------------------------------------------------------

However our desk command is rising sooner than our desk. Thankfully, we are able to use a little bit programming trick to make our process simpler and our code neater. Discover that many of the code in our command() choices is similar. We are able to retailer that code in a native macro and use the native macro in our command() choices. I’ve saved the instructions in an area macro named myresults.

. native myresults "Normotensive = r(mu_1) Hypertensive = r(mu_2) Diff = (r(mu_2)-r(mu_1)) pvalue = r(p)"

. show "`myresults'"
Normotensive = r(mu_1) Hypertensive = r(mu_2)  Diff = (r(mu_2)-r(mu_1))  pvalue = r(p)

Now we are able to change the prolonged column definitions with the native macro `myresults’ in our command() choices.

. desk (command) (outcome),                                   
>       command(`myresults' : ttest age,      by(highbp))     
>       command(`myresults' : ttest tcresult, by(highbp))     
>       command(`myresults' : ttest tgresult, by(highbp))     
>       command(`myresults' : ttest hdresult, by(highbp))     
>       nformat(%6.2f  Normotensive Hypertensive Diff)        
>       nformat(%6.0f  pvalue)

--------------------------------------------------------------------------
                           |  Normotensive   Hypertensive    Diff   pvalue
---------------------------+----------------------------------------------
ttest age, by(highbp)      |         42.17          54.97   12.81        0
ttest tcresult, by(highbp) |        208.73         229.88   21.15        0
ttest tgresult, by(highbp) |        129.23         166.04   36.81        0
ttest hdresult, by(highbp) |         49.94          49.22   -0.73        0
--------------------------------------------------------------------------

Let’s add the remainder of the lab variables to our desk utilizing our intelligent new trick.

. desk (command) (outcome),                                
>       command(`myresults' : ttest age,      by(highbp))  
>       command(`myresults' : ttest hgb,      by(highbp))  
>       command(`myresults' : ttest hct,      by(highbp))  
>       command(`myresults' : ttest iron,     by(highbp))  
>       command(`myresults' : ttest albumin,  by(highbp))  
>       command(`myresults' : ttest vitaminc, by(highbp))  
>       command(`myresults' : ttest zinc,     by(highbp))  
>       command(`myresults' : ttest copper,   by(highbp))  
>       command(`myresults' : ttest lead,     by(highbp))  
>       command(`myresults' : ttest top,   by(highbp))  
>       command(`myresults' : ttest weight,   by(highbp))  
>       command(`myresults' : ttest bmi,      by(highbp))  
>       command(`myresults' : ttest bpsystol, by(highbp))  
>       command(`myresults' : ttest bpdiast,  by(highbp))  
>       command(`myresults' : ttest tcresult, by(highbp))  
>       command(`myresults' : ttest tgresult, by(highbp))  
>       command(`myresults' : ttest hdresult, by(highbp))
--------------------------------------------------------------------------------
                           |  Normotensive   Hypertensive        Diff     pvalue
---------------------------+----------------------------------------------------
ttest age, by(highbp)      |      42.16502       54.97281    12.80779          0
ttest top, by(highbp)   |      167.7243       167.5506   -.1736495   .3661002
ttest weight, by(highbp)   |      68.26626       76.85565    8.589386   9.1e-181
ttest bmi, by(highbp)      |      24.20231       27.36081    3.158506   4.5e-241
ttest bpsystol, by(highbp) |       116.485       150.5388    34.05383          0
ttest bpdiast, by(highbp)  |      74.17222       92.01394    17.84172          0
ttest tcresult, by(highbp) |      208.7272       229.8798     21.1526   4.3e-105
ttest tgresult, by(highbp) |      129.2284       166.0427     36.8143   7.01e-41
ttest hdresult, by(highbp) |      49.94449       49.21784   -.7266526   .0194611
ttest hgb, by(highbp)      |      14.14038       14.42436    .2839752   4.99e-25
ttest hct, by(highbp)      |      41.65235       42.44271    .7903588   2.16e-27
ttest iron, by(highbp)     |       101.842       96.17436   -5.667648   5.70e-17
ttest albumin, by(highbp)  |      4.680295       4.654088   -.0262068   .0000896
ttest vitaminc, by(highbp) |      1.048238       1.016469   -.0317686   .0070212
ttest zinc, by(highbp)     |      87.06462       85.74782   -1.316802   .0000162
ttest copper, by(highbp)   |      125.0756       126.3356    1.259952   .0673572
ttest lead, by(highbp)     |      13.87513       14.93369    1.058555   2.36e-09
--------------------------------------------------------------------------------

Subsequent we are able to change the labels of the degrees of the dimension command. Let’s start by itemizing the labels for every stage.

. acquire label listing command, all

  Assortment: Desk
   Dimension: command
       Label: Command possibility index
Degree labels:
           1  ttest age, by(highbp)
          10  ttest top, by(highbp)
          11  ttest weight, by(highbp)
          12  ttest bmi, by(highbp)
          13  ttest bpsystol, by(highbp)
          14  ttest bpdiast, by(highbp)
          15  ttest tcresult, by(highbp)
          16  ttest tgresult, by(highbp)
          17  ttest hdresult, by(highbp)
           2  ttest hgb, by(highbp)
           3  ttest hct, by(highbp)
           4  ttest iron, by(highbp)
           5  ttest albumin, by(highbp)
           6  ttest vitaminc, by(highbp)
           7  ttest zinc, by(highbp)
           8  ttest copper, by(highbp)
           9  ttest lead, by(highbp)

Discover that the degrees are sorted as strings fairly than numbers. It is because ranges might be strings or numbers. We are able to see the variable names related to every stage, and we are able to relabel them utilizing acquire label ranges.

. acquire label ranges command 1  "Age (years)"                 
>                              10 "Peak (cm)"                 
>                              11 "Weight (kg)"                 
>                              12 "Physique Mass Index"             
>                              13 "Systolic Blood Stress"     
>                              14 "Diastolic Blood Stress"    
>                              15 "Serum ldl cholesterol (mg/dL)"   
>                              16 "Serum triglycerides (mg/dL)" 
>                              17 "Excessive density lipids (mg/dL)" 
>                              2  "Hemoglobin (g/dL)"           
>                              3  "Hematocrit (%)"              
>                              4  "Serum iron (mcg/dL)"         
>                              5  "Serum albumin (g/dL)"        
>                              6  "Serum vitamin C (mg/dL)"     
>                              7  "Serum zinc (mcg/dL)"         
>                              8  "Serum copper (mcg/dL)"       
>                              9 "Lead (mcg/dL)"                
>                              , modify

. acquire preview

---------------------------------------------------------------------------------
                            |  Normotensive   Hypertensive        Diff     pvalue
----------------------------+----------------------------------------------------
Age (years)                 |      42.16502       54.97281    12.80779          0
Peak (cm)                 |      167.7243       167.5506   -.1736495   .3661002
Weight (kg)                 |      68.26626       76.85565    8.589386   9.1e-181
Physique Mass Index             |      24.20231       27.36081    3.158506   4.5e-241
Systolic Blood Stress     |       116.485       150.5388    34.05383          0
Diastolic Blood Stress    |      74.17222       92.01394    17.84172          0
Serum ldl cholesterol (mg/dL)   |      208.7272       229.8798     21.1526   4.3e-105
Serum triglycerides (mg/dL) |      129.2284       166.0427     36.8143   7.01e-41
Excessive density lipids (mg/dL) |      49.94449       49.21784   -.7266526   .0194611
Hemoglobin (g/dL)           |      14.14038       14.42436    .2839752   4.99e-25
Hematocrit (%)              |      41.65235       42.44271    .7903588   2.16e-27
Serum iron (mcg/dL)         |       101.842       96.17436   -5.667648   5.70e-17
Serum albumin (g/dL)        |      4.680295       4.654088   -.0262068   .0000896
Serum vitamin C (mg/dL)     |      1.048238       1.016469   -.0317686   .0070212
Serum zinc (mcg/dL)         |      87.06462       85.74782   -1.316802   .0000162
Serum copper (mcg/dL)       |      125.0756       126.3356    1.259952   .0673572
Lead (mcg/dL)               |      13.87513       14.93369    1.058555   2.36e-09
---------------------------------------------------------------------------------

Lastly, let’s format the numbers utilizing acquire fashion cell and take away the best border from the primary column.

. acquire fashion cell outcome[Normotensive Hypertensive Diff], nformat(%8.2f)

. acquire fashion cell outcome[pvalue], nformat(%6.4f)

. acquire fashion cell border_block, border(proper, sample(nil))

. acquire preview

-------------------------------------------------------------------------
                             Normotensive   Hypertensive    Diff   pvalue
-------------------------------------------------------------------------
Age (years)                         42.17          54.97   12.81   0.0000
Peak (cm)                        167.72         167.55   -0.17   0.3661
Weight (kg)                         68.27          76.86    8.59   0.0000
Physique Mass Index                     24.20          27.36    3.16   0.0000
Systolic Blood Stress            116.49         150.54   34.05   0.0000
Diastolic Blood Stress            74.17          92.01   17.84   0.0000
Serum ldl cholesterol (mg/dL)          208.73         229.88   21.15   0.0000
Serum triglycerides (mg/dL)        129.23         166.04   36.81   0.0000
Excessive density lipids (mg/dL)         49.94          49.22   -0.73   0.0195
Hemoglobin (g/dL)                   14.14          14.42    0.28   0.0000
Hematocrit (%)                      41.65          42.44    0.79   0.0000
Serum iron (mcg/dL)                101.84          96.17   -5.67   0.0000
Serum albumin (g/dL)                 4.68           4.65   -0.03   0.0001
Serum vitamin C (mg/dL)              1.05           1.02   -0.03   0.0070
Serum zinc (mcg/dL)                 87.06          85.75   -1.32   0.0000
Serum copper (mcg/dL)              125.08         126.34    1.26   0.0674
Lead (mcg/dL)                       13.88          14.93    1.06   0.0000
-------------------------------------------------------------------------

Export the desk to Microsoft Phrase

As soon as we’re pleased with the structure of our desk, we are able to export it to many alternative file codecs. I’m going to make use of putdocx, acquire fashion putdocx, and putdocx acquire to export our desk to a Microsoft Phrase doc. A lot of you’ll discover that the instructions under are nearly similar to the instructions in my earlier posts about tables.

. putdocx clear

. putdocx start

. putdocx paragraph, fashion(Title)

. putdocx textual content ("Hypertension in the USA")

. putdocx paragraph, fashion(Heading1)

. putdocx textual content ("The Nationwide Well being and Diet Examination Survey (NHANES)")

. putdocx paragraph

. putdocx textual content ("Hypertension is a serious explanation for morbidity and mortality in ")

. putdocx textual content ("the USA.  This report will discover the predictors ")

. putdocx textual content ("of hypertension utilizing the NHANES dataset.")

. acquire fashion putdocx, structure(autofitcontents)               
>         title("Desk 2: Comparability of demographic, anthropometric, and lab outcomes by Hypertension Standing")

. putdocx acquire
(assortment Desk posted to putdocx)

. putdocx save MyTable2.docx, change
efficiently changed "MyTable2.docx"

graph1

Conclusion

On this submit, we discovered the way to use the command() possibility with the desk command to create a desk of statistical checks. The steps are easy: run the command of curiosity, kind return listing to view the statistics left in reminiscence, create your desk utilizing the row dimension command and the column dimension outcome, and place your command within the command() possibility. It’s possible you’ll want to specify customized columns for the statistics in your desk, and we discovered the way to use native macros to simplify that process.

I’ll present you the way to use the command() choice to create a desk of regression coefficients in my subsequent submit.



Related Articles

Latest Articles