Announcement

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

  • Separate dataset by one variable

    Hi all

    How can I separate dataset by variable yearmonth. Which mean save the same year observations in a dataset. (For example, save the dataset below as 4 datasets.)

    Best,

    Jack Liang


    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(yearmonth id)
    201501 1
    201512 2
    201601 3
    201603 3
    201709 4
    201711 5
    201801 6
    201802 7
    201803 7
    201804 8
    end
    ------------------ copy up to and including the previous line ------------------







  • #2
    Code:
    ssc desc savesome

    Comment


    • #3
      I think the simplest way to do this is:

      Code:
      numdate monthly mdate = yearmonth, pattern("YM")
      gen year = year(dofm(mdate))
      drop yearmonth
      
      capture program drop save_one_year
      program define save_one_year
          local year = year[1]
          sort id mdate
          save data_`year', replace
          exit
      end
      
          
      runby save_one_year, by(year)
      The first obstacle is that you need a real date variable, not a floating point number that human eyes see as a monthly date but that Stata would not handle in a sensible way. -numdate-, by Nick Cox and available from SSC makes short work of that. Then you have to extract the year from that. Then you just loop over years to save each year's data in a separate file. I also chose to sort the data by id and date within each file on the assumption that it would be convenient to have it that way, but that is not a necessary part of the code. There are other ways to loop, but -runby-, written by Robert Picard and me, and also available from SSC, will be particularly fast and efficient for this if your real data set is appreciably large.

      Added: Crossed with #2. You will have to embed that in your own loop over years.

      Comment


      • #4
        Thank you all!

        Comment

        Working...
        X