Announcement

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

  • xtset generate lagged variable


    Dear all,

    I have a large panel , the panelid is firmid, timeid is date, below I show you the first few obs. And my code.
    Code:
    clear
    input int(firmid date) double indret
    10000 9502 0 
    10000 9503 .0076336 
    10000 9504 -.0077519 
    10000 9505 0 
    10000 9506 0 
    10000 9509 0 
    10000 9510 0 
    10000 9511 0 
    10000 9512 0 
    10000 9513 0 
    10000 9516 0 
    end
    format %tddd-Mon-YY date

    here i try to generate lagged indret (industry stock return). but out put seems to be incorrect.
    Code:
    sort firmid date
    xtset firmid date
    
    gen L_indret=L1.indret
    output
    Code:
    input int(firmid date) double indret float L_indret
    10000 9502 0 . 
    10000 9503 .0076336 0 
    10000 9504 -.0077519 .0076336 
    10000 9505 0 -.0077519 
    10000 9506 0 0 
    10000 9509 0 . 
    10000 9510 0 0 
    10000 9511 0 0 
    10000 9512 0 0 
    10000 9513 0 0 
    10000 9516 0 . 
    end
    format %tddd-Mon-YY date
    why the lag variable has missing values ?

    Thanks,

    Rochelle

  • #2
    Rochelle:
    that seemingly weird result is due to:
    - the way lagged variables work (see -help tsset- for more details);
    - the gaps diagnosed by -xtset- in your panel dataset.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      Adding to Carlo's excellent advice, if you want 13-Jan-86 to be treated as if it were the day after 10-Jan-86 (I'm guessing that your data is based on weekdays and the gap is due to weekends), you should use a Stata business calendar. I have no need for them in my work, so I don't know much about them, other than that this is precisely the problem they solve. See -help business calendars-.

      Comment


      • #4
        Thank you Carlo !

        Thank you Clyde! Your assumption is correct. My data is daily stock return, so we do have no trading dates (weekends) in my sample.


        I follow http://www.stata.com/features/overvi...ess-calendars/

        and did

        Code:
        bcal create sp500, from(date)

        but not sure what it does. no new variable is created. Nothing changed.

        Comment


        • #5
          As noted earlier, I don't actually use business calendars myself, so I don't really know the details of using them. I think the gist of it is that having the business calendar, you then have to generate new business date variables from your original date variables using the -bofd()- function, and use the new business date variables in your -xtset- command. Then the lags and leads should work the way you expect them too. I'm afraid that's as much detail as I can provide here, having no experience with these commands myself.

          Comment


          • #6
            Thanks Clyde ! You are always very helpful !!!

            I will explore this further.

            Comment

            Working...
            X