Announcement

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

  • If qualifier for n variables equal to zero

    Dear Stata community, dear Nick
    I am looking for a command which allows me to create an if-statement which refers to about 40 variables.
    I have got yearly bilateral FDI data for 40 different classes of foreign direct investment.
    I want to delete an observation if a country reports only zeros on all FDI classes.

    Thus, something similar to :
    Code:
    drop if var1-var40=0
    I already tried that line but it did drop less observations than using:
    Code:
    drop if var1 =0 & var2 = 0 ... & var40 = 0
    You can see my data below where the ID* abbreviations are representing different classes of FDI (var1-var40):

    input str36 name str38 cpname int timeperiod double(IDA_G_DV IDA_G IDL_G IDL_G_DV)
    "AfghanistanIslamic Republic of" "Germany" 2016 0 0 0 0
    "AfghanistanIslamic Republic of" "Germany" 2017 0 0 0 0
    "AfghanistanIslamic Republic of" "Italy" 2009 0 0 0 0
    "AfghanistanIslamic Republic of" "Italy" 2010 0 0 0 421572.76040272
    "AfghanistanIslamic Republic of" "Italy" 2011 0 0 0 1801631.46384125
    "AfghanistanIslamic Republic of" "Italy" 2012 0 0 0 0
    "AfghanistanIslamic Republic of" "Italy" 2013 0 0 0 0

    Thank you very much for your time.
    I am using Stata 14.2

  • #2
    why not use one of the -egen- functions to create a new variable that has the condition you want; then use that in your drop statement and then, if you want, drop the new variable; here are two examples:
    Code:
    even wanted=rowtotal(var1-var40)
    even wanted=rowmax(var1-var40)
    the above examples assume that none of var1-var40 can be negative and also that they are next to each other in your data set; replace var1-var40 with your varnames Iif all start "ID" and if no other variables start with ID, then you can use "ID*" in the parens above; if wanted is then equal to 0 you have the condition you want and can just use "if wanted==0" in your command

    Comment


    • #3
      I agree entirely with the recommendation in post #2 from Rich Goldstein .

      To help you better understand Stata I'll mention where you went wrong in your code in post #1.

      First, to compare two values, the operator is "==" not "=", so you should have written
      Code:
      drop if var1 == 0 & var2 == 0 ... & var40 == 0
      And even if you make that correction to
      Code:
      drop if var1-var40=0
      it still will not work, because
      Code:
      var1-var40==0
      is not recognized by Stata as a shortcut for "all of the variables in the list are equal to zero" but rather as "(var1 minus var40) is equal to zero".

      Comment


      • #4
        Thanks for the quick and simple answer Rich and William.

        Comment

        Working...
        X