Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: RE: tokenize `varlist' error?


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: tokenize `varlist' error?
Date   Fri, 23 Apr 2004 16:55:56 +0100

-tokenize- doesn't understand 
the varlist idea. It just splits text 
that it is fed according to white space
(in this case), or specified parsing characters, 
subject to binding by double quotes or 
compound double quotes. 

One way of proceeding in your case is 
a prior -unab- (I am ignoring one 
stray "_" as a possible typo) 

unab varlist : `varlist' 
tokenize `varlist'
while "`1'" ~= "" {
	generate a1997`1'=0
	replace  a1997`1'=1 if `1'==1997
	generate a1998`1'=0
	replace  a1998`1'=1 if `1'==1998
	macro shift
}

But you can avoid all this by  
using -foreach- to cycle through your 
varlist. 

local varlist p26_1_1-p26_1_3
foreach v of var `varlist' {
	generate a1997`v'=0
	replace  a1997`v'=1 if `v'==1997
	generate a1998`v'=0
	replace  a1998`v'=1 if `v'==1998
}

The local is redundant: 

foreach v of var p26_1_1-p26_1_3 {
	generate a1997`v'=0
	replace  a1997`v'=1 if `v'==1997
	generate a1998`v'=0
	replace  a1998`v'=1 if `v'==1998
}

and the inside can be condensed 

foreach v of var p26_1_1-p26_1_3 {
	generate a1997`v' = `v' == 1997
	generate a1998`v' = `v' == 1998
}

For more background, see the paper "How 
to face lists with fortitude" in SJ 2(2) 2002 
(or Google to get internet copies of earlier
version). 

Nick
[email protected] 

Adrian Gonzalez-Gonzalez
 
I want to perform a simple procedure over 100 variables and I don't want to enter the name of each variable one by one.
If I use the first definition of varlist (below), my little procedure works fine.  But if I used the second definition of varlist, using "-" I get an error (procedure below).
 
How can I solve this problem?  I am sure this is very simple for most of you, but I am new trying to do these things with Stata.
My version is 7.0 updated.
 
Thanks a lot
 
Adrian
 
 
local varlist p26_1_1 p26_1_2 p26_1_3
 
local varlist p26_1_1-p26_1_3
 
tokenize `varlist'
 
 while "`1'" ~= "" {
  2. generate a1997`1'_=0
  3. replace  a1997`1'=1 if `1'==1997
  4. generate a1998`1'=0
  5. replace  a1998`1'=1 if `1'==1998
  6. macro shift
  7. }
invalid syntax
r(198);
 
end of do-file
r(198);

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