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

  • Split data frame by years

    Hi everybody,

    I am new in Stata. I want to split a data frame into several smaller ones. This looks like a very trivial question, however, I cannot find a solution from a web search.
    My data like that,
    Stock Year Return
    AAA 2001 0.1
    ABC 2001 0.2
    AAA 2002 0.15
    ABC 2002 0.2
    AAA 2003 0.12
    ABC 2003 0.21
    ABS 2003 0.3
    AAA 2004 0.1
    ABC 2004 0.2
    ABS 2004 0.31
    HSC 2004 0.4
    AAA 2005 0.13
    ABS 2005 0.2
    ABC 2005 0.12
    Now, I want to split this data file( in 5 years) into a seperate year. 2001, 2002,2003,2004,2005. How can I do it?
    Thank you in advance.

  • #2
    * Example generated by -dataex-. To install: ssc install dataex
    input str3 stock int year float return
    "AAA" 2001  .1
    "ABC" 2001  .2
    "AAA" 2002 .15
    "ABC" 2002  .2
    "AAA" 2003 .12
    "ABC" 2003 .21
    "ABS" 2003  .3
    "AAA" 2004  .1
    "ABC" 2004  .2
    "ABS" 2004 .31
    "HSC" 2004  .4
    "AAA" 2005 .13
    "ABS" 2005  .2
    "ABC" 2005 .12
    levelsof year, local(years)
    foreach year in `years'{
        keep if year==`year'
        local savefilename "savefilename`year'"
        save `savefilename'


    • #3
      Jorrit Gosens gave a good answer to your question; see also savesome from SSC.

      Nevertheless a fair question in return is why you think you need this. Almost always Stata users wanting something similar just need to keep their data in one file and use different machinery, such as
      if or by.


      • #4
        @Jorrit Gosens Thank you so much.


        • #5
          @Nick Cox
          Thank you for your question. Because I prepared to run the regression for a model like that.

          Returni,d= α + β1 MarketReturnd-1+ β2MarketReturnd+ β3 IndustryReturni,d-1 + β4 IndustryReturni,d + ei,d

          where Returni,d is firm i’s return on day d
          MarketReturnd is the value-weighted market return for day d
          IndustryReturni,d is the firm industry value-weighted return excluding firm i’s daily return

          I have to step by step try to find out the way to solve it. My data in the attachment.
          First, I think that I have to run regression by yearly. Find the way to pick up IndustryReturn., etc.... To be honest, I am still confused.
          I would be grateful if you could give me some advice.
          Thank you so much.

          Attached Files


          • #6
            Please note advice in 12.5 on spreadsheet attachments.

            What I am picking up from #5 is that you are thinking of separate regressions for each year. If so, then the advice in #3 still holds: there is no need to split your data into different datasets.

            The forum includes hundreds of posts on regression for return data.


            • #7
              @Nick Cox
              Thank you for your reminder. I will take the time to read it.