Extracting Specific Rows from asdocx Tables

With the new “Extracting Specific Rows from asdocx Table in Stata” feature in asdocx, you can now take control of your table data like never before. This powerful feature gives your the power to extract specific rows from asdocx-generated tables and seamlessly compile them into customized new table. In this guide, we’ll walk you through the process of extracting rows from asdocx tables and creating new tables to suit your exact needs.

Step by step guide to row extraction


To extract specific rows and construct a new table from them using asdocx, use the keeprow() option within your asdocx code. The following rules are followd:

1. Using the keeprow() Option

To initiate row extraction and table building, simply include the keeprow() option in your asdocx code. This option serves as the key to specifying which rows you wish to retain in your table.

2. Initial Usage of keeprow()

When you add the keeprow() option for the first time, asdocx will perform the following actions:

Header Row Retention: asdocx automatically retains the header row of the table. There’s no need to specify it explicitly; it’s included by default.

Specified Row Extraction: You can specify the row(s) you want to retain in the table using the keeprow() option. Everything else in the table will be removed.

3. Subsequent keeprow() Usage

For all subsequent runs of your code, asdocx will behave as follows:

Row Extraction: It extracts the specified row(s) from the table body while retaining the previously collected rows.

Appending Rows: Extracted rows are appended to the previously collected rows

4. Termination of the extraction

asdocx will continue to build the table until it encounters a run where the keeprow() option is not provided, or when option replace is used. At this point, it will cease appending rows and finalize the table.

5. Exporting the table

Once the table is ready, use the asdocx export command to export the table to Word, Excel, LaTeX, or HTML. You can export to these formats by using the option save(Myfile.docx), save(Myfile.xlsx), save(Myfile.text) or save(Myfile.html), see this page for more details.

Real-World Use Cases


An asdocx’s user wrote :

I would like to generate a table, which will present the results from univariable analysis for all variables. I used the following, but I got different tables saved in the same word document. I want them to be saved in the same table. How should I do it?Restore

preserve

stset time_recurrence, fail(Recurrence==1)

foreach var of varlist death card mi stroke ret {

asdocx logistic `var' i.robotic, vce(id) label template(reg1) dec(2) dect(3) btp

}

The problem

The above code uses the reg1 template of asdocx. As shown on the reg1 template page, the template creates a regression table that is commonly used in the medical / epidemiology research. The above code generates separate table for each regression. The user wants to collect only the row where the cofficients of the independent variables are reported and build a table from it.

The solution

The preceding problem presents an excellent opportunity to utilize the keeprow() option. Rather than employing a loop, as in the previous code, I will post the regression code for each variable line by line. This approach enhances clarity and understanding. Let’s begin by running the initial regression to identify the row(s) we wish to extract

use https://asdocx.com/robotics.dta, clear

asdocx logistic death i.robotic, vce(cl id) label template(reg1) replace


Table: Regression Results - death

0 |1 2 3
----+-------------------------------------------------------
1 |Variable OR [95%% CI] P-value
----+-------------------------------------------------------
2 |(Reference: Manual)
3 | Robotic 0.314 [0.133, 0.741] 0.008
4 |Constant 0.021 [0.014, 0.032] 0.000
5 |Observations 2128

------------------------------------------------------------


At the top of the table, column numbers are displayed, while the table rows are listed on the extreme left. The table row headers are located in the first row. To extract a specific row, such as row number 3 (‘Robotic’), we can commence the creation of our custom table.


* This the first run of keeprow(): The output table has both

* the header row and row 2

asdocx logistic death i.robotic, vce(cl id) ///
label template(reg1) keeprow(3) btp replace



0 |1 2 3

----+-------------------------------------------------------

1 |Variable OR [95% CI] P-value

----+-------------------------------------------------------

2 | Robotic 0.314 (0.133, 0.741) 0.008

------------------------------------------------------------




* The second time we use keeprow(), only the given row from

* table body is appended. Also note, I am not using replace

asdocx logistic card i.robotic, vce(cl id) label template(reg1) keeprow(3) btp

0 |1 2 3

----+-------------------------------------------------------

1 |Variable OR [95%% CI] P-value

----+-------------------------------------------------------

2 | Robotic 0.314 (0.133, 0.741) 0.008

3 | Robotic 0.249 (0.053, 1.177) 0.079

------------------------------------------------------------


Since the dependent variable in both regressions is ‘robotic,’ we can see that the row label does not provide much information. Instead, we would prefer to report the dependent variable label here. One approach could be to change the value label for the ‘robotic’ variable each time before running the regression. The second approach involves using ‘flexmat‘ to directly overwrite the labels once the table is complete or immediately after each regression. In the following code, I will now provide the full code using a loop and also demonstrate the ‘flexmat’ approach for modifying the table rows directly

* Running first regression outside loop 

* because replace cannot be used inside a loop

asdocx logistic death i.robotic, vce(cl id) label template(reg1) ///

keeprow(3) btp replace save(row.tex)

‎ ‎
* Get variable labels

loc depvar : var label death


* Write the labels in the new table

flexmat addcell, data(`depvar') row(2)

loc nextrow = 3

foreach var of varlist card mi stroke ret {


asdocx logistic `var' i.robotic, vce(cl id) template(reg1) keeprow(3) btp


* replace the row label in the new table

loc depvar : var label `var'

flexmat addcell, data(`depvar') row(`nextrow')

loc ++nextrow

}

* Export the newly created table

asdocx export


Stata custom tables from extracting rows from asdocx tables and building a new table

Details of the keeprow() Option

The keeprow() option accepts the following formats, each with its corresponding behavior:

  • keeprow(#): Use a single integer to retain a specific row.
  • keeprow(#, #, #): List different rows separated by commas.
  • keeprow(#-#): Specify a range or contiguous rows to keep.


order asdocx  Order asdocx


Yearly license of asdocx is available at $9.99. Its life-time license is available at $49.99. With the asdocx membership, you get :

  • Life-time license to use
  • All future updates
  • All premium templates / plugins.
Order asdocx