I am attempting to write a Stata/Mata program which formats excel files using Mata, by looping over all .xlsx files in a specified directory. The first problem I have run in to is that Stata gives error message 199 "} is not a valid command name" when it reaches the end of the loop, after the "end" command for mata is processed. I'm assuming this is because Stata thinks "end" should close the program, however, I'm not sure what the solution is. The code is currently set up as follows:

Code:

prog formatxl args directory local list : dir "$root/`directory'" files "*.xlsx" foreach xlfile of local list { mata b = xl() b.load_book("`xlfile'") *formatting commands follow end } end

Thanks so much,

Nick Mahoney]]>

I am using the 'actest' command to conduct a serial correlation test after time-series regressions. However, I need to repeat the test 48 times (i.e., after each of the 48 regressions). So, I am trying to write a Stata code that generates the results automatically as follows:

matrix autocorr=J(48,7,.)

local i=1

forvalues j = 1/48 {

reg y x1 x2 x3 x4 if id==`j', robust

actest, r q0 lag(1/3)

matrix [`i',1]=(r(p))

i=`i'+1

}

matrix list autocorr

I get the message:

matrix operation not found

r(501);

I need help on how to fix the issue.

Thanks in advance for your reply.

Emna]]>

I am running a two-step joint dynamic panel GMM estimator on Mata, with the stacked formula: b = (inv(x'*z*inv(z'*(e*e')*z)*z'*x))*(x'*z*inv(z'*(e* e')*z)*z'*y), where x refers to matrix of regressors, z refers to matrix of instrumental variables and e refers to vector of (1st-Step) residuals.

For the above two step formula, for a single equation, I calculate the variance of the moment conditions (z'*(e*e')*z), (let's call it S) as first step towards deriving cross-section heteroscedasticity robust weighing matrix

k = cols(z)

st_view(id=. , . , ("id"))

info = panelsetup(id,1)

nc = rows(info)

S= J(k,k,0)

for(i=1; i<=nc; i++) {

panelsubview(zi = . , z , i , info)

panelsubview(ei = . , e , i , info)

S = S + zi'*(ei*ei')*ei

}

The problem arises since I have a large number of cross-sections in my panel dataset, which has 2941 cross-sectional units spread across 14 years. Thus the matrix S at the end of the loop is giving null values. Where am I going wrong ? On datasets with shorter cross-sectional counts, I can calculate S just fine, but in this instance the S matrix is returning null / no values. Any other way I may derive the S matrix ?

]]>

I have an unbalanced panel dataset consisting of firms (permno) and monthly dates (month_id).

I am trying to assign to each deviation (defined as current reporting variable minus the mean of the previous three quarters) a percentile value relative to all stocks’ deviations in the previous year.

The following code solved the problem but is super slow as I am working with a huge dataset:

foreach x in cheq_deviation oiadpq_deviation req_deviation saleq_deviation capxy_deviation icaptq_deviation invtq_deviation {

g `x'_rank = . //rank compared with all permno's in that 1 year day window

g `x'_nobs = . //contains total considered in ranking

loc d = 12 //set window length here

su month_id, meanonly

quietly forval n = `r(min)'/`r(max)' {

count if month_id == `n'

if r(N) {

tempvar i j

egen `i' = rank(`x') if inrange(month_id, `n' - `d' + 1, `n'), track

replace `x'_rank = `i' if month_id == `n'

egen `j' = count(`x') if inrange(month_id, `n' - `d' + 1, `n')

replace `x'_nobs = `j' if month_id == `n'

drop `i' `j'

}

}

}

Using Mata would probably be way faster. The following code does not properly work but would look somehow like this:

mata:

mata clear

real rowvector myrank(real matrix X)

{

real matrix R

R = mm_ranks(X,1,1)

return(rows(X), R[rows(X)])

}

end

rangestat (myrank) cheq_deviation, interval (month_id -12 0) casewise

I need the relative ranking across ALL firms for the previous year. The number of observations when running in the code are perfectly fine. However, I want different rankings for different firms. Within the current (latest) month of the event window all ranks are the same across all firms, even though the values for the deviations are different across firms.

Also can I combine mata with the "foreach" loop?

Thank you very much in advance!

Best,

Michael

]]>

Code:

clear matrix C = (.25, .5*.25 \ .5*.25, .25) set seed 12345 drawnorm u0 u1, n(2000) cov(C) /* normal r.v. */ generate son = exp(u1) /* lognormal r.v.*/ generate dad = exp(u0) generate son_disc = irecode(u1, -1, -0.5, 0, 0.5, 1) /*discrete r.v.*/ generate dad_disc = irecode(u0, -1, -0.5, 0, 0.5, 1) drop u* capture program drop myindex program myindex, rclass syntax varlist(min=2 max=2 numeric) [if] [in] [, tau(real 0) s(real 0.25)] marksample touse tempvar y x ry rx diff tempname num den tokenize ‘varlist’ quietly { generate ‘y’ = ‘1’ if ‘touse’ generate ‘x’ = ‘2’ if ‘touse’ cumul ‘y’, gen(‘ry’) cumul ‘x’, gen(‘rx’) count if (‘ry’-‘rx’) > ‘tau’ & ‘rx’ <= ‘s’ & ‘touse’ scalar ‘num’ = r(N) count if ‘rx’ <= ‘s’ & ‘touse’ scalar ‘den’ = r(N) return scalar UW = ‘num’/‘den’ } end igmobil son dad, nosingle noinequal userwritten(myindex son dad, tau(0.1) s(0.25)) classes(4)

Code:

marksample touse tempvar y x ry rx diff

]]>