Labeling variables for putdocx table (HW4 and final)

Labeling variables for putdocx table (HW4 and final)

by Eleni Jaswa -
Number of replies: 4

Hi!

I am re-labeling variables using the command: 

label variable oldvarname "New variable name"

However, when I then create a table using the putdocx command (for example Table 1), the variable names listed in the rows retain the original (hard to interpret) variable names.

Is there a workaround (other than editing in the word document?)

Thanks!

In reply to Eleni Jaswa

Re: Labeling variables for putdocx table (HW4 and final)

by Crystal Langlais -
Hi Eleni,

Thanks for posting.

The code you provide in line two (label variable oldvarname "New variable name") would more appropriately be described as: label variable varname "varlabel". The distinction is small but worth pointing out: you are not renaming your variable (you would use the 'rename' command for that) but rather labeling your variable. This might help clarify which options might be useful.

What method are you using to create the table before you use putdocx to output it? It may be as simple as specifying the "varlabel" option. This tells Stata to print the variable labels rather than using the variable names. For some methods, this is much less simple.
In reply to Crystal Langlais

Re: Labeling variables for putdocx table (HW4 and final)

by Eleni Jaswa -
Thanks, Crystal.
Below is my code:

===

*Re-label variables to be clearer in table
label define bmi2obesevsrestlab 0 "Not obese" 1 "Obese"
label values bmi2obesevsrest bmi2obesevsrestlab

rename variable bmi2obesevsrest "BMI Group"
label variable coll_egg_age "Age (yrs)"
label variable bmi2 "Body mass index"
label variable mii_total "Mature eggs collected"


*Table 1 commands to export to doc
tabstat coll_egg_age bmi2 mii_total, by(bmi2obesevsrest) stat(mean sd) col(stat) long nototal save
return list

matrix obesestats = r(Stat2)'
matlist obesestats

matrix nonobesestats = r(Stat1)'
matlist nonobesestats

putdocx clear
putdocx begin

putdocx table ObeseTable1 = matrix(obesestats), rownames colnames layout(autofitcontents) title("Obese Group")

putdocx table NonObeseTable1 = matrix(nonobesestats), rownames colnames layout(autofitcontents) title("Non-obese Group")

putdocx save "Final Project.docx", replace

===

I tried
1) adding "varlabel" as an option
(putdocx table ObeseTable1 = matrix(obesestats), rownames colnames layout(autofitcontents) title("Obese Group") varlabel)
But I get the error: "option varlabel not allowed"
2) changing from labeling to renaming the variables
(rename variable bmi2obesevsrest BMI_Group)
But I get the error: "syntax error
Syntax is
rename oldname newname [, renumber[(#)] addnumber[(#)] sort ...]
rename (oldnames) (newnames) [, renumber[(#)] addnumber[(#)] sort ...]
rename oldnames , {upper|lower|proper}"

I'm totally fine editing the variable names directly in the word document, but wanted to make sure I wasn't missing something I should've learned from class.

Appreciate the help!
In reply to Eleni Jaswa

Re: Labeling variables for putdocx table (HW4 and final)

by Crystal Langlais -
Eleni,
Thanks for sharing your code.

Unfortunately, some of the variable labeling efforts are lost once transported to a matrix. You can use the following commands to label the columns and rows of the matrix once you create it (replace capitalized words with your values):
matrix colnames MATRIXNAME = "COL 1 NAME" "COL 2 NAME"...."COL N NAME"
matrix rownames MATRIXNAME = "ROW 1 NAME" "ROW 2 NAME"...."ROWNNAME"

Notes:
-quotes are necessary when one of the row or column names is more than one word in length
-provide the same number of row or column names as there are rows or columns in your matrix. If you don't, Stata will replace the subsequent rows or columns with the last label in the list.


In regards to the renaming command, you don't need "variable" in code. So you could rename your variable using
rename bmi2obesevsrest BMI_Group
rather than
rename variable bmi2obesevsrest BMI_Group
That said, this still requires editing within the word doc so doesn't fully address your problem.

Hope this helps,
Crystal