Thanks Nick, Scott and Maarten,

Many thanks for helpful code and helpful comments and explanations.

/Tomas

-----Ursprungligt meddelande-----

Från:

[hidden email] [mailto:

[hidden email]] För Maarten buis

Skickat: den 17 november 2010 17:17

Till:

[hidden email]
Ämne: Re: st: scalar do not allow -if-

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

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