Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
From | Nick Cox <n.j.cox@durham.ac.uk> |
To | "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu> |
Subject | RE: st: RE: scalar do not allow -if- |
Date | Wed, 17 Nov 2010 20:39:22 +0000 |
Yes and no; the code is good, but the commentary could be misleading. You can certainly use -cond()- to define these scalars, as others have already pointed out in this thread. But its role is not analogous to that of the -if- qualifier. -if- qualifiers apply conditionally to a subset of observations (which may be empty or the whole set), but no such notion applies to scalars. Nick n.j.cox@durham.ac.uk Sergiy Radyakin the scalar version of -if- is cond(): scalar hig = cond(w2>w1,cond(w3>w2,3,2),cond(w3>w1,3,1)) although if in your example you had w1,w2,...w100, you wouldn't want to write anything like the above. you would then program one -forvalues- loop and one -if- branch that updates the index of the max element. On Wed, Nov 17, 2010 at 11:11 AM, Nick Cox <n.j.cox@durham.ac.uk> wrote: > A command > > ... if <condition> > > applies observation by observation and can not be what you want. > > I guess that you want > > if (w1 > w2) & (w1 > w3) { > scalar hig = 1 > } > else if (w2 > w1) & (w2 > w3) { > scalar hig = 2 > } > else if (w3 > w1) & (w3 > w2) { > scalar hig = 3 > } > > These sound like "w1 biggest", "w2 biggest", "w3 biggest". If there are any ties w1 == w2 | w2 == w3 your scalar will be undefined. (I've corrected what looks like a typo in your code, but not this problem with ties, which is perhaps unlikely.) Tomas Lind > In a small simulation I generate data and analyze them with 3 regression methods. In the bottom of the loop I save (among other things) > > scalar b2 = exp(_b[x]) // beta > > and then put the -scalar- values into a variable in a dataset where I collect the results from the 1000 simulated datasets. For example > > quietly replace b2=scalar(b1) in `i' > > > That works fine. Now I want to calculate "hig" (in the bottom of the loop) and save it in the dataset with aggregated data (where w1, w2, w3 are -scalars-). > > scalar hig = 1 if (w1 > w2) & (w1 > w3) > scalar hig = 2 if (w2 > w1) & (w2 > w3) > scalar hig = 3 if (w3 > w1) & (w3 > w1) > > However, -if- is not allowed in combination with -scalar-. > > > * > * 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/ > * * 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/ * * 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/