Bookmark and Share

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


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

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


From   Nick Cox <[email protected]>
To   [email protected]
Subject   Re: st: Mata Data Structure or "variable" variable names for timeseries computations
Date   Wed, 1 Aug 2012 10:00:22 -0500

Tirthankar gave a detailed reply with lots of good-looking code. On
the detail that Mata [sic] does not have macros in the same way as
Stata, see

SJ-11-2 pr0052  . . . . Stata tip 100: Mata and the case of the missing macros
        . . . . . . . . . . . . . . . . . . . . . . . . W. Gould and N. J. Cox
        Q2/11   SJ 11(2):323--324                                (no commands)
        tip showing how to do the equivalent of Stata's macro
        substitution in Mata

Nick

On Tue, Jul 31, 2012 at 11:49 PM, Matthew McKay
<[email protected]> wrote:

> 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. //
>
>
*
*   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/


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