Announcement

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

  • Doing Merge using rangejoin

    Hello, I am a new member to this forum! Nice to meet you all.

    I have just went through set of posts regarding rangejoin, but I could not apply it to my own data project, and all helps would be very grateful!

    So, I have two data sets where

    use dataSet1

    PERMNO FIRSTDATE LASTDATE
    25881 19701113 19780630
    10015 19830920 19860731
    10023 19721214 19730605


    and
    use dataSet2
    PERMNO TargetDate
    25881 19701220
    10015 19840305
    10023 19721216

    and on.
    so, using dataset 1, I ran the following code
    (Both my FirstDate and LastDate are in long format)

    rangejoin NamesDate FirstDate LastDate using dataSet2.dta, by (PERMNO)
    but I am getting
    no observation with valid interval bounds to use as my error message. Would anyone be willing to help me out? Thank you all.

  • #2
    Welcome to Statalist.

    I note that your rangejoin command specifies NamesDate, but your dataSet2 show TargetDate. Perhaps that is the problem.

    I suspect that the dates you show in dataSet1 are simple numbers rather than Stata Internal Format daily dates displayed with date formats. That would be a mistake in the long run - see the comments at the end - but should not prevent rangejoin from working. And when I run the following code, as you can see, rangejoin works.
    Code:
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input int permno long(firstdate lastdate)
    
           permno     firstdate      lastdate
      1. 25881 19701113 19780630
      2. 10015 19830920 19860731
      3. 10023 19721214 19730605
      4. end
    
    . tempfile ds1
    
    . save `ds1'
    file /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_23590.000001 saved
    
    . 
    . * Example generated by -dataex-. To install: ssc install dataex
    . clear
    
    . input int permno long targetdate
    
           permno    targetdate
      1. 25881 19701220
      2. 10015 19840305
      3. 10023 19721216
      4. end
    
    . tempfile ds2
    
    . save `ds2'
    file /var/folders/xr/lm5ccr996k7dspxs35yqzyt80000gp/T//S_23590.000002 saved
    
    . 
    . use `ds1', clear
    
    . rangejoin targetdate firstdate lastdate using `ds2', by (permno)
      (using rangestat version 1.1.1)
    
    . list, abbreviate(12)
    
         +--------------------------------------------+
         | permno   firstdate   lastdate   targetdate |
         |--------------------------------------------|
      1. |  25881    19701113   19780630     19701220 |
      2. |  10015    19830920   19860731     19840305 |
      3. |  10023    19721214   19730605     19721216 |
         +--------------------------------------------+
    
    .
    Now, my comments on dates. Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After that, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

    All Stata manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

    At this point I am guessing that the date in dataSet2 is stored differently than the dates in dataSet1, and that is why rangejoin is unable to succeed. Any of these dates that are not SIF daily dates as explained in the recommended reading should be converted to SIF daily dates.

    And some comments on Statalist. If what I've written, and what anyone else may write, doesn't lead you to a solution, you should take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It's particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

    Particularly with date and time data, using dataex to post sample data is crucial.

    The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

    Comment

    Working...
    X