Statalist


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

Re: st: if and if


From   "Ashim Kapoor" <ashimkapoor@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: if and if
Date   Thu, 13 Nov 2008 15:24:52 +0530

Hello Svend,

I have a question. Where did you read that the command

if j==1

will be testing the FIRST observation. I don't think that it is in the
stata help for if. Could you point me in the right direction here.


Also I liked your use of the local in that way. I did not think of
that . Did you read that somewhere too ?

Thank you,
Ashim.

On Thu, Nov 13, 2008 at 2:10 PM, Svend Juul <SJ@soci.au.dk> wrote:
>
> 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
> __________________________________________
>
> 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
> Email:  sj@soci.au.dk
> __________________________________________
>
> *
> *   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/



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