Announcement

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

  • Collapsing code

    Hello all, is there a good way to collapse all of this code into a smaller number of lines of code that is more efficient rather than typing it out over and over? I apologize if this is very elementary, I find myself blanking on how to do this and re-referencing my STATA manuals to try and find the answer myself. Thanks in advance. Here is the code:

    replace Age50to64 = (labor/multiplier[1])*multiplier[3] if year == 2012
    replace Age50to64 = (labor/multiplier[1])*multiplier[4] if year == 2013
    replace Age50to64 = (labor/multiplier[1])*multiplier[5] if year == 2014
    replace Age50to64 = (labor/multiplier[1])*multiplier[6] if year == 2015
    replace Age50to64 = (labor/multiplier[1])*multiplier[7] if year == 2016
    replace Age50to64 = (labor/multiplier[1])*multiplier[8] if year == 2017
    replace Age50to64 = (labor/multiplier[1])*multiplier[9] if year == 2018
    replace Age50to64 = (labor/multiplier[1])*multiplier[10] if year == 2019
    replace Age50to64 = (labor/multiplier[1])*multiplier[11] if year == 2020
    replace Age50to64 = (labor/multiplier[1])*multiplier[12] if year == 2021
    replace Age50to64 = (labor/multiplier[1])*multiplier[13] if year == 2022
    replace Age50to64 = (labor/multiplier[1])*multiplier[14] if year == 2023
    replace Age50to64 = (labor/multiplier[1])*multiplier[15] if year == 2024
    replace Age50to64 = (labor/multiplier[1])*multiplier[16] if year == 2025


  • #2
    Short answer is that you can do this:

    Code:
    local j = 3
    forval y = 2012/2025 {
              replace Age50to64 = (labor/multiplier[1])*multiplier[`j'] if year == `y'
              local ++j
    }

    A longer answer, in terms of your work, is to ask whether your data structure is fit for purpose. I sense here bad habits from spreadsheet use in so far as your variables are not aligned within observations, but it may not matter that much.
    Last edited by Nick Cox; 26 Oct 2016, 09:21.

    Comment


    • #3
      Sure, this what loops are for.

      Code:
      local m=3
      forvalues y=2012/2025{
      replace Age50to64 = (labor/multiplier[1])*multiplier[`m'] if year == `y'
      local m=`m'+1
      }

      Comment


      • #4
        Thank you both. My issue has been solved.

        Comment

        Working...
        X