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

# Re: st: Mata Data Structure or "variable" variable names for timeseries computations

 From Christophe Kolodziejczyk To statalist@hsphsun2.harvard.edu Subject Re: st: Mata Data Structure or "variable" variable names for timeseries computations Date Thu, 2 Aug 2012 17:07:41 +0200

```I think one solution is to use a vector of pointers where you store
with a loop over this vector.

As suggested by Tirthankar, another way is to pool all your datasets
and treat it like a panel with year is the identifier. Then you can
create a function which computes the first differences. See the
Panelsetup() in the mata manual in order to do this.

Hope this helps

Christophe

2012/8/1 Matthew McKay <matthew.mckay@anu.edu.au>:
> 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,
> Matthew
>
> 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
>     mata
>     Mcp_`year' = st_data(., .)
>     `END'
> }
>
> Step#2:
> ** MATA FUNCTION **
>
> clear all
> mata
> 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'
> !!!!!!!!!!
>   }
> }
> end
>
> Step #3:
> mata: calcdiffvectors(1995, 2010)
> // I can then retrieve the difference vectors using get mata etc. //
>
>

--
Christophe Kolodziejczyk
Research Fellow

AKF, Anvendt KommunalForskning
Danish Institute of Governmental Research
DK-1150 København K

*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/
```