Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

st: RE: Adding output created in Mata to a Stata dataset.

From   Amy Dunbar <>
To   "" <>
Subject   st: RE: Adding output created in Mata to a Stata dataset.
Date   Wed, 29 Sep 2010 13:19:10 +0000

Thank you, Steve. I tried the code in Gould's response to you, but I get a conformability error. I want to write one value (Y[2,1]) for every firm for date1. I tried "getmata" but I can't make that work either. 

Amy Dunbar

Date: Mon, 27 Sep 2010 23:42:30 -0400
From: Steve Samuels <>
Subject: Re: st: Adding output created in Mata to a Stata dataset.

will answer your question.


-----Original Message-----
From: Amy Dunbar 
Sent: Monday, September 27, 2010 9:51 PM
To: ''
Subject: Adding output created in Mata to a Stata dataset.

My dataset has daily returns for 816 trading days for  1200 companies. To create an equally weighted daily return across this portfolio of firms, I could use bysort date: egen meanret=mean(ret).  
But I want to create a weighted return based on a company characteristic, e.g., size.  My ultimate purpose is use 
reg meanret vwretd event1-event11
where meanret would be the weighted return for each trading day, vwret is the value-weighted market return, and event1-event11 are event days for a market study, where the coefficient on an event date would be the abnormal return. See Sefcik, S.E., and R. Thompson. 1986. An Approach to Statistical Inference in Cross-Sectional Models with Security Abnormal Returns As Dependent Variable. Journal of Accounting Research 24 (2):316-334.

The following do file creates a weighted daily return that varies by date but is the same for each company in the dataset (just as meanret would be the same).  I created a small dataset below, where r is one day of returns for 10 companies, and v is a company characteristic.   I read Gould. 2006. Mata Matters: Creating new variables -sounds boring, isn't. The Stata Journal 6 (1): 112-123, but I still don't understand how to use st_addvar() and st_store to add the second row of the following Y matrix (created by the do file below) to each observation in the original dataset. I also read Cameron and Trivedi, 2009, Microeconometrics Using Stata, Ch 3, OLS Using Mata, and Appendix B, Mata. The solution is probably there, but I am a mata newbie.

  1 |   .0076411483  |
  2 |  -.0111004781  |
In the dataset below, I want each obs (row) to have -.0111004781 as the weighted return for trading date 1.  If I had all 816 trading dates and 1200 companies, Y would be 2 X 816; I want to add the 816 daily weighted return variables to my dataset, which would be the same across the companies.

* do file creates weighted return for a portfolio of 10 firms
* create dataset for one trading day, where r is return and v is a firm characteristic.
input r v
0.002	0.2
0.001	0.4
0.004	0.5
0.001	0.4
0.009	0.1
0.004	0.2
0.006	0.3
0.007	0.4
0.001	0.5
0.007	0.1

gen cons = 1

st_view(R=., ., ("r"))
st_view(F=., ., ("cons", "v"))
W = FFinv * F'
Y = W*R  //create weighted returns

How do I add the weighted return for trading day 1 (-.0111004781) to each observation in my Stata dataset?
Thank you for considering my request for help.

Amy Dunbar
University of Connecticut
School of Business
Department of Accounting
cell 860-208-2737

*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index