Announcement

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

  • Combine two date variables colomn wise.

    Hi there,
    Dont know if anyone has faced a similar issue and can help,
    When i combine two date variables with this code;
    egen z = concat( date1 date2 ),format (%td)
    I get a new variable, z, with points"." at the start and end of the new variable.
    -------------+
    | z |
    |-------------|
    1. | .04sep2014 |
    2. | .17mar2015 |
    3. | .08apr2015 |
    4. | .30apr2015 |
    5. | .16jun2015 |
    |-------------|
    6. | 18aug2015. |
    7. | 25aug2015. |
    8. | 02sep2015. |
    9. | .08sep2015 |
    10. | 01sep2015. |

    The new variable can nolonger function as a date variable.
    This happens even when I start by coverting the original variables to string.

    Thank you

  • #2
    Hello Dorothy,

    I'm a bit confused at the moment with what you are asking for. First, assuming that your -egen concat()- works as it should, you still would not be able to use the new variable as a date variable. Second, I am not finding that the issue you are reporting is happening when I try something similar.

    Keep in mind, I made up an example dataset. You did not provide a sample data set, so I am not sure if your issue appears specifically because of the way your data set is arranged. To provide a sample data set, use -dataex- (See http://www.statalist.org/forums/help#stata ). It is also useful to use the code delimiters when you present Stata code our output.

    Regardless, I am not running into the issue you describe. Here is the code I ran.
    Code:
    clear
    set obs 10
    gen date1 = td(16mar2017)+_n
    gen date2 = td(16apr2020)+_n
    format date1 date2 %td
    
    egen z = concat(date1 date2), format(%td)
    And the results of the -egen- are as follows.
    Code:
    . list z
    
         +--------------------+
         |                  z |
         |--------------------|
      1. | 17mar201717apr2020 |
      2. | 18mar201718apr2020 |
      3. | 19mar201719apr2020 |
      4. | 20mar201720apr2020 |
      5. | 21mar201721apr2020 |
         |--------------------|
      6. | 22mar201722apr2020 |
      7. | 23mar201723apr2020 |
      8. | 24mar201724apr2020 |
      9. | 25mar201725apr2020 |
     10. | 26mar201726apr2020 |
         +--------------------+
    I don't think that this output is what you want. "17mar201717apr2020" does not make any sense as a date variable, and it seems that you want to use z as a date variable. Could you explain what you want z to contain, and what you want to do with z? Perhaps there is a better way to get to what you want.

    Comment

    Working...
    X