Merge two mactrices with flexmat

 sysuse auto, clear
* Make one-way table for rep78 when foreign is 0
 asdocx tab rep78 if foreign == 0 , replace
 
  * Keep just the percentage column
 flexmat dropcol , col(2 ,4)
 
  *Repeat the above when foregin is 1
 asdocx tab rep78 if foreign == 1
 flexmat dropcol , col(2 ,4) loc(2)
 
  * Merge the two tables
 flexmat merge , matloc(1 ,2)
 flexmat showmat , locinfo
 
  * Change the column headers to reflect foreign categories
 flexmat addcell , data(Domestic) row(1) col(2)
 flexmat addcell , data(Foreign) row(1) col(3)
 
  * Export with asdocx
 asdocx export
 
Tabulation of rep78
Repair Record 1978 Domestic Foreign
1 4.17
2 16.67
3 56.25 14.29
4 18.75 42.86
5 4.17 42.86
Total 100.00 100.00
Notes: