Announcement

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

  • how to "align" 2 time series with different dates

    Dear all,

    Let's say I have 2 time series, X and Y

    Y has values for t1, t3, t5.

    X has values for t1, t2, t3, t4, t5

    I want to study the effect of lagged values of X on Y. I guess that for the dates that X has values but Y does not, a missing value should appear?

    How can I import both time series with the respective dates so that I can do that? At the moment I import the time series but they are imported serially, I could not find a way to import the correct values to the correct dates for each time series separately..

    Thanks in advance!


  • #2
    Adhoul,

    Without knowing more about your data, I would suggest the following, which assumes that you have a variable id, that identifies each subject, and a variable t, that identifies the time of the observation (could be an actual date or a sequence number like 1,2,3,4,5):

    Code:
    open dataset_with_x, clear
    merge 1:1 id t using dataset_with_y
    This should produce a data set with id, t, x, and y, one record per time point, with values of y missing for times 2 and 4.

    If your data are not structured in the way that I describe, please provide more details.

    Regards,
    Joe

    Comment


    • #3
      Hi Joe,

      Thanks for your reply and the information.

      I do not currently have an ID for each "subject". My data are time series in daily frequency, thus for every day I have the date on one cell and next to that the returns let's say.

      The problem has to do with e.g. stock markets operating only on Weekdays (variable Y), while some other variable could also have values for e.g. the Weekends (variable X).

      How can I generate this ID on Stata?

      Furthermore, what you are saying would lead to creating a new .dta file for every merge ?

      Thanks

      Comment


      • #4
        Adhoul,

        If your data sets only have one observation per day, then you can do what I suggested, just leave out id and just merge on date. This will yield a new data set in memory that is the combination of the two data sets. You don't have to save it to disk if you don't want to. Try it!

        Regards,
        Joe

        Comment


        • #5
          Joe,

          Thank you! That's what I've been looking for. But this means I will have to create a .dta for each time series variable I want to merge, right ?

          Comment


          • #6
            Adhoul,

            Sorry, I didn't understand your original question. Do you mean that you have each time-series variable in a different data set to start with? If so, you can keep merging in new data sets using a merge command with the same syntax, and end up with one unified data set. If not, or if I am misunderstanding you, please describe exactly how your data are currently structured. (Giving short examples is always helpful.)

            Regards,
            Joe

            Comment


            • #7
              Joe,

              My data are initially in Excel, then I import everything into Matlab where I do the data processing, then I import them in Stata to do mostly of the econometrics analysis..


              I have more then 50 time series, many of which do not have the same dates. For every time series think that I have 2 columns (2 time series), one is the date (daily data) and the other is the actual prices for this date.

              What you suggested works perfectly fine, the only problem being I have to create a .dta file for each one of these time series.

              Comment


              • #8
                Adhoul,

                Yes, it sounds like you will need a data set for each time series. This process can be automated using loops, depending on how your files are named. Moreover, it might be possible to create temporary files for each time series, merge them together into one, and then when you are finished Stata will delete all of the temporary files automatically. If you can provide additional information about the file names for your 50 time series, we can probably help suggest a method.

                Just out of curiosity, do you start out with everything in one Excel spreadsheet, or do you start with 50 spreadsheets? What do you do in Matlab that can't be done in Stata?

                Regards,
                Joe

                Comment


                • #9
                  Hi Joe,

                  Sorry for the delay.

                  I start with around 10 spreadsheets, each containing on average five time series, for 4 subperiods.

                  I do some data processing on Matlab such as aligning the dates, keeping the same dates for some time series, using some econometric models (generating GARCH models etc.) and a few other stuff which can possible be also done on Stata but I find it easier to be done with Matlab which I think is some how better in some programming (I do not really like the ` and ' and all that in Stata which is of course really great in many many things!)

                  Comment

                  Working...
                  X