Change format of scalar in tbl

Change format of scalar in tbl

by Logan Pierce -
Number of replies: 1

I am trying to create a table using the tbl command and various scalars that I have previously defined, however I am having trouble changing the format of the scalars in the table. I don't fully understand that section of the 'help tbl' file either. 

Can you give me an example of how to change the format of a scalar (to %9.2f) in my tbl command? Thanks.

In reply to Logan Pierce

Re: Change format of scalar in tbl

by Kristen -

Sure, here are two examples: (from the lecture 7 .do file)


webuse nhanes2, clear

tabstat zinc copper vitaminc iron, by(diabetes) stat(n mean sd) save

matrix stats1 = r(Stat1)

matrix stats2 = r(Stat2)


Example 1

local d_n : display %9.0f stats2[1,1]  

local d_z : display %9.2f stats2[2,1]  "("%9.1f stats1[3,1] ")"

local d_c : display %9.2f stats2[2,2]  "("%9.1f stats1[3,2] ")"

local d_v : display %9.2f stats2[2,3]  "("%9.1f stats1[3,3] ")"

local d_i : display %9.2f stats2[2,4]  "("%9.1f stats1[3,4] ")"


local nd_n : display %9.0f stats1[1,1]  

local nd_z : display %9.2f stats1[2,1]  "("%9.1f stats1[3,1] ")"

local nd_c : display %9.2f stats1[2,2]  "("%9.1f stats1[3,2] ")"

local nd_v : display %9.2f stats1[2,3]  "("%9.1f stats1[3,3] ")"

local nd_i : display %9.2f stats1[2,4]  "("%9.1f stats1[3,4] ")"



tbl ("Mean (SD) ", "N", "Zinc", "Copper", "Vitamin C", "Iron" \ ///

"_Diabetics_", `d_n', `d_z', `d_c', `d_v', `d_i' \ ///

"_Non diabetics_", `nd_n', `nd_z', `nd_c', `nd_v', `nd_i') ///

, title(Table 1: Serum Zinc, Copper, Vitamin C and Iron levels (mcg/DL) for Diabetics and Non-Diabetics in NHANES) 


Example 2

foreach var of varlist zinc copper vitaminc iron  {

      summarize `var'

 local `var'_n : display r(N)

      local `var'_mean : display %9.2f r(mean)

      local `var'_sd   : display %9.2f r(sd)

}



tbl ("Variable Name", "N", "Mean", "SD" \ ///

"_Zinc_",`zinc_n',`zinc_mean', `zinc_sd' \   ///

"_Copper_", `copper_n',`copper_mean', `copper_sd' \ ///

"_Vitamin C_",`vitaminc_n',`vitaminc_mean', `vitaminc_sd'       \ ///

"_Iron_", `iron_n',`iron_mean', `iron_sd') ///

, title("Table 1. Summary of _Zinc_, _Copper_, _Vitamin C_, and _Iron_")