Referencing individual elements of (local) macros

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Referencing individual elements of (local) macros

ranjit b
Dear Statalisters,

I am wondering whether it is possible to reference the individual
elements of a (local) macro by a number. For example, if I have the
local macro x "var1 var2 var3". How would something like reg y `x'[3]
work.

Long explanation:

I would like to estimate a number of models with different dependent
and independent variables. The control variable varies with the
dependent variable. I would like to use some kind of loop structure
(since I want to do a lot of estimations). so, I have defined two
macros. Lets say that they each have three elements.

local depvars " depvar1 depvar2 depvar3"
local controlvars " controlvar1 controlvar2 controlvar3"

I would like to do something like that

i=1
foreach c of local depvar{
reg `c' `controlvars'[i]
i++
}

Thanks for your help!


R.
*
*   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/
Reply | Threaded
Open this post in threaded view
|

Re: Referencing individual elements of (local) macros

Sergiy Radyakin
Dear Miros Lav,

I salut your intent to use arrays for that, indeed you can do it with
arrays. Many statalisters
prefer variables lists instead. Given that you want only one var from
each list, just extract it
as a word by its index. See below.

If you are doing any data mining/optimal model search, investigate
further, e.g. see -bestreg-
by Zurab Sajaia:

net from http://adeptanalytics.org/download/ado/

Best,
    Sergiy Radyakin





sysuse auto

local depvars "price weight length"
local controlvars "rep78 foreign mpg"

local n_models : word count `depvars'
assert `n_models'==`:word count `controlvars''  // this ensures that
the two lists are of the same length

forval i=1/`n_models' {
        local this_depvar `: word `i' of `depvars''
        local this_controlvar `:word `i' of `controlvars''
        regress `this_depvar' `this_controlvar'
}





On Thu, Dec 2, 2010 at 4:03 PM, miros lav
<[hidden email]> wrote:

> Dear Statalisters,
>
> I am wondering whether it is possible to reference the individual
> elements of a (local) macro by a number. For example, if I have the
> local macro x "var1 var2 var3". How would something like reg y `x'[3]
> work.
>
> Long explanation:
>
> I would like to estimate a number of models with different dependent
> and independent variables. The control variable varies with the
> dependent variable. I would like to use some kind of loop structure
> (since I want to do a lot of estimations). so, I have defined two
> macros. Lets say that they each have three elements.
>
> local depvars " depvar1 depvar2 depvar3"
> local controlvars " controlvar1 controlvar2 controlvar3"
>
> I would like to do something like that
>
> i=1
> foreach c of local depvar{
> reg `c' `controlvars'[i]
> i++
> }
>
> Thanks for your help!
>
>
> R.
> *
> *   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/
>

*
*   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/
Reply | Threaded
Open this post in threaded view
|

RE: Referencing individual elements of (local) macros

Nick Cox
Also check out -tokenize- and a discussion in

SJ-3-2  pr0009  . . . . . . . . . . . . . Speaking Stata:  Problems with lists
        . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
        Q2/03   SJ 3(2):185--202                                 (no commands)
        discusses ways of working through lists held in macros

Nick
[hidden email]

Sergiy Radyakin

I salute your intent to use arrays for that: indeed you can do it with
arrays. Many statalisters
prefer variable lists instead. Given that you want only one var from
each list, just extract it
as a word by its index. See below.

If you are doing any data mining/optimal model search, investigate
further, e.g. see -bestreg-
by Zurab Sajaia:

net from http://adeptanalytics.org/download/ado/

sysuse auto

local depvars "price weight length"
local controlvars "rep78 foreign mpg"

local n_models : word count `depvars'
assert `n_models'==`:word count `controlvars''  // this ensures that
the two lists are of the same length

forval i=1/`n_models' {
        local this_depvar `: word `i' of `depvars''
        local this_controlvar `:word `i' of `controlvars''
        regress `this_depvar' `this_controlvar'
}


On Thu, Dec 2, 2010 at 4:03 PM, miros lav

> I am wondering whether it is possible to reference the individual
> elements of a (local) macro by a number. For example, if I have the
> local macro x "var1 var2 var3". How would something like reg y `x'[3]
> work.
>
> Long explanation:
>
> I would like to estimate a number of models with different dependent
> and independent variables. The control variable varies with the
> dependent variable. I would like to use some kind of loop structure
> (since I want to do a lot of estimations). so, I have defined two
> macros. Lets say that they each have three elements.
>
> local depvars " depvar1 depvar2 depvar3"
> local controlvars " controlvar1 controlvar2 controlvar3"
>
> I would like to do something like that
>
> i=1
> foreach c of local depvar{
> reg `c' `controlvars'[i]
> i++
> }
>
> Thanks for your help!

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