Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: scalar do not allow -if-
From 
 
Maarten buis <[email protected]> 
To 
 
[email protected] 
Subject 
 
Re: st: scalar do not allow -if- 
Date 
 
Wed, 17 Nov 2010 16:17:26 +0000 (GMT) 
--- On Wed, 17/11/10, Tomas Lind wrote:
> 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-.
-if- after a command is supposed to work on the dataset, 
i.e. the result is a vector of trues and falses, one for
each observation in the dataset. This is why -scalar-
cannot handle it: it needs to be fed one number.
-if- on its own or before a command is supposed to work 
on its own and will result in a scalar outcome either 
true or false.
So you could get your scalar -hig- by:
if (w1 > w2) & (w1 > w3)  scalar hig = 1           
if (w2 > w1) & (w2 > w3)  scalar hig = 2  
if (w3 > w1) & (w3 > w1)  scalar hig = 3 
 
or: 
if (w1 > w2) & (w1 > w3) {
    scalar hig = 1           
}
if (w2 > w1) & (w2 > w3) {
    scalar hig = 2  
}
if (w3 > w1) & (w3 > w1) {
    scalar hig = 3 
}
or:
scalar hig = cond( (w1 > w2) & (w1 > w3), 1,  ///
             cond( (w2 > w1) & (w2 > w3), 2,  ///
             cond( (w3 > w1) & (w3 > w1), 3, .)))
Hope this helps,
Maarten
--------------------------
Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
Germany
http://www.maartenbuis.nl
--------------------------
      
*
*   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/