Announcement

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

  • How do I efficiently create race variables from race_gender variables?

    Hello,

    Currently, I am working with data that separates each race by gender. See example data below:

    [CODE]
    input long(wa_male wa_female ba_male ba_female)
    21295 22002 4559 5130
    1411 1316 362 317
    1521 1526 399 374
    1658 1620 431 406
    1628 1585 502 424
    1201 1184 293 312
    1234 1223 277 350

    Where wa=white and ba=Black

    What I want are variables that are the total of each respective race, so that I end up with:
    [CODE]
    input float(white black)
    43297 9689
    2727 679
    3047 773
    3278 837
    3213 926
    2385 605
    2457 627

    To obtain the above results, I typed in the following two commands:
    egen white=rowtotal(wa_male wa_female)
    egen black=rowtotal(ba_male ba_female)

    However, I have many race variable that I am working with and I'd like to see if there is an easier way to rowtotal all of them into their own respective race totals, perhaps by using foreach command (or something similar). I just can't find an easier way to do this as I'd still (I think) need to hand-rename the races or hand rowtotal everything. Any advice--including letting me know that how I'm doing it is in fact the easiest/fastest way--is much appreciated.

    Thanks,
    Brooke

  • #2
    Well, there's no magic way to have Stata understand that "wa" is to become "white" without you telling it. The following code may start you in a useful direction.
    Code:
    . local race_wa white
    
    . local race_ba black
    
    . foreach r in wa ba {
      2.     generate `race_`r'' = `r'_male + `r'_female
      3. }
    
    . list, clean abbreviate(16)
    
           wa_male   wa_female   ba_male   ba_female   white   black  
      1.     21295       22002      4559        5130   43297    9689  
      2.      1411        1316       362         317    2727     679  
      3.      1521        1526       399         374    3047     773  
      4.      1658        1620       431         406    3278     837  
      5.      1628        1585       502         424    3213     926  
      6.      1201        1184       293         312    2385     605  
      7.      1234        1223       277         350    2457     627

    Comment


    • #3
      Thank you!

      Comment

      Working...
      X