Announcement

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

  • Generating new variable based on the maximum value of a group of variables by country

    Dear all,

    I am currently working with a country-level religion adherence dataset which looks as follows:

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input str34 country double(CATH00 PROT00 OTHCHRIST00 ORTH00 JEWS00 MUSLIM00 HINDU00 BUDDIS00 EASREL00 OTHREL00 NONREL00)
    "Afghanistan"                 0    0    0    0    0 .981 .004    0 .001                .014    0
    "Albania"                  .168 .006  .02 .161    0 .388    0    0 .002                   0 .256
    "Algeria"                  .001    0 .002    0    0 .967    0    0    0                   0  .03
    "American Samoa"              .    .    .    .    .    .    .    .    .                   .    .
    "Andorra"                  .892 .002 .041    0 .003 .006 .005    0 .001                   0  .05
    "Angola"                   .621  .15 .169    0    0    0    0    0    0                .051 .009
    "Antigua and Barbuda"      .115 .642 .177    0    0 .004 .001    0 .009                .038 .014
    "Argentina"                 .85 .034 .048 .002 .013  .02    0 .001 .001                   0 .031
    "Armenia"                  .046 .003 .009 .782    0 .027    0    0    0                   0 .133
    "Aruba"                       .    .    .    .    .    .    .    .    .                   .    .
    "Australia"                 .26 .322 .178 .034 .005 .012 .003 .013 .007  .00399999999999998 .162
    "Austria"                  .755  .05 .073 .019 .001 .022    0 .001 .001  .00199999999999999 .076
    "Azerbaijan"               .001    0 .001 .045 .004 .837    0    0    0                   0 .113
    "Bahamas, The"             .156 .634 .131 .001 .003    0    0    0 .004                .018 .053
    "Bahrain"                  .041 .013 .047 .004 .001 .824 .063 .001 .002                   0 .005
    "Bangladesh"               .002 .001    0    0    0 .857 .123 .006    0                 .01 .001
    "Barbados"                 .041 .601 .327 .001    0 .008 .003    0 .013 .000999999999999973 .005
    "Belarus"                  .132 .013 .071 .487 .006 .003    0    0    0                   0 .289
    "Belgium"                  .809 .013 .055 .005 .002 .036    0 .002 .001  .00199999999999992 .075
    "Belize"                   .568 .208 .133    0 .011 .006 .023 .004 .029   .0100000000000001 .008
    "Benin"                    .208 .038 .034    0    0   .2    0    0 .002                .515 .003
    "Bermuda"                  .142 .596 .188    0    0    0    0    0 .005                .029  .04
    "Bhutan"                      0 .002 .003    0    0  .01 .205  .74    0                .038 .002
    "Bolivia"                   .86 .049 .032    0    0    0    0 .001 .032  .00900000000000001 .017
    "Bosnia and Herzegovina"   .172 .001 .001 .176    0   .6    0    0    0                   0  .05
    "Botswana"                 .037 .117 .446    0    0 .002 .001    0 .008                .388 .001
    "Brazil"                   .742 .093 .082 .001 .002 .001    0 .003    0                .049 .027
    "Brunei Darussalam"        .017 .032 .027    0    0 .644 .008 .091 .056                .113 .012
    "Bulgaria"                 .011 .012 .073 .716 .001 .119    0    0    0                   0  .07
    "Burkina Faso"             .095 .065 .005    0    0 .486    0    0    0                .342 .007
    "Burundi"                  .572 .195  .15    0    0 .014 .001    0 .001                .066 .001
    "Cambodia"                 .002 .002 .007    0    0 .023 .003 .847 .049                 .04 .027
    "Cameroon"                 .265 .207  .07    0    0 .212    0    0 .004                .239 .003
    "Canada"                   .391 .156 .207 .017 .013  .01 .019 .008 .027   .0259999999999999 .126
    "Cabo Verde"               .894 .018  .02    0    0 .028    0    0 .002                 .03 .008
    "Cayman Islands"              .    .    .    .    .    .    .    .    .                   .    .
    "Central African Republic" .184 .144 .351    0    0 .156    0    0 .003                .153 .009
    "Chad"                     .066 .102  .06    0    0 .591    0    0 .011                .169 .001
    "Channel Islands"             .    .    .    .    .    .    .    .    .                   .    .
    "Chile"                    .674 .017   .2 .001 .002    0    0    0 .001  .00999999999999993 .095
    "China"                    .006 .001 .064    0    0 .015    0 .084 .285   .0420000000000001 .503
    "Colombia"                 .943 .015 .014    0    0 .001    0    0 .002   .0110000000000001 .014
    "Comoros"                   .01 .002 .001    0    0  .98    0    0 .001  .00500000000000001 .001
    "Congo, Dem. Rep."         .485 .201 .269    0    0 .011 .002    0 .004                .024 .004
    "Congo, Rep."              .493  .17 .249    0    0 .013    0    0 .004                 .05 .021
    "Costa Rica"               .872 .058 .036    0 .001    0    0 .001 .015                   0 .017
    "Cote d'Ivoire"            .148 .051 .124 .001    0 .301 .001    0 .002                .369 .003
    "Croatia"                  .885 .006 .006 .056 .001 .023    0    0    0                   0 .024
    "Cuba"                      .39 .017 .027    0    0 .001 .002 .001 .002                 .19  .37
    "Cyprus"                   .012 .011  .03 .686    0 .219    0    0 .001  .00199999999999993 .039
    "Czech Republic"           .404 .031 .189 .006 .001    0    0    0    0                   0 .369
    "Czechoslovakia"              .    .    .    .    .    .    .    .    .                   .    .
    "Denmark"                  .006 .878 .032    0 .001 .013 .001    0    0                   0 .069
    "Djibouti"                 .014    0 .001  .03    0 .941    0    0 .001                   0 .013
    "Dominica"                 .758 .149 .039    0    0 .002 .002 .001 .018                 .03 .001
    "Dominican Republic"       .883  .04 .019    0    0    0    0    0 .002                .031 .025
    "Ecuador"                  .931 .016  .03    0    0    0    0 .001 .002  .00499999999999995 .015
    "Egypt, Arab Rep."         .003 .008 .003 .136    0 .844    0    0    0                   0 .006
    "El Salvador"              .834 .054 .088    0    0    0    0    0 .004  .00400000000000005 .016
    "Equatorial Guinea"        .829  .02 .037    0    0 .041 .001    0 .005                .018 .049
    "Eritrea"                  .034 .006 .004 .461    0 .447    0    0    0  .00699999999999993 .041
    "Estonia"                  .004 .172 .295 .165 .002 .003    0    0    0                   0  .36
    "Ethiopia"                 .007 .136 .092 .365    0 .304    0    0    0                .094 .002
    "Faroe Islands"               .    .    .    .    .    .    .    .    .                   .    .
    "Fiji"                     .075 .402 .093    0    0 .069 .333    0 .011  .00400000000000002 .013
    "Finland"                  .001 .851 .065 .011    0 .002    0 .001    0  .00100000000000001 .068
    "France"                    .67 .008 .025 .006  .01 .071 .001 .008 .003  .00199999999999989 .196
    "French Polynesia"            .    .    .    .    .    .    .    .    .                   .    .
    "Gabon"                    .575 .172 .161    0    0 .046    0    0 .005   .0310000000000001  .01
    "Gambia, The"              .024 .005  .01    0    0 .869    0    0 .008                .078 .006
    "Georgia"                  .011 .005 .025 .581 .004 .193    0    0    0  .00100000000000006  .18
    "Germany"                  .335 .356 .059 .008 .001 .044 .001 .001    0  .00100000000000008 .194
    "Germany, East"               .    .    .    .    .    .    .    .    .                   .    .
    "Germany, West"               .    .    .    .    .    .    .    .    .                   .    .
    "Ghana"                    .095 .178  .28    0    0 .197    0    0 .001                .246 .003
    "Greece"                   .004 .001  .02 .921 .001 .033    0    0    0 .000999999999999904 .019
    "Greenland"                   .    .    .    .    .    .    .    .    .                   .    .
    "Grenada"                   .56 .355 .057    0    0 .003 .007    0 .002                .011 .005
    "Guam"                        .    .    .    .    .    .    .    .    .                   .    .
    "Guatemala"                .774  .09 .113    0    0    0    0    0 .002  .00299999999999998 .018
    "Guinea"                   .016 .009 .015    0    0 .673    0    0    0                .284 .003
    "Guinea-Bissau"            .105 .005 .017    0    0 .399    0    0    0                .458 .016
    "Guyana"                   .101 .285 .114  .01    0 .081 .325 .002  .02                .043 .019
    "Haiti"                    .738 .146 .074    0    0    0    0    0 .002                .026 .014
    "Honduras"                 .845 .056 .069 .001    0 .001    0 .001 .005                .011 .011
    "Hong Kong SAR, China"        .    .    .    .    .    .    .    .    .                   .    .
    "Hungary"                  .609 .236  .02 .008 .005 .006    0    0    0                   0 .116
    "Iceland"                   .01 .891  .07    0    0    0 .001    0 .003  .00699999999999997 .018
    "India"                    .013 .015 .031 .003    0 .121 .771 .007 .002   .0219999999999999 .015
    "Indonesia"                .027 .057 .047    0    0 .547 .033 .009 .014                .245 .021
    "Iran, Islamic Rep."          0    0 .002 .003    0 .956 .001    0    0                .035 .003
    "Iraq"                     .012    0 .015 .006    0  .96    0    0    0                   0 .007
    "Ireland"                  .847 .044  .08    0 .001 .002    0    0    0  .00100000000000002 .025
    "Isle of Man"                 .    .    .    .    .    .    .    .    .                   .    .
    "Israel"                   .027 .004 .018 .009 .771  .12    0    0 .003                   0 .048
    "Italy"                    .798 .007 .014 .002 .001 .012    0    0 .001                   0 .166
    "Jamaica"                  .043 .289 .508 .001    0 .001 .012    0 .006                .101 .039
    "Japan"                    .004 .006 .028    0    0 .001    0 .571  .26                   0 .131
    "Jordan"                   .007 .003 .012  .02    0 .935    0    0 .003                   0 .021
    "Kazakhstan"               .031 .002 .047 .086 .001 .427    0 .001    0  .00299999999999995 .402
    end
    Basically, for each country I have adherence fractions of population for 10 religion groups and non-religion (incl. atheists). I would like to generate a variable that displays the predominant religion within each country, thus a variable that takes the highest fraction from these 10 religion groups and labels this religion group the predominant religion. For example, the religion group with the highest fraction in Afghanistan is MUSLIM, so I'd like the newly generated string variable mainreligion to have the value MUSLIM for Afghanistan.


    If anyone has an idea on how one could do this, I would greatly appreciate it.

    I thank you all for your time.

    Best,

    Satya

  • #2
    Code:
    rename *00 *_00
    reshape long @_00, i(country) j(religion) string
    by country (_00), sort: gen dominant_religion = religion[_N]
    reshape wide
    rename *_00 *00
    Note: You do not say what you want to happen if there are two religions that are exactly tied for the predominant position. In that case, the code shown here will break the tie randomly and irreproducibly.

    Comment


    • #3
      Dear Clyde,

      Thank you for your reply. Your code works perfectly. And that is an interesting point you make, and I have actually no clue how to answer on you that matter, so I will think about that one, thanks! I did use the following code to count for each country how many distinct observations there are:
      Code:
      egen tag= tag(country BUDDIS00 CATH00 EASREL00 HINDU00 JEWS00 MUSLIM00 NONREL00 ORTH00 OTHCHRIST00 OTHREL00 PROT00)
      egen distinct = total(tag), by(country)
      And I found no value which was higher than 1, so I can conclude that in my case there is not the problem of two religions that are exactly tied for the predominant position.

      Thank you!

      Best,

      Satya

      Comment

      Working...
      X