Introduction

Option row is a new feature in version 2.0 of asdoc. This feature allows building tables in pieces. That is good news for those who want to make highly customized tables from Stata output.

This feature can be considered an advanced topic and might not be good for Stata beginners. With many other Stata commands, using asdoc is exceptionally easy. You can read this concise blog post for some basic examples of using asdoc. However, if you are already familiar with Stata macros and results returned in r() and e() macros, then you should continue reading this post.

How does option row work?


Option row allows building a table row by row from text and statistics. In each run of asdoc with option row, a row is added to the output table. The syntax for using this option is given below:

asdoc, row(data1, data2, data3, ...)

As shown above, we shall type nothing after the word asdoc. Therefore, all other arguments of the command come after the comma. The first required option is row(data1, data2, …). Here data1, data2, … can be either a numeric value, string, or both. Within the brackets after option row, each piece of data should be separated by the character comma and hence it will be written to a separate cell in the output table. If a cell is empty, then each comma should be accompanied by a backslash that is  “,\”

Options


We can use the following options when using option row. dec(): for specifying the number of decimal points. If not used, the default is to use three decimal points. An example of using this option could be dec(2) for using two decimal points. title() : This will add a title to the table. This option works only when the row option is used for the first time in the creation of a table. For example, title(Descriptive Statistics). replace: this option will replace any existing file. Without option replace, the default is to append results.

save(): This will save file with the specified name. For example, save(Table 1) will save the file with the name Table 1. 

A simple example


To understand how does the option row work, let us write first the table column title and then some data. Let us create a table that has four columns. The columns are named as KP, Sindh, Baluchistan, and Punjab. We shall write the table title as Provincial GDP of Pakistan.  So the first row is the header row

 asdoc, row(Years, KP, Sindh, Baluchistan, Punjab) title(Provincial GDP of Pakistan over years) replace

The above line of code generates the table title and the header row. Please note that we also included Years in the table columns because we shall report the provincial GDP over years, therefore we need one additional column for displaying the year labels in the first column. Now let us continue writing to this table. Make sure that you close the Word file before writing additional rows to it.

asdoc, row(1999, 2500.55, 4000.35, 1000.21,  5500.74) dec(2)

In the second line of code, we did not write replace as we wanted to append the results to the same file “MyFile.doc” and we also skipped the title option. We used option dec(2) to report two decimal points with numeric values. We can continue writing additional rows to this table.

asdoc, row(200, 2600.25, 4500.35, 1100, 5700.87) dec(2)

Collecting stats with option accum


We can create a table from text and statistics that are collected from different Stata commands. There is one challenge to developing such a flexible table with option row – that a given row has to be written in one go. So once a row is written, no further cells can be appended to the same row. This means that we need to first collect all the required bits of information before writing a row. Collecting and holding these bits of information can be tricky or too time-consuming. To facilitate this process, asdoc offers option accum(data1 data2,…). The word accum is an abbreviation that I use for accumulate. The syntax of this option is given below:

asdoc, accum(data1 data2 data3 data4 data5 ...) [ dec(#) show ]

Actually, the above command can be run as long as the limit of the global macro to hold data is not reached. The above command will accumulate text and statistics from different runs of asdoc and hold them in the global macro ${accum}. Once we have accumulated all the needed bits of information in the global macro, then its contents can be written to the Word table with option row. Option show can be used to show the contents of the global macro ${accum}. Assume that we want to build an odd table that presents the number of observations, mean, and standard deviation for two variables in two different time periods. The researcher wants to follow the following format:

webuse grunfeld, clear

asdoc, row( \i, \i, invest, \i, \i, kstock,\i) replace

asdoc, row( Periods, N, Mean, SD, N, Mean, SD)

sum invest if inrange(year , 1935, 1945)

asdoc, accum(`r(N)', `r(mean)', `r(sd)')

sum kstock if inrange(year , 1935, 1945)

asdoc, accum(`r(N)', `r(mean)', `r(sd)')

asdoc, row( 1935-1945, $accum)

 

Explanation:


1. The second row of our required table reveals that a total of 7 cells are needed, this is why we created 7 cells in the first line of code. The text ” \i,” is a way of entering an empty cell. We entered empty cells so that the variables names invest and kstocks are written in the middle of the table.

2. The second line of code writes the table header row.

3. The third line finds summary statistics. We shall collect our required statistics from the macros that are left behind in r() by the sum command.

4. The fourth line accumulates the required statistics for our first variable invest

5. We are not yet writing the accumulated statistics to the Word file. So we find statistics for our second variable kstocks in the fifth line.

6. We again accumulate the needed statistics for our second variable in the sixth line.

7. Since our row of required statistics is now complete, we write the accumulated statistics and the first-row label, i.e, 1935-1945 to our Word file. Let us write one more row to the table. This time, the statistics are based on years 1946-1954

sum invest if inrange(year , 1946, 1954)

asdoc, accum(`r(N)', `r(mean)', `r(sd)')

sum kstock if inrange(year , 1946, 1954)

asdoc, accum(`r(N)', `r(mean)', `r(sd)')

asdoc, row( 1946-1954, $accum)

 

Need more examples?


There was a question on Statalist for a customized table for reporting ttest results. Liu Qiang made an excellent use of the option row() of asdoc. See his solution here