Announcement

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

  • If NOT conditions with multiple strings

    Hey Folks

    I am writing a code for doing a ttest for a variable where in i am eliminating certain strings with != sign. For some reason Stata is not correctly picking up the if condition. Here is the code.

    I want to eliminate all the observations where either the job is not Information Specialist or Developer. I tried few codes but no luck

    ttest income if job!="Information Specialist" & job!="Developer", by(firms)-- this is not correct as it checks both conditions are true (1 1)

    ttest income if (job!="Information Specialist"| job!="Developer"), by(firms)

    ttest income if (job!="Information Specialist"| job!="Developer"), by(firms)

    Let me know how i can resolve this issue

    Thanks
    Veeresh

  • #2
    Your first statement looks right. So my question is why you think Stata is not doing what you want. A simple but important detail is that Stata will check for exact inequality, so small difference of capitalisation, spacing, spelling and punctuation could all be biting. Also, be careful that your variable really is string and not a numeric variable with value labels.

    Comment


    • #3
      I want to eliminate all the observations where either the job is not Information Specialist or Developer [emphasis added]
      I'm not sure I understand what you want to do, Vereesh. Does this mean you want to restrict your t-test to those observations where the job is either "Information Specialist" or "Developer"? If that's what you want, it's:

      Code:
      ttest income if inlist(job, "Information Specialist", "Developer"), by(firms)

      Comment


      • #4
        Thanks Nick. You were spot on about that. I reformatted the variables and now it is showing the exact number of observations i was expecting.

        Comment


        • #5
          Clyde I was trying to restrict observations where the jobs are not equal to. Quick question, i know i can use inlist for a range of values. Can you use inlist in an opposite effect? (something like outlist)

          Comment


          • #6
            If you want a range of values, use -inrange()- instead of -inlist()-. If you want "outlist,", just use -!inlist()-

            Code:
            sysuse auto
            list if inlist(rep78, 2, 5)
            list if inrange(rep78, 2, 5)
            list if !inlist(rep78, 2, 5)

            Comment


            • #7
              Dear Clyde and Nick,

              I just read this post and I am really excited about the possibility to use an outlist.

              However, I immediately tried to use this function, but it doesn´t seem to work.

              I am using Stata 14.1 SE, and the following code was supposed to be shortened:

              * Original Syntax (works)
              bys persnr: replace check = 2 if married == 2 & married[_n+1] != 6 & married[_n+1] != . & married[_n+1] != 3 & married[_n+1] != 4 & married[_n+1] != 5 & married[_n+1] != 9

              * Optimized Syntax (doesn´t work)
              bys persnr: replace check = 2 if married == 2 & !inlist(married[_n+1],6,.,3,4,5,9)

              I would be really thankful for a solution or any other equivalent option to shorten these commands!

              Best regards,
              Sascha

              Comment


              • #8
                Sascha-Christopher: Example data please.

                Comment


                • #9
                  In addition to example data that produces the problem with that code, you should also explain what you mean by "doesn't work." Do you mean that you get an error message? Or do you get results that differ from what you want? If so, what results did you get and in what way do they differ from what you want?

                  Comment

                  Working...
                  X