asdoc: Export matrix to MS Word | the case of xttab command in Stata

  • 2

asdoc: Export matrix to MS Word | the case of xttab command in Stata

Category:asdoc,Blog

asdoc provides a variety of ways in which results from various Stata commands can be exported to MS Word or an RTF file. In this blog post,  I show how to export a Stata matrix to MS word. Usually, Stata commands leave results in r() or e() macros and sometimes in a Stata matrix. Consider the example of xttab command.  xttab is a generalization of tabulate oneway. It performs one-way tabulations and decomposes counts into between and within components in panel data. The command returns results in the r(results) matrix which we can then send to MS word.

 

The syntax

asdoc follows the following syntax for exporting matrix to a word document.

asdoc wmat, matrix(matrix_name) [rnames(row names) cnames(row names) replace append other_options]

 

Description

wmat is the command name – an abbreviation for writing matrix. Option matrix() is a required option to get the name of an existing matrix. Option rnames() and cnames() are optional options to specify row names and column names of the matrix. If these options are left blank, existing row and column names of the matrix are used. Other options of asdoc can also be used with wmat. For example, replace will replace an existing output file, while append will append to the existing file. fs() sets the font size, while option title() can be used to specify the title of the matrix in the output file.

An example: The case of xttab command

The dataset that we shall use is from the help file of xttab.

webuse nlswork
xtset id year
xttab race
mat T = r(results)
asdoc wmat, mat(T) replace

 

Explanation

1. The first line downloads the example data

2. The second line declares the data as panel data

3. The third line tabulates the race variable

4. The fourth line creates a matrix with the name T from the xttab command

5. The fifth line writes the T matrix to a Word file. wmat is a sub-command in asdoc for writing matrix data to the output file. The two words after command are options of asdoc. The first option tells asdoc about the name of the matrix that has to be exported. The second option tells asdoc to replace any existing output file.

asdoc produces the following Table.

Results Table

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

20180

70.723

3329

70.664

r2

2

8051

28.215

1325

28.126

r3

3

303

1.062

57

1.21

r4

3

28534

100

4711

100

 

Over a grouping variable?

If we wished to do the above for each category of the grouping variable msp, that has two categories i.e., 0 and 1, we can use the if qualifier and append the results to the same file. So

xttab race if msp == 1
mat T = r(results)
asdoc wmat, mat(T) replace title(When msp == 1)
xttab race if msp == 0
mat T = r(results)
asdoc wmat, mat(T) title(When msp == 0)

 

When msp == 1

 

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

13321

77.475

2747

75.405

100

r2

2

3682

21.414

853

23.415

100

r3

3

191

1.111

43

1.18

100

r4

3

17194

100

3643

100

100

 


When msp == 0

 

 value

 Overall:Freq

 Overall:Pe~t

 Between:Freq

 Between:Pe~t

 Within:Per~t

r1

1

6853

60.517

2046

65.724

100

r2

2

4359

38.493

1036

33.28

100

r3

3

112

.989

31

.996

100

r4

3

11324

100

3113

100

100

 


Leave a Reply

2 Comments

Jay Stiles

September 13, 2018at 8:38 am

Thank you very much for this extra information.

This is very helpful.

Many thanks,

J

Jacob Schilke

September 14, 2018at 11:17 pm

asdoc provides a whole bunch of options to virtually send anything from Stata to MS word. I would say it is a big breakthrough for Stata users.

Leave a Reply

5 × three =