Announcement

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

  • Help _Loop _ "Forval" command_ create implied volatility using STATA

    Hello everyone,

    I have a dataset that has:
    1) data from 1/1/1990 to 12/1/2014 (in this format 12/1/2014)
    2) I divided the date in three variable: day (1..31), month (1..12) and year (1990 .. 2014).
    3) I grouped by month and year and created a variable that counts the days as they go until the end of the month and a variable that reports the total number of days in the month.
    4) my X is a squared on compounded S&P500 log returns.

    My code looks like this: g date = date("01/01/2008", "MDY")
    gen month=month(date)
    gen yr=year(date)
    gen day=day(date)
    egen first_id = group(month yr)
    sort first_id
    by first_id: generate N1 = _N
    by first_id: generate n1 = _n
    I am trying to create a loop that allows me to:
    1) sum all my X from the first day of the month to the last day of that month when it's the first of the month
    2) sum all my X from the second day of the month to the last day of that month when it's the second of the month
    3) sum all my X from the third day of the month to the last day of that month when it's the third day of the month
    4) and so on, for each month.

    For example when it's 1/1/1990 I am summing my X from 1/1/1990 to 1/31/1990, when it's 1/2/1990 I am summing my X from 1/2/1990 to 1/31/1990 and so on. When the month starts again, 2/1/1990 I will repeat the same logic and sum my X from 2/1/1990 to 2/31/1990 and so on.

    So far I have used this code, BUT IT IS NOT CORRECT; sort date
    g monthsum=
    forval n = 1/`=_N' {
    cap drop i`n'
    g i`n' = 1 if n1>=1 & n1<= N1
    egen monthsum`n' = sum(X) if i`n'==1
    replace monthsum = monthsum`n' if mi(monthsum)
    drop i`n'
    drop monthsum`n'
    }
    Can anyone help me with the code for this LOOP ? Thank you so much

    Giulia
Working...
X