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

Re: st: if and if

From   Svend Juul <SJ@SOCI.AU.DK>
To   <>
Subject   Re: st: if and if
Date   Thu, 13 Nov 2008 09:40:44 +0100

Ashim wrote:

I realize that there are 2 kinds if's in Stata.

Type 1 : would be something like replace j = 2 if k==2
here the replace in j would happen ONLY in the corresponding
observation of k. THIS IS WHAT I WANT.

Type 2 : the programming if something like

local j

if `j'==2 {

do something.



I guess I want to do something which is in between the above 2.

I want to say the following : --

replace j=2 if k==2
replace m=2 if k==2
replace n=2 if k==2

in ONE shot.

so I try : -
************************************** Block A
if k==2 {
replace j=2
replace m=2
replace n=2
This does not work. Because k==2 would mean k==2 in ALL observations.


Actually the command -if k==2- means k==2 in the FIRST observation.
In Stata terms, your type 1 -if- is a qualifier; your type 2 -if- is 
a command.

There is probably no smart way to do what you want. In some cases 
you might benefit from putting the condition in a macro, like:

    sysuse auto.dta , clear
    local X  "if foreign==1"
    replace price = 1 `X'
    replace weight = 2 `X'

Hope this helps

Svend Juul
Institut for Folkesundhed, Afdeling for Epidemiologi
(Institute of Public Health, Department of Epidemiology)
Vennelyst Boulevard 6
DK-8000  Aarhus C, Denmark
Phone:  +45 8942 6090
Home:   +45 8693 7796

*   For searches and help try:

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