Announcement

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

  • Arrays? For each country-year, I need to multiply one variable by another country's variable

    My observations are Country - Year [CCODE, YEAR]

    For each CCODE - YEAR, I have variables for:
    • Whether human trafficking criminalization was extant [HTEX] (I have multiple extancy variables, but if I can solve the problem with one, I can solve it with all of them.)
    • Ratio of total trade with [country] (that is, I have ~190 ratio variables) [e.g, RTT_USA]
    So now I need to replace the ratio variable so that it multiplies the ratio of trade with country X by whether or not criminalization was extant in country X.

    What I want to do seems like it would treat my data as an array. Something like:
    • replace RTT_USA = HTEX (at CCODE==2 & YEAR==_N) * RTT_USA
    Except there is no "at" in STATA.

    I've searched for help with arrays, but I haven't (yet) found what I am looking for.

    Can someone point me in the right direction?

    Thanks for the help.





  • #2
    I'm sure there is a way to do this but it is hard to give exact code without seeing the structure of your data. I think you will increase the chances of getting a useful answer if you provide a minimal example of your data (make sure to use dataex in doing so).

    Crucially, the answer depends on your CCODE variable and how its values can be mapped to the suffix of RTT_.

    Comment


    • #3
      Thanks for directing me to dataex.
      CREX is the criminalization extant variable.
      You said "the answer depends on your CCODE variable and how its values can be mapped to the suffix of RTT_". I am currently using RTT_country name, but I could code these by CCODE, e.g., RTT_002, RTT_020, etc., if that would help.
      But here is a sample of what I have right now:

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int CCODE float(YEAR CREX RTT_USA RTT_Canada RTT_Bahamas RTT_Cuba RTT_Haiti)
      2 1999 0 . .20800896 .0006009974 3.2197315e-06 .00052579364
      2 2000 1 . .20085923 .0006621087 1.7409515e-06 .0004377747
      2 2001 1 . .20084915 .0007064682 . .0004301493
      2 2002 1 . .19768904 .000766431 .00007633187 .0004463858
      2 2003 1 . .19571835 .0007807881 .0001287268 .0004836126
      2 2004 1 . .19108295 .0007897035 . .0004455986
      2 2005 1 . .1908994 .0009462982 . .0004348451
      2 2006 1 . .18201813 .0009348188 .00011768534 .0004457768
      2 2007 1 . .17799585 .0009422081 .0001406567 .0003807762
      2 2008 1 . .17328542 .000977119 . .0004064501
      2 2009 1 . .16279157 .001240453 . .000510284
      2 2010 1 . .16286044 .0012444503 .00011424664 .0005492034
      2 2011 1 . .16166545 .0011399171 .0000984496 .0004880529
      2 2012 1 . .16140826 .0010575873 .00012154484 .00047748126
      2 2013 1 . .1646336 .0010526509 . .000539093
      2 2014 1 . .1663787 .000971816 . .00055057317
      2 2015 1 . .1535427 .00075852 . .0005590239
      2 2016 1 . .14934006 .0006802157 . .00054935075
      2 2017 1 . .14970453 .0009107938 . .0005929421
      2 2018 1 . .1467522 .0008238165 .00006587688 .00056628
      2 2019 1 . .14792125 .0009006692 .000069899455 .0005548006
      20 1999 0 .7736016 . .00008791285 .0009975987 .00007918444
      20 2000 0 .7637421 . .00005127937 .0009449614 .00004240177
      20 2001 0 .763078 . .00005658457 .0009663604 .00004930397
      20 2002 0 .7560123 . .000070945745 .0007800197 .00006054171
      20 2003 0 .7405699 . .00011131112 .0008556173 .00005391986
      20 2004 0 .7264463 . .0001196178 .0011820504 .00005493205
      20 2005 1 .706803 . .00015127925 .001231203 .00007184183
      20 2006 1 .6849571 . .00009522853 .0013719287 .00006032721
      20 2007 1 .6677517 . .0001864319 .001902706 .00006293314
      20 2008 1 .6540773 . .0003136244 .0018374892 .0000760795
      20 2009 1 .6264707 . .00024734688 .0011350731 .00008509078
      20 2010 1 .6217844 . .00025929158 .001308225 .00009142648
      20 2011 1 .6126633 . .0001646019 .0013155427 .00007083104
      20 2012 1 .6213898 . .0007027666 .0010526427 .00007197165
      20 2013 1 .6359597 . .00024553153 .0010211723 .0000739656
      20 2014 1 .6546726 . .0003249561 .0009793446 .00008532064
      20 2015 1 .6441894 . .000104972 .0009596203 .00008779987
      20 2016 1 .637264 . .00012308948 .0007723884 .00009560886
      20 2017 1 .634165 . .00020100956 .0008483139 .00010488044
      20 2018 1 .6290261 . .0003213697 .0009966702 .00010721642
      20 2019 1 .6277526 . .0002140134 .0008052898 .00014869407
      31 1999 0 .27946374 .010224804 . . .00006033816
      31 2000 0 .842862 .011639087 . .00013822505 .00004561
      31 2001 0 .7953753 .010836566 . .0007360798 .00004855591
      31 2002 0 .8498988 .007991798 . .0003148338 .00013359508
      31 2003 0 .8150369 .009182039 . .0008550372 .00009006741
      31 2004 0 .8467401 .010683375 . .0006989698 .00015051763
      31 2005 0 .7978957 .010404777 . . .00001200389
      31 2006 0 .7970036 .01068365 . .0013274024 .00011994073
      31 2007 0 .8415241 .015784519 . .0011083819 .00011363143
      31 2008 1 .8048447 .011632663 . .0030572566 .00009746198
      31 2009 1 .8603792 .011747212 . .0013651288 .00004134605
      31 2010 1 .8653562 .01016615 . .00011879368 .00004672063
      31 2011 1 .8577237 .008868153 . .00002663195 .0000641131
      31 2012 1 .8234934 .009809166 . .000022415905 .00018424998
      31 2013 1 .6277195 .006974267 . .00002529288 .00005242275
      31 2014 1 .6068332 .005471679 . 9.086859e-06 .000024052786
      31 2015 1 .6741999 .002336147 . 8.564489e-06 .00011077472
      31 2016 1 .7230509 .0023319826 . 3.573522e-06 .00007866952
      31 2017 1 .8086919 .007287127 . 1.9992108e-06 .000173003
      31 2018 1 .6501756 .002727276 . 5.846951e-06 .00008942281
      31 2019 1 .5615059 .002441529 . 1.0867777e-06 .0000981658
      40 1999 0 .001396478 .10979548 . . .
      40 2000 0 . .09756303 .009537037 . .002101621
      40 2001 0 . .09310499 .0017402614 . .0018816678
      40 2002 0 .031326327 .08427194 .0018214846 . .0027397885
      40 2003 0 .05223371 .08240145 .0036058656 . .001892836
      40 2004 0 .05655793 .09879734 .0003832151 . .0015788614
      40 2005 0 .05010577 .084609 .0003199896 . .001506026
      40 2006 0 .06001022 .0499823 .00050152524 . .0017869854
      40 2007 0 .07046451 .05962591 .00054681225 . .0020138286
      40 2008 0 . .069792755 .0004163133 . .0014802193
      40 2009 0 . .04196902 .00039119 . .0014160456
      40 2010 0 .063877314 .05344979 .00041427705 . .0015182937
      40 2011 0 .05163706 .05467102 .0004722256 . .0017791953
      40 2012 0 .06379377 .04742147 .0004385769 . .001628488
      40 2013 0 . .04593359 .0004268417 . .0015993513
      40 2014 0 . .04632789 .000360155 . .0013049884
      40 2015 0 . .0401604 .0003043284 . .0010757832
      40 2016 0 . .034897793 .00025652253 . .0008680848
      40 2017 0 . .035004266 .0003081595 . .0010743823
      40 2018 0 .0385279 .03808533 .0003228661 . .0011153065
      40 2019 0 .04541237 .03733552 .0003243287 . .0011018907
      41 1999 0 .6421542 .02684623 .00010844223 . .
      41 2000 0 .528506 .02874407 .0002708734 .010977737 .
      41 2001 0 .487538 .03031371 .00005822317 .011075672 .
      41 2002 0 .4504561 .03244113 .0008161257 .00781029 .
      41 2003 0 .4727193 .029159216 .00017557095 .01085023 .
      41 2004 0 .50070274 .023094364 .00008112114 .006651576 .
      41 2005 0 .498795 .03179852 .00005509596 .00652417 .
      41 2006 0 .4628399 .02629243 .00016446153 .007125716 .
      41 2007 0 .4551762 .032460585 .00011567718 .006719545 .
      41 2008 0 .41045305 .037189834 .00011593238 .007563096 .
      41 2009 0 .4630338 .033143535 .00008235525 .007020697 .
      41 2010 0 .4040279 .03179716 .00005550984 .008296079 .
      41 2011 0 .4053949 .025745476 .00006291215 .007160924 .
      41 2012 0 .4406133 .027924515 .0002731663 .006759173 .
      41 2013 0 .4321321 .025158556 .00005322924 .00697259 .
      41 2014 1 .4316369 .027869916 .00005821321 .006918729 .
      end

      Comment


      • #4
        I don't know how or what you searched, but Stata gives some answers. I would start with

        Code:
        . search array, faq
        
        Search of official help files, FAQs, Examples, and Stata Journals
        
        FAQ     . . . . . . . . . . . . . . . .  Implementing SAS-like ARRAYs in Stata
                . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . W. Gould
                2/03    How do I implement SAS-like ARRAYs in Stata?
                        http://www.stata.com/support/faqs/data-management/
                        implement-sas-arrays/
        A wider search turns up also a command I know, or least remember, nothing about:

        ​​​​​​​
        Code:
        ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        package array from http://fmwww.bc.edu/RePEc/bocode/a
        ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        
        TITLE
              'ARRAY': package to support arrays of variables
        
        DESCRIPTION/AUTHOR(S)
              
              This package contains four programs, and their associated help
              files:  array.ado, aelem.ado, assign.ado, and ido.ado.  The first
              is used to  define arrays of variables, the second provides
              access to particular array  elements (i.e., variables), the third
              assigns values to array elements, and  the fourth provides a
              simple looping construct to specify array elements in  a
              systematic manner. This provides the Stata programmer with most
              of the capabilities of SAS arrays.
              
              Author: David E. Moore, The Hartman Group, Inc.
              Support: email david@tinderboxthg.com
              
              Distribution-Date: 19980616
        
        ANCILLARY FILES                                     (click here to get)
              array.zip
        These are different kinds of answers, but there are others: If William (Bill) Gould were writing that FAQ now and not in 2003, he would want to tell you about Mata as a way to work on several variables at once.

        Also, just looking at your dataset, my main reaction is to reshape

        Code:
        reshape long RTT_, i(CCODE YEAR) j(country) string

        after which most things are easier. Otherwise, a short answer is to loop over variables.
        Last edited by Nick Cox; 25 May 2020, 09:23.

        Comment


        • #5
          Thanks for the help! I was able to use "reshape" to solve my problem. Again, thanks for all of the suggestions.

          Comment

          Working...
          X