Announcement

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

  • Matching dates of variables

    I have a set of variables data but variables observations are with different length of time, i.e.
    Date 1 Variable 1 Date 2 Variable 2
    1-1-2001 2 1-1-2001 13
    2-1-2001 3 2-1-2001 23
    3-1-2001 6 4-1-2001 12
    4-1-2001 8 5-1-2001 8
    5-1-2001 11 7-1-2001 5
    I would like to run a regression but the their dates are not in parallel, how can I solve this.

  • #2
    I'm not sure I understand what you want. Perhaps you mean that you would like to regress the value of variable 1 on the value of variable 2 from the same date? If so, you need to reorganize the data first.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float date1 byte variable1 float date2 byte variable2
    14976  2 14976 13
    15007  3 15007 23
    15035  6 15066 12
    15066  8 15096  8
    15096 11 15157  5
    end
    format %td date1
    format %td date2
    
    gen long obs_no = _n
    reshape long date variable, i(obs_no) j(_j)
    drop obs_no
    reshape wide variable, i(date) j(_j)
    
    regress variable1 variable2
    Note: The code assumes that your dates are month-day-year (they could be day-month-year, no way to tell from what's shown) and that they are Stata internal format numeric variables. If they are just strings that look like dates to human eyes, you will have to convert them first. You won't be able to do this in any sensible way with string variables for dates.

    In the future, when showing data examples, please use the -dataex- command to do so, as I have done in this response. If you are running version 15.1 or a fully updated version 14.2, it is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    When asking for help with code, always show example data. When showing example data, always use -dataex-.




    Comment

    Working...
    X