Statalist The Stata Listserver


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

st: RE: RE: Funky local macro result


From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   st: RE: RE: Funky local macro result
Date   Fri, 18 Aug 2006 17:49:19 +0100

What you are trying to do is possible, 
I think, as shown by this toy 

program dferry 
           version 9
	   	
           sysuse auto, clear
           count
		
           if r(N)>50 {
                   local lagif "foreach l in 2 4"
           }
           else local lagif "forval l = 1(1)4"

	     `lagif' { 
                   di `l'
           } 
end 

or a similar one with semi-colons as delimiters. 

But I'd pontificate that it is not especially good
style. In essence, you have to branch any way, 
so what you want to do could well be set out more 
transparently. So, I agree with Scott. 

Here is a version. I don't understand all of what
you want, but I'd guess you want 

L`l'. 

rather than

L`l'

Note that repeated -tokenize-s are not needed 
in the last section. You can just loop over the 
contents of `restx'. 

program get_yx
	version 9
	args y r
	use time region using "$remach/data/regData" ///
		if time>=q(1998-1) & region==`r', clear
	count
	local small = r(N) < 900 
	
	use using "$remach/pooledr/reg_unitroot" if region==`r', clear
	
	levelsof diffvar if var=="`y'", local(y) c
	levelsof diffvar if xy=="x", local(X) c
	
	global y "`y'"
	global X 
	
	tokenize `X'
	local firstx `1'
	mac shift
	local restx `*'

	if `small' { 
		foreach l in 2 4 {
			global X "$X L`l'`firstx'"
		}
	else { 
		forval l = 1/4 { 
			global X "$X L`l'`firstx'"
		}
	}

	foreach v in `restx' {
		foreach l in 2 4 {
			global X "$X L`l'`v'"
		}
	}
	
end


Nick 
[email protected] 

Scott Merryman
 
> I am not sure why Stata doesn't recognize the final closing 
> bracket, but a
> simpler example returns:
> 
> . sysuse auto,clear
> (1978 Automobile Data)
> 
> . local lagif "foreach l in 2 4"
> 
> . `lagif' {
> foreach command may not result from a macro expansion 
> interactively or in do
> files
> r(198);
> 
> 
> Since -foreach- cannot result from a macro expansion one way 
> around this
> would be to wrap the -foreach- and -forv- in -if- statements.  
> 
> For example:
> 
> count 
> if r(N) > 900 {
> 	local lagif  "l in 2 4"
> 	local tag =1 
> }
> else {
> 	local lagif "l = 1(1)4"
> 	local tag =2
> 	}
> 
> if `tag' == 1 {
> 	foreach  `lagif' {
> 	//global X...
> 	}
> }
> 
> if `tag' == 2 {
> 	forv `lagif' {
> 	//global X...
> 	}
> }

dferry

> > I've pasted in a program I wrote, below. As shown, it works 
> just fine.
> > However, if I change the lines that say:
> > foreach l in 2 4 {;
> > 
> > to:
> > `lagif' {;
> > 
> > then, I get an error in which Stata doesn't recognize the 
> final closing
> > bracket in the program. I.e., I get the following error msg:
> > unrecognized command:  } invalid command name
> > r(199);
> > 

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