Announcement

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

  • How to use "if not" condition in regression

    Hi all,

    I read the syntax of regress and operators but still cannot find how to run a regression with a condition that: variable "INDC3" not equal to 'NA', 'UNCLS', 'UQEQS')
    My original code below works well
    Code:
    areg wINV_DAY pt wFIRM_SIZE LNGDP UNEMPLOYMENT  INFLATION wTANGIBILITY  i.yr , a(TYPE2)
    My code below caused error:
    Code:
    areg wINV_DAY pt wFIRM_SIZE LNGDP UNEMPLOYMENT  INFLATION wTANGIBILITY  i.yr if (INDC3 ~=('NA', 'UNCLS', 'UQEQS')) , a(TYPE2)
    The error is:
    Code:
    'NA','UNCLS','UQEQS' invalid name
    r(198);
    When I changed from "~=" TO "!=" , the same error popping up.
    Can you please help me to adjust the code to run the regression where INDC3 not equal to "NA", "UNCLS" or "UQEQS"?


    Update:
    I changed from single quotation to double quotation (
    HTML Code:
    if (INDC3 ~=("NA", "UNCLS", "UQEQS"))
    )and it works, but I am not sure whether it is a proper fix?
    Last edited by Phuc Nguyen; 12 Apr 2021, 17:35.

  • #2
    if (INDC3 ~=("NA", "UNCLS", "UQEQS"))
    is not legal Stata syntax. I'm surprised it does not give you an error message. In any case, after some experimentation, as best I can tell, it is not doing what you want it to: it is only testing whether INDC3 is unequal to "NA" and ignoring "UNCLS" and "UQEQS"

    In any case, the solution to your problem is via the -inlist()- function. See -help inlist()- for details of how to use it.

    Comment


    • #3
      Thank you Clyde Schechter
      After a while with inlist(), I found the code below can be used:
      Code:
      !inlist(INDC3,"NA", "UNCLS", "UQEQS")
      Warm regards.

      Comment


      • #4
        What you figured out is the best way to go:

        Code:
        . sysuse auto, clear
        (1978 Automobile Data)
        
        . keep make
        
        . keep in 1/4
        (70 observations deleted)
        
        . list if !inlist(make, "AMC Concord" , "AMC Pacer" )
        
             +---------------+
             | make          |
             |---------------|
          3. | AMC Spirit    |
          4. | Buick Century |
             +---------------+
        An alternative and not so best is the following:

        Code:
        . list if make!="AMC Concord" & make!= "AMC Pacer"
        
             +---------------+
             | make          |
             |---------------|
          3. | AMC Spirit    |
          4. | Buick Century |
             +---------------+
        And as Clyde said, it is a mystery what Stata does when you try your original logical but invalid syntax:

        Code:
        . list if make != ("AMC Concord", "AMC Pacer")
        
             +---------------+
             | make          |
             |---------------|
          2. | AMC Pacer     |
          3. | AMC Spirit    |
          4. | Buick Century |
             +---------------+
        
        . dis ("AMC Concord", "AMC Pacer")
        AMC Concord
        
        . list if make != ("AMC Concord" | "AMC Pacer")
        type mismatch
        r(109);
        Originally posted by Phuc Nguyen View Post
        Thank you Clyde Schechter
        After a while with inlist(), I found the code below can be used:
        Code:
        !inlist(INDC3,"NA", "UNCLS", "UQEQS")
        Warm regards.
        Last edited by Joro Kolev; 13 Apr 2021, 01:03.

        Comment

        Working...
        X