Announcement

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

  • Generating Binary Variable using Local Macros

    I have a panel dataset involving a bunch of different countries. I am looking to construct a local macro by which I can create a binary variable which is equal to 1 for a developed country and 0 for a developing country. How would I do this using local macros?

    . dataex

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date byte ID str11 country float(dm ctr fdncdiff fdnddiff fdnvdiff fdecdiff fdeddiff fdevdiff fdctr)
    4868 19 "France"    159   -.009145856    .037080456   -.04080692   .004857263   -.02857831  -.033172935    .005448088    -.010164057
    4868  2 "Australia" 159 -.00024956846   -.033006944 -.0011386788   .007398677   .007256866  .0016877055   .0019807816   -.0006408025
    4868  7 "Canada"    159   -.005427846   -.036944978  -.006934156  -.005309578  -.009573966   -.00951782   .0035434216   -.0009796345
    4868 59 "UK"        159   .0041462616 -.00054898206   -.05203605 -.0023859595   .007279336   .006423324    .007751971     .005355704
    4868 30 "Japan"     159   -.004951578   .0021359883    .08419366  -.002828053  .0041186213 -.0024021864  -.0006611645    -.008301396
    4868 20 "Germany"   159   -.001175694    -.08412702  -.019894077  -.019909587  -.012164474  -.018957317    .014137488    -.003162872
    4868  3 "Austria"   159  -.0046039913   -.028232163   -.02325177  -.012226265  -.020529747  -.027514756     .01605246     .005433331
    4868 29 "Italy"     159   -.010749075    -.06855877    -.0428904  -.003392713  -.006325245  -.006189525    .008345798     .031893637
    4868  4 "Belgium"   159   -.008897077     .06059929  -.030633565  -.013313816  -.005979061  -.003455937    .007676736    .0016469182
    4868 46 "SA"        159   .0006007588  -.0018031355   .002888745   .005801771   -.01952034  -.015675068     .00299041    .0007795389
    4899 59 "UK"        160     .03015185  -.0010928642   .016735436 .00025191763   -.02092308   -.02602309    .007613122     .026005587
    4899 29 "Italy"     160   .0006843748     .07339588    .02607062 -.0028522734  -.031763434  -.030008256    .006731838      .01143345
    4899  7 "Canada"    160    .007353524     .02870879   .035058092   .003333847 -.0005263686 -.0016404092  -.0003268719      .01278137
    4899 19 "France"    160      .0537556      -.099546   .072831675 -.0025711455   -.06091115   -.06991023     .01096876      .06290145
    4899  3 "Austria"   160     .04379689    -.06557957  -.007652625   -.01347332    -.0286389  -.024022996    .006310193      .04840088
    4899 20 "Germany"   160     .05016674  -.0046150014    .05593802  -.006613092  -.026243627   -.02436471   -.005797785      .05134244
    4899  4 "Belgium"   160     .04795302   -.002767347   .068402424     .0158757   -.03487605  -.036820233 -8.195639e-06       .0568501
    4899 46 "SA"        160   -.001978605     .13127297   .005694224   -.00489718   .003752589  -.003852427    .010023057    -.002579364
    4899 30 "Japan"     160    .004815178   .0001616869   -.10610597  -.003347619     .0911177    .09688347   -.006295472     .009766756
    4899  2 "Australia" 160  -.0009183664     .09863126  -.036217436  -.006510527   .005163074  .0033949614     .00957431   -.0006687979
    4928  2 "Australia" 161   .0011469227    -.10367627   .026548835   .011999596   -.05952418    -.0645411   .0044633597     .002065289
    4928 59 "UK"        161    .005829735    -.04070522  -.009181259   .008359083   -.05002657   -.04834512    .007897019    -.024322113
    4928 20 "Germany"   161     .10598928   -.010834556   -.05093006    .01513018   .001951933   .005658746   -.011645803      .05582254
    4928  7 "Canada"    161  -.0026651046    -.03749692   -.06899622   .010017664  .0003002584    .00289318   .0018354654    -.010018628
    4928  4 "Belgium"   161     .06218279   -.030218177   -.03789504  -.003288321     .0135746   .015034556    .012352407     .014229767
    4928 29 "Italy"     161   .0041997675   -.009277061 -.0045413002    .02183777  -.017205775   -.02597594    .005702555    .0035153925
    4928  3 "Austria"   161     .10912064    .035234045   -.01129606    .02674442   -.06594539   -.04885846   -.006410528     .065323755
    4928 46 "SA"        161      .0495581     -.0852003  -.024367517   .010702956   -.02542645  -.029423535    .005975962      .05153671
    4928 30 "Japan"     161    .011733835   -.030748226 -.0036900174    .03325931   -.03653073   -.04202279     -.0048486     .006918657
    4928 19 "France"    161    .036488753    -.01188732   -.02821311   .004389252  -.032577932  -.014310658     .00699605    -.017266847
    4960 59 "UK"        162    -.03149244     .03878761     .1383549  -.029298224   -.03045678  -.029313356     .01576233     -.03732217
    4960  7 "Canada"    162   -.002678315    .034097396    .05574148   -.00925471   -.04348052   -.04825509    .012934685 -.000013210112
    4960  2 "Australia" 162  .00005412633    .017756734    .03599448  -.018593572   -.02155161  -.016951442     .01493965   -.0010927963
    4960  3 "Austria"   162    .025602456    -.07216491    .07020565  -.033970986  -.037088573   -.05399096    .019202195      -.0835182
    4960 19 "France"    162   .0006161162   -.036387004    .06225763  -.014861988  -.014519215   -.01828903    .011894032     -.03587264
    4960  4 "Belgium"   162     .01183742   -.073903754    .03755669  -.014570802   -.00608635   -.00965625    .010304332     -.05034537
    4960 20 "Germany"   162     .03875528   -.011407372   .072677895  -.017924592   -.02633941  -.031734407   .0023061242       -.067234
    4960 46 "SA"        162   .0019791555     -.1535442    .07477184  -.013678072   -.03744453   -.03519714    .014600813     -.04757895
    4960 29 "Italy"     162    -.03604232     -.3435913    .03931174   -.02681206   -.02532053   -.02980077     .02122408     -.04024209
    4960 30 "Japan"     162   -.009036432    -.01715345  .0004579108   -.00941876   -.02515799   -.02516034    .027364194    -.020770267
    4991  4 "Belgium"   163    -.05352255    -.02763311   -.10573582   .025793176    .02100879   .028391004   -.005175054     -.06535997
    4991  3 "Austria"   163    -.04504953    .015436776   -.12159637   .028701954   .003276825   .029289544   -.016145932    -.070651986
    4991 29 "Italy"     163     .04107609      .2906154   -.08335485    .02949196   -.00943935   .021803856    -.00704062      .07711841
    4991 46 "SA"        163  -.0014919546      .0829822   -.09842768    .02256584   .032510698    .04030478  -.0010257959     -.00347111
    4991 19 "France"    163    -.04829838  -.0007255375   -.09956445   .022186283    .02699727    .03826177   -.005348057      -.0489145
    4991  7 "Canada"    163   -.003995073    -.06457353    -.0769782   .014284284 -.0034467876  .0004117191    .001531422   -.0013167583
    4991 59 "UK"        163   -.016654642    -.05196156   -.13703778    .04121619    .09742844    .10084388    -.01478806     .014837796
    4991 20 "Germany"   163    -.05198215    .032307047   -.11616877   .036021315   .026619196    .03726381   -.017293664     -.09073743
    4991 30 "Japan"     163  -.0029349134   -.013482016  -.005360764  -.008365958  -.032836877  -.029820457    .016710281     .006101519
    4991  2 "Australia" 163  .00010538664     -.0813527  -.029532775    .03545469   .021768987    .02360177  -.0039595068   .00005126032
    5019 46 "SA"        164   .0008961268    -.01201011     .1525722  -.036967795   -.03918773   -.06401807     .02173844    .0023880815
    5019 30 "Japan"     164   -.001605522     .05985041    .16581793   -.05026543  -.023466036   -.02973747    .007562399    .0013293914
    5019  7 "Canada"    164 -.00005986887     .11986263     .1179337   -.03318344  -.063060224    -.0661377    .017435774    .0039352044
    5019  2 "Australia" 164     .04901676     .12138728      .138346   -.04690368  -.010853648  -.012296617    .027159184      .04891137
    5019 29 "Italy"     164    .007468317    -.05907467     .1306853   -.06020827   -.04777575   -.07129109    .023034304    -.033607777
    5019 20 "Germany"   164    .018407557    .022808155     .1498899   -.05252516   -.06563401   -.07855433     .02048361       .0703897
    5019  4 "Belgium"   164     .02385526     .09300464    .14781907   -.03864049   -.07239121  -.070417404     .02120453       .0773778
    5019 59 "UK"        164   -.018633785      .0632773    .08477354   -.05101518  -.022809565   -.02930957      .0265674   -.0019791424
    5019  3 "Austria"   164     .01553809     .04145162    .11936178   -.05209243   -.07769537   -.09548897    .013257874      .06058762
    5019 19 "France"    164    .007268336     .07098536    .12367928   -.04427025   -.04194826   -.05150133    .018571079      .05556672
    5052  3 "Austria"   165    -.01426299     .07810003    .00397045    .04535904    .02363515   .023366094    -.04009156     -.02980108
    5052 59 "UK"        165    .010344846   -.002512174    -.0658516   .026693687    .05516544    .05442494   -.025429577      .02897863
    5052  4 "Belgium"   165   -.003092956    .021663554   -.07879907   .017344335    .08326387    .08077806    -.02100453    -.026948215
    5052  2 "Australia" 165  -.0010087917    .017279731     -.144814    .01995186    .13117135    .12967145    -.02136025     -.05002555
    5052 20 "Germany"   165    -.01254721     .08341475   -.09080525    .05861004      .084588    .08978504   -.032601472    -.030954767
    5052  7 "Canada"    165    .006848735    -.14720552    -.1658228    .04023273    .04926148    .05132326   -.018413216     .006908604
    5052 29 "Italy"     165   -.017816242     .10181251   -.07041867   .036780693    .07273382    .08523196   -.033240512     -.02528456
    5052 19 "France"    165    .005110486    .006401337   -.08972302    .02476486    .06737727    .07183099    -.02703452   -.0021578497
    5052 30 "Japan"     165   -.003608856    -.00940514   -.13563287    .04539982    .13344164    .14356819    -.04580858    -.002003334
    5052 46 "SA"        165 .000011982343     .06842233   -.09822205   .012181418    .07119411    .09864295    -.02171588   -.0008841445
    5082  7 "Canada"    166  -.0009686465    .016418248    .02385739 -.0013479702   -.10001647   -.10567462      .0242009    -.007817382
    5082 19 "France"    166     -.0792671    -.09903854    -.0474056    .03023124 -.0006742477    -.0133996 -.00012733042     -.08437759
    5082 59 "UK"        166     -.0392617    -.04162288   .002801449    .03526082   .003457576  -.004542351    .002162099     -.04960655
    5082  4 "Belgium"   166    -.06885295    -.12829652   -.10908108   .028783904   .007942915 -.0021901727  -.0004017651    -.065759994
    5082 46 "SA"        166  .00055877684    -.16203533    -.0872735    .03818274   .022385895 -.0034861565  -.0008019805    .0005467945
    5082 20 "Germany"   166    -.06899138     -.1601594   -.08322775  -.022192035  .0039460063   -.01773274     .02623518     -.05644417
    5082 29 "Italy"     166    -.04881486    -.19690254   -.14180009   .023844894    .02979642   .010752141   .0045750886     -.03099862
    5082 30 "Japan"     166    -.04845716        .00259   -.01915553  -.017090693    .02104374   .006727505    .003026411      -.0448483
    5082  2 "Australia" 166  -.0013899254    -.08981042   -.08127495    .02238027 -.0022403002  -.013694644   .0011423826   -.0003811336
    5082  3 "Austria"   166    -.05940832     -.2073314    -.1974218  -.019920943    .03881276    .02294749    .005904154     -.04514533
    5113  3 "Austria"   167    -.02765205      .1046629    .11145658   .013365455   -.20635927     -.162436   -.009124519     .031756267
    5113  2 "Australia" 167   .0022402727     .02410711     .0845066  -.023267416   -.11717916   -.11445278     .02925202     .003630198
    5113  4 "Belgium"   167    -.04444278      .0490826    .08621198  -.007960772   -.14254028   -.11682206     .03093499      .02441017
    5113 29 "Italy"     167     .02126082     .05292815    .10600744   -.02829282   -.17603886   -.15838206     .03059551     .070075676
    5113 59 "UK"        167    -.00942598    -.00943921    .04112091   -.01241102  -.016844243  -.012105227     .03846475     .029835723
    5113 20 "Germany"   167     -.0311573   .0044442043    .06016128   -.01247232   -.13508743   -.11310571    .005222127      .03783408
    5113  7 "Canada"    167    .003282546     .07173106   .070412695  -.013399924   -.10660785   -.09714097    .023834214    .0042511923
    5113 46 "SA"        167  -.0008570201     .21729127    .06169705   -.02950444   -.10943562    -.1056295    .034892768    -.001415797
    5113 19 "France"    167    -.04437569     .05565429    .05324989  -.018099437    -.0798164   -.06942093     .03027092      .03489141
    5113 30 "Japan"     167 -.00023055184   -.019761905    .06760137   -.00644167  -.033892862  -.025548004   -.012612134      .04822661
    5144  4 "Belgium"   168    -.01624205     .05790485   .020217555   -.02860358   -.02476406   -.03821313    .015631706     .028200734
    5144 46 "SA"        168    .000665712    -.04286212   .011349976  -.013773343  -.073336005   -.05639243    .005115062     .001522732
    5144 29 "Italy"     168      -.114199      .2160657    .07134809  .0039977105   -.04851353   -.04382718    .004041463     -.13545983
    5144 59 "UK"        168   -.017807154    .028635254  -.016077925  -.028998526  -.030750215   -.02728525     .01873106    -.008381174
    5144 19 "France"    168    -.03915419     .06443345  -.017809425   -.02184068   -.05798489   -.05179566    .008921295     .005221497
    5144  3 "Austria"   168    -.01551946     .04972937    .06189941  -.008830182   -.05658579   -.07407713    .006502842     .012132592
    5144  7 "Canada"    168    .007528321    .025385775    .01913008  -.007359887   -.04457927   -.04177797    .008905694    .0042457753
    5144  2 "Australia" 168     .00561344     .15516745   .002548579 -.0021031364   -.04238337   -.03774595    .006004632    .0033731675
    5144 30 "Japan"     168    -.06296154    .029251615     -.075394    .04164521    .02220053    .03633421   -.021121666     -.06273099
    5144 20 "Germany"   168    -.01860294      .1594281    .05551496   .024460027  -.065898836   -.05215216   -.006312154      .01255436
    end
    format %td date
    format %tm dm
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 10350 observations
    Use the count() option to list more

    .

  • #2
    What criterion or criteria are you using to define developed and developing? Whether local macros are part of the eventual answer I can't say.

    Comment


    • #3
      The easiest way would be to create a table that just contains the list of countries (or country_id) and then a 0/1 variable developed and merge that in. That way you don't have to mess with macros and loops.

      Code:
      *** To do it with a list or a macro
      
      *** Do it using inlist()
      gen developed = 0
      replace dev=1 if inlist(country, "Austria", "Australia", "Canada", "France", "Germany", "UK") // inlist() only allows 10 fields of text however
      
      *** Do it using a macro and loop
      local dev_country Japan Italy Canada France Germany UK
      foreach c in local `dev_country' {
          replace dev=1 if country=="`c'"
          }
      
      . tabulate country dev
      
                  |       developed
          country |         0          1 |     Total
      ------------+----------------------+----------
        Australia |         0         10 |        10 
          Austria |         0         10 |        10 
          Belgium |        10          0 |        10 
           Canada |         0         10 |        10 
           France |         0         10 |        10 
          Germany |         0         10 |        10 
            Italy |         0         10 |        10 
            Japan |         0         10 |        10 
               SA |        10          0 |        10 
               UK |         0         10 |        10 
      ------------+----------------------+----------
            Total |        20         80 |       100
      Code:
      *** But you would be better off with a table and then merging using that table
      
      *** I created a short table of countries
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input byte country_id str32 country byte is_developed
       1 "Afghanistan"                      0
       2 "Angola"                           0
       3 "Bangladesh"                       0
       4 "Benin"                            0
       5 "Bhutan"                           0
       6 "Burkina Faso"                     0
       7 "Burundi"                          0
       8 "Cambodia"                         0
       9 "Central African Republic"         0
      10 "Chad"                             0
      11 "Comoros"                          0
      12 "Democratic Republic of the Congo" 0
      13 "Australia"                        1
      14 "Austria"                          1
      15 "Belgium"                          1
      16 "Canada"                           1
      17 "Denmark"                          1
      18 "Finland"                          1
      19 "France"                           1
      20 "Germany"                          1
      21 "Italy"                            1
      22 "Japan"                            1
      23 "Sweden"                           1
      24 "UK"                               1
      end
      
      . list, noobs sepby(is) abbrev(12)
      
        +--------------------------------------------------------------+
        | country_id                            country   is_developed |
        |--------------------------------------------------------------|
        |          1                        Afghanistan              0 |
        |          2                             Angola              0 |
        |          3                         Bangladesh              0 |
        |          4                              Benin              0 |
        |          5                             Bhutan              0 |
        |          6                       Burkina Faso              0 |
        |          7                            Burundi              0 |
        |          8                           Cambodia              0 |
        |          9           Central African Republic              0 |
        |         10                               Chad              0 |
        |         11                            Comoros              0 |
        |         12   Democratic Republic of the Congo              0 |
        |--------------------------------------------------------------|
        |         13                          Australia              1 |
        |         14                            Austria              1 |
        |         15                            Belgium              1 |
        |         16                             Canada              1 |
        |         17                            Denmark              1 |
        |         18                            Finland              1 |
        |         19                             France              1 |
        |         20                            Germany              1 |
        |         21                              Italy              1 |
        |         22                              Japan              1 |
        |         23                             Sweden              1 |
        |         24                                 UK              1 |
        +--------------------------------------------------------------+
      
      
      *** Merging in from this table
      // Assume the file with country and is_developed is called "country_table.dta"
      
      merge m:1 country using "country_table.dta", keepus(country_id is_developed) keep(match master) gen(merge_country) nonotes
      
      . tabulate country is_dev
      
                            | is_develop
                            |     ed
                    country |         1 |     Total
      ----------------------+-----------+----------
                  Australia |        10 |        10 
                    Austria |        10 |        10 
                    Belgium |        10 |        10 
                     Canada |        10 |        10 
                     France |        10 |        10 
                    Germany |        10 |        10 
                      Italy |        10 |        10 
                      Japan |        10 |        10 
                         UK |        10 |        10 
      ----------------------+-----------+----------
                      Total |        90 |        90

      Comment

      Working...
      X