Statalist


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

Re: st: Naming transformed variables in a program


From   Maarten buis <[email protected]>
To   [email protected]
Subject   Re: st: Naming transformed variables in a program
Date   Wed, 14 May 2008 10:55:48 +0100 (BST)

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 <[email protected]> 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?
> 
> Cheers
> 
> Paulo
> *
> *   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/
> 


-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands

visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434

+31 20 5986715

http://home.fsw.vu.nl/m.buis/
-----------------------------------------


      __________________________________________________________
Sent from Yahoo! Mail.
A Smarter Email http://uk.docs.yahoo.com/nowyoucan.html
*
*   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/



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