Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • IF NOT statements in Stata

    Hi all,

    I've been reviewing the documentation on Stata to try and figure out how to generate IF NOT statements. Essentially, I have a load of string values for one variable and I want to recode these so that all except a particular value are coded into a 1, and the other value into a 0. I've been using the following code without success, can someone point out where I'm going wrong?

    gen newvariable=.
    replace newvariable=1 if oldvariable~="N/A"
    replace newvariable=0 if oldvariable=="N/A"

    Thanks,

    Calum

  • #2
    I don't see an error in your code. What made you think that your code gave you wrong results?

    If we try this in a example dataset we all have access to, you can see that it works.

    Code:
    sysuse auto, clear
    
    // your method
    gen foo1 = .
    replace foo1 = 1 if make == "Subaru"
    replace foo1 = 0 if make ~= "Subaru"
    
    // shorter version
    gen byte foo2 = ( make == "Subaru" )
    
    // the result is the same
    tab foo1 foo2
    There is also a shorter version, that uses the fact that for Stata a "true" for a logical statement is 1 and a "false" is 0.
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Like Maarten, I don't see any reason why your code shouldn't work.

      Comment


      • #4
        What type of variables are you dealing with, numeric or string?
        If your variables are string like the example above you can first encode it and then use the mvencode command to deal with the missing values.
        you might also find the following example useful.
        Code:
        clear
        input str8 var
        "1"
        "2"
        ""
        "N/A"
        "D/K"
        "R"
        end
        g newvar=cond(var=="",.,cond(inlist(var,"N/A","D/K"),0,1))
        
        .    l,    sep(0)
        
                +--------------+
                var   newvar 
                --------------
            1.    1        1 
            2.    2        1 
            3.    . 
            4.    N/A        0 
            5.    D/K        0 
            6.    R        1 
                +--------------+

        Comment


        • #5
          Hi all,

          Thanks for the quick replies. I stupidly mis-typed in Stata (but not on here, oddly) so it's working now. I'm having some trouble with blank values though. Using the below code, blanks don't seem to be re-coded, but remain as '.' when I run codebook on that variable

          replace newvariable=0 if oldvariable=="."

          Comment


          • #6
            In strings, blank can sometimes be just that - blank.
            try
            Code:
               replace newvariable=0 if oldvariable==" "
             

            Comment


            • #7
              Hi Ariel,

              Thanks for your reply. Unfortunately, that still doesn't work. Prior to running it, I had the following data:
              • 281 with a value of '0'
              • 151 with a value of '1'
              • 72 with a value of '.'
              I'm looking for the 72 '.' to move into the '0' set but this doesn't seem to be happening. Any other ideas?

              Thanks,

              Calum

              Comment


              • #8
                Maybe it's not "." but ". " or " ." or " . " .... the possiblities are quite endless. try maybe trimming oldvar before coding newvar.

                Comment


                • #9
                  As advised by Maarten,

                  Code:
                  gen newvariable = oldvariable=="N/A"
                  should map matches to 1 and all others to 0.

                  Following Ariel's pointer

                  Code:
                  ... if inlist(trim(oldvariable), "", ".")
                  should catch "" "." and any spaces surrounding.

                  Comment

                  Working...
                  X