Announcement

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

  • foreach & levelsof, using two variables

    Hi Statalist,

    I have constructed 27 portfolios across 1962 - 2018. Now i want to find the market cap average percentiles across the years and the portfolios for June.

    I have tried this:
    Code:
    // We only keep the June data to identify each portfolio
    keep if month(dofm(YM)) == 6
    
    drop if portfolio27 == .
    
    // Identify percentiles in each portfolio
    levelsof june_year, local(years)
    levelsof portfolio27, local(level)
    gen int size_percentiles = .
    foreach y of local years | x of local level {
        pctile cutoffs = MktCap if june_year  == `y' | portfolio27 == `x', nq(3)
        xtile pf = MktCap, cutpoints(cutoffs)
        replace size_percentiles = pf if june_year == `y' | portfolio27 == `x'
        drop pf cutoffs
    }
    but i get this error message ''{ required''. So i tried to add another set of brackets and got this error ''program error: code follows on the same line as open brace'', even without the error, is this really doing what i want it to?

    PS. I know that the code works, because i used it to construct the portfolios, but that was only on june_year, so in this example i have tried to add another levelsof, namely the portfolio27.

    Data sample:
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(june_year MktCap portfolio27)
    1974    6.0145 19
    1975  15.68175 12
    1976   14.4495 12
    1977         . 11
    1971  242.5963 17
    1972 254.29945 17
    1973 250.79996 16
    1974  246.0596  9
    1975         .  9
    1977 293.78162 16
    1978 308.89575 16
    1979  292.1955 17
    1980         . 17
    1982  287.0055 18
    1987    1.6495 19
    1988  6.654375 19
    1989  6.329674 12
    1990  5.013796  2
    1991     7.152  3
    1992    34.344  2
    1993    126.45 10
    1994 190.25626  6
    1982   12.7875  3
    1983    12.705  3
    1984     10.83  3
    1985    19.944  1
    1986    16.874 10
    1976  7.890738 21
    1977         . 21
    1979  21.77247  3
    1980  62.57572 10
    1981  52.41597 13
    1982 202.86795  6
    1983  157.0747  9
    1984 210.75395  9
    1985   537.444  7
    1986   686.475  7
    1986 228.45325  4
    1987   356.094  8
    1988 188.35873 16
    1989 257.36996 17
    1990    253.74  7
    1991  318.9904  9
    1992 583.59436  9
    1993    296.67  7
    1994  405.6938 16
    1995  431.8043  8
    1996 367.74625  9
    1997    559.13 17
    1998  979.1423 18
    1999 2172.8984  9
    2000 1097.8752 18
    2001  870.4118 18
    2002 1115.4672 16
    2003  1074.688 25
    2004   771.005 26
    2005 1317.0795 22
    2006 1134.2701 27
    2007  969.8489 27
    1988  541.9273  9
    1989    379.33 18
    1990 295.57333 18
    1991 317.95575 26
    1992  470.8856 26
    1993  634.4625 26
    1994  889.4475 18
    1995  1040.405 18
    1996 1102.0231 26
    1997         . 25
    1974  6.316873 19
    1975  8.252998 19
    1976     7.422 19
    1977    10.392 19
    1978 13.152375 20
    1971  1160.392  9
    1972  406.7928  9
    1973   283.815  9
    1974   382.561  7
    1975         . 16
    1979  324.8184 17
    1980  532.4825 25
    1981  357.6225 18
    1982   385.009 18
    1983  428.0753 17
    1984   489.843 25
    2015 15206.462  7
    1994         .  3
    1997  22.17456 10
    1998   29.3615  1
    1999  18.31578  3
    2000    15.796 10
    2001   22.9968 10
    2002   17.9622  2
    2003 17.925648 11
    2004  30.27879 10
    2005    90.784  3
    2006  167.8835  3
    2007  88.04961  3
    2008  56.83838  3
    2009   67.2382 19
    end

    Let me know if something is unclear

    Dennis










  • #2
    I think you just need two loops, one for each variable
    Code:
     levelsof june_year, local(years)
    levelsof portfolio27, local(level)
    gen int size_percentiles = .
    foreach y of local years {  
          foreach x of local level {    
               pctile cutoffs = MktCap if june_year  == `y' | portfolio27 == `x', nq(3)    
               xtile pf = MktCap, cutpoints(cutoffs)    
                replace size_percentiles = pf if june_year == `y' | portfolio27 == `x'    
               drop pf cutoffs    
           }
    }
    HTH
    Fernando
    Last edited by FernandoRios; 30 May 2018, 06:15.

    Comment


    • #3
      Thanks, it works

      Just not as intended but thats another problem..

      Comment

      Working...
      X