Announcement

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

  • Generating a variable if value of obs1 is included in a local

    Some context before I ask my question:

    I have a country dataset that is in wide mode:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str63 ghecause3 double(afghanistan albania algeria angola antiguaandbarbuda argentina armenia)
    "Population ('000) (2)"                                                 29186        2948       35977       23356          88       40896        2877
    "All Causes"                                                      17638.90519 791.6554766 9000.543422 17835.01064 21.86865848 11472.27578 959.7303262
    "I. Communicable, maternal, perinatal and nutritional conditions"           .   58.131547 2148.348008 12606.21368 3.040690815 1520.611924 92.53143811
    "A. Infectious and parasitic diseases"                                      . 13.73803992 457.8259199  6035.01793 1.172297013 486.7600974 22.80165377
    "1. Tuberculosis"                                                           .  .505634161 177.5524798 899.9878949  .043649908 21.98516738 6.782443094
    "2. STDs excluding HIV"                                                     .  .467617604 9.656389858 103.4173163  .028337724 7.828691408  .699179626
    "a. Syphilis"                                                               .  .118382254 3.978862245 97.20594281  .001195393 1.235240785  .073528887
    "b. Chlamydia"                                                              .  .086156928 1.686355774 1.142276611  .004408259 1.346285491  .133672959
    "c. Gonorrhoea"                                                             .  .043373299  .823506852 1.351495562  .001852797  .713995777  .081503836
    "d. Trichomoniasis"                                                         .  .086024014  .874204434  .796841495  .004953674  .960610828   .14323437
    "e. Genital herpes"                                                         .  .052038173  .870731822 1.259828214  .005215848  1.74979586  .071220726
    "f. Other STDs"                                                             .  .081642937 1.422728904 1.660931562  .010711753 1.822762678  .196018839
    "3. HIV/AIDS"                                                               .  .484557871 8.781621292 646.3431386   .51618161 83.02695196 1.462131419
    "4. Diarrhoeal diseases"                                                    . 6.460200488  161.511244 2027.283188  .152642029 56.17996447 6.319187744
    end
    I need to distinguish some countries from others, so I have created some locals with countries lists that I need, and I used these locals to create the variables needed. I merged this data set with other data sets to create a multi-year data set. I use this compiled data sets for several analyses that I need.

    For a different set of analyses, I need the data in a different shape. I use xpose to transpose and then a reshape from long to wide to finally have the following:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str24 country float(dalys_diarrhea2010 dalys_diarrhea2019 dalys_diarrheachange)
    "afghanistan"                    .         .           .
    "albania"                   6.4602  5.510137  -.14706416
    "algeria"                161.51125 151.41914  -.06248543
    "angola"                 2027.2832 1099.4377   -.4576792
    "antiguaandbarbuda"      .15264203  .1752379   .14803186
    "argentina"               56.17997  52.46862   -.0660617
    "armenia"                 6.319188  5.967241  -.05569488
    end
    Now to my question:
    I would like to create a categorical variable that identifies the countries in the same way I already did with the locals earlier. Something of this sort:

    Code:
    local jee1 afghanistan angola benin botswana burkinafaso 
    local jee2 algeria bhutan burundi cambodia cameroon comoros 
    
    * more locals are added, the code continues, I xpose, reshape, etc.
    
            gen jee = 1 if inlist(country,`jee1')
            replace jee = 2 if inlist(country,`jee2')
    I would really appreciate any help on how to do this. I guess I could just replace the code above to have the full list of countries that are in each category, but I have several locals, each with several countries, so a more expedite way of doing so would be great!

  • #2
    -inlist(country, `jee1')- will fail, because the function arguments of -inlist- need to be bound in quotes and separated by commas. In principle you could modify the local macros to do that, but I think it is more straightforward to do this:
    Code:
    gen jee = .
    foreach j of local jee1 {
        replace jee = 1 if country == `"`j'"'
    }
    foreach j of local jee2 {
        replace jee = 2 if country == `"`j'"'
    }
    You say you have "several" locals. If several means a large enough number that the above approach is impractical, then you can do nested loops. Let's suppose there are 100 such local macros, named jee1 through jee100:

    Code:
    gen jee = .
    forvalues i = 1/100 {
        foreach j of local jee`i' {
            replace jee = `i' if country == `"`j'"'
        }
    }
    will do it. (Of course, you can do it this way even if you only have two local macros, but I think the first approach is easier to read and understand than the second if the number of local macros involved is small enough.)

    Comment


    • #3
      This is wonderful! Thank you so much Clyde!! I had tried something similar but had failed at setting the right combination of `' and "" (I didn't add the outer `'). So I really appreciate this clarification.

      I found a workaround earlier today, in case it is useful for anyone in the future. Before the xpose and reshape, I added this bit of code:

      Code:
          set obs `=_N+1'
          replace sex = "jee" in `=_N'
          foreach lcl in jee1 jee2 jee3 jee4 jee5 nojee nojeeeur {
          foreach country of local `lcl' {
              replace `country' = 1 in `=_N' if "`lcl'"=="jee1"
              replace `country' = 2 in `=_N' if "`lcl'"=="jee2"
              replace `country' = 3 in `=_N' if "`lcl'"=="jee3"
              replace `country' = 4 in `=_N' if "`lcl'"=="jee4"
              replace `country' = 5 in `=_N' if "`lcl'"=="jee5"
              replace `country' = 6 in `=_N' if "`lcl'"=="nojee"
              replace `country' = 7 in `=_N' if "`lcl'"=="nojeeeur"
              }    
              }

      Comment

      Working...
      X