[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

From |
Sabrina Carrossa <carrossa@gmail.com> |

To |
statalist@hsphsun2.harvard.edu |

Subject |
Re: st: RE: Re: local macro with if qualifier |

Date |
Wed, 8 Apr 2009 17:47:23 +0200 |

Dear all, i am really sorry for my question. It was generic and it was not about syntax-problem Basically, I did a logic error. As prof. Pisati wrote me – in a private conversation – I can’t assign to a scalar a specific value in respect of the values of a variables (it is impossible). He suggest me to find a new algorithm, and I did it. Here the solution. My object is to find the best friend among a list of friends. I have a list of “i” occupation (in the db they are 20, in the example just 3). For each occupation in the db I have the variables: - d31b_i : do you have a friend that have the occupation “i”? (yes/no) - d31c_i: how many time do you spend with this person in the occupation “i”? (0/6, with 6 a lot of time) - d31d_i: how do you feel close with this person in the occupation “i”? (0/10, with 10 really close) Than, my idea is that the best friend is a friend with: - d31b_i=1 - d31c_i= max (among each i) - d31d_i=max (among each i) And, if I have more friends with the same value for these three vars, the best friend is the friend with higher occupation (max i) ************************************************ * Stata Code: ************************************************ * compute n vars as sum of the information on d31b d31c d31d and occupation (i) forval i=1/3 { gen d31_`i'=0 replace d31_`i'= (d31b_`i'*100000) + (d31c_`i'*10000) + (d31d_`i'*100) + `i' } * in the first step i assign to friend the value of the friend in the first occupation (i=1) * than i compare the value of this first friend with the values of the other friends * if the values of the other friends are highest (more contact and more closeness) I replace the value of friend gen friend=d31_1 gen f=1 forval y=2/3 { replace f=`y' if confront<d31_`y' replace friend= d31_`y' if confront<d31_`y' } I really hope that you can apologize me. Sincerely, Sabrina. 2009/4/7 Nick Cox <n.j.cox@durham.ac.uk>: > 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/ > -- sabrina * * 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:*"Nick Cox" <n.j.cox@durham.ac.uk>

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

**st: RE: Re: local macro with if qualifier***From:*"Nick Cox" <n.j.cox@durham.ac.uk>

- Prev by Date:
**AW: st: AW: How to add the same block of data to every individual** - Next by Date:
**RE: st: RE: Re: local macro with if qualifier** - Previous by thread:
**st: RE: Re: local macro with if qualifier** - Next by thread:
**RE: st: RE: Re: local macro with if qualifier** - Index(es):

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