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: Mata Data Structure or "variable" variable names for timeseries computations

From   Matthew McKay <[email protected]>
To   [email protected]
Subject   st: Mata Data Structure or "variable" variable names for timeseries computations
Date   Wed, 01 Aug 2012 14:49:34 +1000

Dear StataList,

I am trying to compute differences in sets of matrices matrices using MATA for a time series 1995 to 2010.
I am producing a mata function to compute the differences for each year after I load in ALL my yearly data matrices.
My Issue is in Step#2 is that I want to use a similar methodology to local macro's in do files. (which can't be done)

How can I pass part of a variable name (like Year) into the function and then perform a set of computations over the data that is loaded in MATA?
In MATA how can you cycle through different variables (substituting in different year append) and compute a new set of matrices?
Should I be constructing a Struct that contains a Data Matrix and Time Series Indicator?

I understand the MATA function is compiled and therefore local macro substitution can't be done ... but was wondering if anyone else has an elegant solution to reference different variables in MATA by changing a component of the variablename (knowing the variable is defined in memory).

Many Thanks,

Step #1:
** Import ALL MCP Matrix
local Years = "1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010"
clear all
local END = "end"
foreach year of local Years {
    use  [file_`year'.dta], clear              // Load MCP Matrices into Mata Memory //
    drop ReporterISO3C
    Mcp_`year' = st_data(., .)   


clear all
void calcdiffvectors(real scalar StartYear, real scalar EndYear) {
  for(year = StartYear; year < EndYear; year++) {
         !!!!!!!!! local NextYear = `year' + 1  !!!!!!!!!!!!!
         !!!!!!!!! Mcp_`year'_`NextYear' = Mcp_`year' :- Mcp_`NextYear'  !!!!!!!!!!

Step #3:
mata: calcdiffvectors(1995, 2010)
// I can then retrieve the difference vectors using get mata etc. //

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