[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

From |
"Nick Cox" <n.j.cox@durham.ac.uk> |

To |
<statalist@hsphsun2.harvard.edu> |

Subject |
RE: st: Naming transformed variables in a program |

Date |
Wed, 14 May 2008 13:17:13 +0100 |

A refinement on this good advice is to put all the new names in one macro. foreach var of varlist `varlist' { tempvar temp gen `temp' = l.`var' local alltemp `alltemp' `temp' ... } Note that each invocation of -tempvar- creates a new temporary name. You just need to store each before the next time you di that. Nick n.j.cox@durham.ac.uk Maarten buis First of all, you can seperate the lefthand and righthand variables in one line using -gettoken-. Second, the answer to your actual question is -foreach-, which will allow you to loop over the variables. I have actually added to your example by also making these variables temporary. This has two advantages: 1) you will always have unique variables names, i.e. you will never try to generate a variables with a name that already exists in your data. 2) you don't have to clean up at the end of the program, the generated vriables will be removed as soon as the program ends. Usually, these are good things, sometimes not. In those cases just generate normal variables. program define myprogramme, eclass syntax [varlist] gettoken y x : varlist foreach var of varlist `varlist' { tempvar _l`var' gen `_l`var'' = l.`var' ... } end Hope this helps, Maarten --- Paulo Regis <pauloregis.ar@googlemail.com> wrote: > Dear All, > > I have some problems when I create variables to be introduced in a > user written programme. I introduce the problem using time lags > though > you may use time operators (l. and f.) to deal with my particular > example. However, it is much simpler to explain the problem in this > way so bare with me. In this example, I have > > program define myprogramme, eclass > syntax [varlist] > tokenize `varlist' > local yvar="`1'" > macro shift > local xvar="`*'" > ... > end > > -tokenize takes the first variable of the variable list and makes it > the dependent variable while the rest are the rigth-hand side > variables. Now, I want to create new variables which are a > transformation of the original list of variables. The simplest > example > is the first time lag. For the dependent variable is simple: > > generate _l`yvar'=l.`yvar' > > The problem is I couldnt find the way to make a similar > transformation > for the rigth-hand side variables. If `xvar' is a variable list with > only one variable (x1), there is a solution > > generate _l`xvar'=l.`xvar' > > However, this solution does not work if there are more than one > right-hand side variables. > > any idea about what I am missing? > * * For searches and help try: * http://www.stata.com/support/faqs/res/findit.html * http://www.stata.com/support/statalist/faq * http://www.ats.ucla.edu/stat/stata/

**References**:**st: Naming transformed variables in a program***From:*"Paulo Regis" <pauloregis.ar@googlemail.com>

**Re: st: Naming transformed variables in a program***From:*Maarten buis <maartenbuis@yahoo.co.uk>

- Prev by Date:
**st: minor problems with Statalist** - Next by Date:
**st: How to perform multivariate simulation with copula** - Previous by thread:
**Re: st: Naming transformed variables in a program** - Next by thread:
**[no subject]** - Index(es):

© Copyright 1996–2015 StataCorp LP | Terms of use | Privacy | Contact us | What's new | Site index |