Announcement

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

  • Looping with paired values but not for every variable

    Hi, let's say I want to create a series of dummy variables as follows:

    Code:
    order work_deg_sector*,sequential
    egen dummy1_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(1)
    egen dummy1_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(1)
    egen dummy1_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(1)
    egen dummy1_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(1)
    egen dummy1_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(1)
    
    gen dummy1 = .
    replace dummy1 = dummy1_1970 if byear==1970
    replace dummy1 = dummy1_1971 if byear==1971
    replace dummy1 = dummy1_1972 if byear==1972
    replace dummy1 = dummy1_1973 if byear==1973
    replace dummy1 = dummy1_1974 if byear==1974
    
    
    egen dummy2_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(2)
    egen dummy2_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(2)
    egen dummy2_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(2)
    egen dummy2_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(2)
    egen dummy2_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(2)
    
    gen dummy2 = .
    replace dummy2 = dummy2_1970 if byear==1970
    replace dummy2 = dummy2_1971 if byear==1971
    replace dummy2 = dummy2_1972 if byear==1972
    replace dummy2 = dummy2_1973 if byear==1973
    replace dummy2 = dummy2_1974 if byear==1974
    I want to create multiple dummy variables. Accordingly, I would like a loop that changes the "dummy1" name to "dummy2" when the loop also changes "v(1)" to "v(2)". However, I am not fully sure how to do this. For example, if I run the loop below, then it'll try to create dummy1 and dummy2 twice, using each values that goes into v()

    Code:
    local dvars dummy1 dummy2 
    foreach d of local dvars {
    foreach i of numlist 1 2 {
    
    egen `d'_1970 = anymatch(work_deg_sector_1998_01-work_deg_sector_2005_04) if byear==1970,v(`i')
    egen `d'_1971 = anymatch(work_deg_sector_1998_01-work_deg_sector_2006_05) if byear==1971,v(`i')
    egen `d'_1972 = anymatch(work_deg_sector_1998_01-work_deg_sector_2007_05) if byear==1972,v(`i')
    egen `d'_1973 = anymatch(work_deg_sector_1998_01-work_deg_sector_2008_08) if byear==1973,v(`i')
    egen `d'_1974 = anymatch(work_deg_sector_1998_01-work_deg_sector_2009_03) if byear==1974,v(`i')
    
    gen `d' = .
    replace `d' = `d'_1970 if byear==1970
    replace `d' = `d'_1971 if byear==1971
    replace `d' = `d'_1972 if byear==1972
    replace `d' = `d'_1973 if byear==1973
    replace `d' = `d'_1974 if byear==1974
    
    
    }
    }
    What would be the most efficient way of creating these multiple dummies? Note that the dummies will have entirely different names (e.g., south, north).

    Please let me know if I should clarify anything

  • #2
    A previous version of this answer overlooked the different variable ranges; my mistake. But I can't see any pattern in the month parts of

    2005_04 2006_05 2007_05 2008_08 2009_03

    that makes it easy to simplify the code.

    The implication of this question is that you are holding panel data in wide layout (some say wide format or structure), which is certainly ill-advised. You should, on this evidence, reshape long as otherwise most non-trivial statistical analysis is impossible in Stata and most data management tasks will lead to extraordinarily lengthy code, as above.
    Last edited by Nick Cox; 31 Jul 2022, 05:04.

    Comment

    Working...
    X