Announcement

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

  • Creating new variable based on values of two other variables.

    Hi, I am new to Stata. Have searched a lot but I am unable to find the solution. My problem is that I want to generate a new variable (Days) based on the values of two other variables (Start and end). Additionally, I want the quarters and company id to remain the same as shown in the example. An example is given below.
    what I Have is
    Company_id Quarter Start End
    1 1 130 133
    1 2 190 194
    1 3 230 231
    what I want is
    Company_id Quarter Start End Days
    1 1 130 133 130
    1 1 131
    1 1 132
    1 1 133
    1 2 190 194 190
    1 2 191
    1 2 192
    1 2 193
    1 2 194
    1 3 230 231 230
    1 3 231


    Only Quarters, Days & company_id is required/important. I have around 30,000+ quarters, with each quarter having a different start and end. Any simple way to do it? for every company, there are 81 quarters and there are 500 companies which all have a unique id.

    Thanks a lot.
    Last edited by Ahmed Khan; 11 Aug 2018, 10:02.

  • #2
    I'd describe what you want as a new set of observations for each Company_id and quarter, with a new variable that numbers those observations starting at "Start."
    Code:
    clear
    input Company_id Quarter Start End
    1     1     130     133
    1     2     190     194
    1     3     230     231
    end
    gen ndays = End - Start + 1
    expand ndays
    bysort Company_id Quarter: gen Days = Start + _n -1
    drop ndays
    list

    Comment


    • #3
      Originally posted by Mike Lacy View Post
      I'd describe what you want as a new set of observations for each Company_id and quarter, with a new variable that numbers those observations starting at "Start."
      Code:
      clear
      input Company_id Quarter Start End
      1 1 130 133
      1 2 190 194
      1 3 230 231
      end
      gen ndays = End - Start + 1
      expand ndays
      bysort Company_id Quarter: gen Days = Start + _n -1
      drop ndays
      list
      Hi Mike, Thanks alot for your solution and quick reply. This is perfect and is exactly what i wanted. You made my life much easier. I was looking for this for days. Thanks a lot. Stay happy and blessed.

      Comment

      Working...
      X