Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

RE: st: RE: if command

From   "Nick Cox" <[email protected]>
To   <[email protected]>
Subject   RE: st: RE: if command
Date   Tue, 18 Jan 2005 19:19:39 -0000

I think you were perfectly clear, 
but this question appears the same 
to me, and so the answer is the same. 

You can go in Stata 

if x == 10 { 
	gen y = 12 
	gen q = 78 

and this is legal, but it is not what 
you want, I think. This code is to Stata 

if x[1] == 10 { 
	gen y = 12 in 1/l 
	gen q = 78 in 1/l 

which is rarely what anybody wants. 

Fortran is no different here from Stata 
(unless in this respect it has changed since
I learnt it 30 years ago), as the apparent 

    Y = 12
    Q = 78

(.EQ. <--> ==)

applies to single values stored
in X, Y, Q, not to one-dimensional
arrays (Fortran's equivalent of 
Stata's variables) -- unless
there are dialects of Fortran 
in which you can do this to arrays. 

I agree that one would occasionally 
like to say something like 

whenever x == 10 { 
	gen y = 12 
	gen q = 78 

(thus leaving -y- and -q- 
missing whenever x != 10), 
the -whenever- being vectorised 
in application, but Stata doesn't have this. 
Also, there is considerable 
repetition of work in 

gen y = 12 if x == 10 
gen q = 78 if x == 10 

but that's the language as it is now. 

I think your question is really for StataCorp 
but they will (usually) maintain cryptic silence 
on what language features may or may not be added 

To put it in a nutshell: 

With -if { }- just one test is made. If 
variables appear in the test, that makes
no difference. The test is applied to 
the _first_ observation. 

With ... if ... as many tests are made
as there are observations. (Sometimes
that's far more than you want, but
that's another story.) 

[email protected] 

Cameron Hooper
> Nick Cox wrote:
> > -if- as a keyword is overloaded. The 
> > two ways of using -if- you exemplify
> > are not equivalent -- unless you have
> > single observation datasets.
> I have not been clear. I apologise. I completely agree that 
> code should 
> be readable.
> Let me explain. I am not looking for a way to exploit some 
> unique aspect 
> of my data to make my code more efficient. So for example,
> gen var1 = (fpi == "big") + 2 * (fpi == "small")
> works, but only for these particular values.
> I am asking a more general question. In stata can I test a 
> condition and 
> as a result of this single test generate (or replace) more than one 
> variable? All of Nick's examples generated one variable as 
> the result of 
> testing one logical expression. In Fortran I can write:
> IF (X.GE.10) THEN
>    Y = 12
>    Q = 78
> Test once, set two variables. The only way I know to do this 
> in stata is
> gen y = 12 if x == 10
> gen q = 78 if x == 10
> I have to test x twice. Is there a way in stata to only test the 
> condition once? I understand that "if exp {}" will not work.

*   For searches and help try:

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