Announcement

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

  • Creating a variable with three condtions

    I am using a data set of 50 countries which are separated into four regions.
    I want to create a variable of regional contagion, where recgontagion for country j takes one if and only if there is a crisis in at least one country other than country j and this country is in the same region as country j.

    I have been the below command but it doesn't work:

    replace regcontagion=1 if (evnt3w==1&country!=country &region_id==region_id)













  • #2
    country!=country is never true
    ---------------------------------
    Maarten L. Buis
    University of Konstanz
    Department of history and sociology
    box 40
    78457 Konstanz
    Germany
    http://www.maartenbuis.nl
    ---------------------------------

    Comment


    • #3
      Which command should I use then?

      Comment


      • #4
        rangestat from SSC should help. Syntax would be something like

        Code:
        rangestat (max) evnt3w, by(region) int(country . .) excludeself
        noting that

        1. You must install
        rangestat first

        2. For this to work
        country must be numeric. You don’t give a data example sufficient for us to be clear (please do read and act on FAQ Advice #12) but at worst the variable is string and you need to do something like

        Code:
        encode country, gen(Country)
        and use the new variable instead.

        Comment


        • #5
          clear
          input long country float id int year float(evnt3w region_id regcontagion)
          3 3 1980 . 3 0
          3 3 1981 . 3 0
          3 3 1982 1 3 0
          3 3 1983 . 3 0
          3 3 1984 . 3 0
          3 3 1985 . 3 0
          3 3 1986 0 3 0
          3 3 1987 0 3 0
          3 3 1988 0 3 0
          3 3 1989 0 3 0
          3 3 1990 0 3 0
          8 8 1980 . 3 0
          8 8 1981 . 3 0
          8 8 1982 1 3 0
          8 8 1983 . 3 0
          8 8 1984 . 3 0
          8 8 1985 . 3 0
          8 8 1986 . 3 0
          8 8 1987 . 3 0
          8 8 1988 0 3 0
          8 8 1989 0 3 0
          8 8 1990 0 3 0
          11 11 1980 . 3 0
          11 11 1981 1 3 0
          11 11 1982 . 3 0
          11 11 1983 . 3 0
          11 11 1984 . 3 0
          11 11 1985 0 3 0
          11 11 1986 0 3 0
          11 11 1987 0 3 0
          11 11 1988 0 3 0
          11 11 1989 0 3 0
          11 11 1990 0 3 0
          21 21 1980 0 4 0
          21 21 1981 0 4 0
          21 21 1982 0 4 0
          21 21 1983 . 4 0
          21 21 1984 . 4 0
          21 21 1985 . 4 0
          21 21 1986 1 4 0
          21 21 1987 . 4 0
          21 21 1988 . 4 0
          21 21 1989 . 4 0
          21 21 1990 0 4 0
          33 33 1980 0 4 0
          33 33 1981 0 4 0
          33 33 1982 0 4 0
          33 33 1983 . 4 0
          33 33 1984 . 4 0
          33 33 1985 . 4 0
          33 33 1986 1 4 0
          33 33 1987 . 4 0
          33 33 1988 . 4 0
          33 33 1989 . 4 0
          33 33 1990 . 4 0
          end
          format %ty year
          label values country country
          label def country 3 "Bolivia", modify
          label def country 8 "Chile", modify
          label def country 11 "Costa Rica", modify
          label def country 21 "Guinea", modify
          label def country 33 "Nigeria", modify
          [/CODE]

          Comment


          • #6
            So, thanks for the extra detail. Evidently, country is numeric and the first line of code is still what is recommended.

            Comment


            • #7
              Sorry Nick, but I still don't get how this command works. I already have the variable
              regcontagion which has just zeros. So, how should I replace the values with 1 if the conditions are fullfilled. I tried your command once but I got all zero.

              Comment


              • #8
                Not so. Your example data came in #5 after my code in #4, but you could have applied it.

                I now do so and find that all the results are all 1. See below for evnt3w_max.

                Code:
                . rangestat (max) evnt3w, by(region) int(country . .) excludeself  
                
                . 
                . 
                end of do-file
                
                . su
                
                    Variable |        Obs        Mean    Std. Dev.       Min        Max
                -------------+---------------------------------------------------------
                     country |         55        15.2    10.76414          3         33
                          id |         55        15.2    10.76414          3         33
                        year |         55        1985    3.191424       1980       1990
                      evnt3w |         26    .1923077    .4019185          0          1
                   region_id |         55         3.4    .4944132          3          4
                -------------+---------------------------------------------------------
                regcontagion |         55           0           0          0          0
                  evnt3w_max |         55           1           0          1          1
                
                .
                This fits your example data, and I can work with no other data. For example, there are two countries in region 4, 21 and 33: it's true for each of them that there is a 1 on record for the other country.

                Incidentally, your specification says nothing whatsoever about year. If being in the same year is part of the recipe that needs to be explicit.

                Also, my code does nothing to your existing regcontagion variable. I wasn't trying to overwrite it or re-create it.

                Comment


                • #9
                  Actually I forgot to mention the year. It should take one if and only if there is a crisis in the region in the same year and not for all years.

                  Definition: The contagion variable for country j takes the value 1 if and only if there is a crisis in at least
                  one country other than country j and this country is in the same geographical region as
                  country j.

                  This can mathematical be expressed as:
                  Click image for larger version

Name:	1.PNG
Views:	1
Size:	5.2 KB
ID:	1522321


                  Comment


                  • #10
                    That was my guess, but it wasn’t what you said in #1.

                    Now that is cleared up, my code needs to be by(region year).

                    Your claim in #7 remains unsubstantiated. My code will produce 1s when the data so imply.

                    Comment


                    • #11
                      rangestat (max) evnt3w, by(region_id year) int(id . .) excludeself

                      This works perfectly. Thanks Nick.

                      Comment


                      • #12
                        Thanks for the closure.

                        Comment

                        Working...
                        X