[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 |
st: RE: Re: local macro with if qualifier |

Date |
Tue, 7 Apr 2009 14:45:39 +0100 |

How to tackle problems like Sabrina's? Here are suggestions of tactics on several levels. Read help files very closely and carefully ========================================== -help local- indicates allowed syntaxes with -local-. If -if- (or -in-) were allowed, that would be explicit in the syntax diagram. It is not. Only rarely does Stata disguise its syntax and only for good reason! (It follows, in this case, that Martin's comment about -if- being evaluated for the first observation does not apply here. More on that in a moment.) Experiment with simple cases ============================ If you're unsure about what a definition implies, experiment with simple cases. Let Stata tell you. Here's an illustration: . sysuse auto (1978 Automobile Data) . local a = 1 . local a = `a' + 1 if mpg == 1 if not allowed r(101); In short, syntax like this is not allowed. (So, in relation to Martin's comment, whether it would do what you want is immaterial.) Now this is legal: . local a if mpg == 1 But what is the result? . di "`a'" if mpg == 1 What you typed is just copied as text, without being executed. So this strategy is unlikely to be what Sabrina wants. Illustrate your problem with mutually accessible data ===================================================== I just did what anybody can do, as datasets like the -auto- data are installed with Stata. The converse should be obvious: we don't have your data, which makes it harder to get to the position you are in. Don't say "it didn't work"; say what happened ============================================= I have an evolving private document detailing (so far) 19 different senses of "didn't work", collected from Statalist postings. Even Stata experts familiar with what you are doing often can't tell what you mean when you say "it didn't work". A report like that above makes it clearer. Don't treat Statalist as write-only =================================== Some members of Statalist post only when they have a problem and ignore it otherwise. That's understandable, especially as we are all "too busy", but it means that those members often miss relevant material. Only two hours before Sabrina's posting Ihsuan Li closed a thread in which the difference between if <condition> { <action> } and <action> if <condition> was clearly exposed. State the problem, as well as your failed solution =================================================== As Martin signalled wisely, Sabrina did not explain her problem. My instinct is that her code looks way more complicated than her problem is likely to require, but I too am too busy to give more time to trying to infer what the problem is. Don't get out of your depth =========================== If you are struggling with Stata basics, writing long complicated code is almost never going to work. Nick n.j.cox@durham.ac.uk Martin Weiss What exactly are those "d31b_`i'"? If they are variables, then the -if- qualifiers are evaluated for the first value only. This may be what you want, but experience shows that users are unaware of this more often than they actually want it. If you could give a description of your data and what you want to achieve, I am quite sure that you will soon get a much easier solution... Sabrina Carrossa > how can i use a local macro with the if qualifier option? > Unfortunately, this code doesn't work: > > local i=`i'+1 if d31b_`i'!=d31b_`y' & d31b_`i'==0 & `y'<3 > > Thank you in advance. > > The full code is: > > gen amico=0 > local i=1 > set trace on > forval y=2/3 { > replace amico=`i' if d31b_`i'!=d31b_`y' & d31b_`i'==1 & `y'==3 > replace amico=`i' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'>d31c_`y' & `y'==3 > replace amico=`i' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'>d31d_`y' & `y'==3 > replace amico=`i' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'==d31d_`y' & `y'==3 > replace debug=1 if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'==d31d_`y' & `y'==3 > > replace amico=`y' if d31b_`i'!=d31b_`y' & d31b_`i'==0 & `y'==3 > replace amico=`y' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'<d31c_`y' & `y'==3 > replace amico=`y' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'<d31d_`y' & `y'==3 > replace amico=`y' if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'==d31d_`y' & `y'==3 > > local i=`i'+1 if d31b_`i'!=d31b_`y' & d31b_`i'==0 & `y'<3 > local i=`i'+1 if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'<d31c_`y' & `y'<3 > local i=`i'+1 if d31b_`i'==d31b_`y' & d31b_`i'==1 & > d31c_`i'==d31c_`y' & d31d_`i'<d31d_`y' & `y'<3 > } > set trace off * * 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/

**Follow-Ups**:**Re: st: RE: Re: local macro with if qualifier***From:*Sabrina Carrossa <carrossa@gmail.com>

**References**:**st: local macro with if qualifier***From:*Sabrina Carrossa <carrossa@gmail.com>

**st: Re: local macro with if qualifier***From:*"Martin Weiss" <martin.weiss1@gmx.de>

- Prev by Date:
**Re: st: Question: postfile with variable number variables** - Next by Date:
**st: Randon Coefficient vs Mean Group Estimation** - Previous by thread:
**st: Re: local macro with if qualifier** - Next by thread:
**Re: st: RE: Re: local macro with if qualifier** - Index(es):

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