Announcement

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

  • Shorter values and match

    Good morning everyone,

    I would kindly ask for help on two related matters.

    First, I have a date variable that looks like this: 2021-08-27 14:31:47. I'd like to split it in date (2021-08-27) and time (14:31:47), basically creating two separate variables.

    Second, I have a separate dataset with values associated with each date, and I would like to match them by Date. Below a summary of my two dataset and what I'd like to obtain after matching by Date. I'd like to bring the values of Dataset B into a new variable in Dataset A.

    Thank you in advance,

    Cristiano

    Dataset A
    Date
    2021-08-27
    2021-08-27
    2021-08-27
    2021-08-28
    2021-08-29
    2021-08-30

    Dataset B
    Date Value to match
    2021-08-27 100
    2021-08-28 110
    2021-08-29 115
    2021-08-30 95

    Match (desired)
    Date Value
    2021-08-27 100
    2021-08-27 100
    2021-08-27 100
    2021-08-28 110
    2021-08-29 115
    2021-08-30 95

  • #2
    Please

    use dataex to make it clear whether your date variables are strings or numeric with date formats

    include an example of a date-time variable such as you describe but do not exemplify
    Last edited by Nick Cox; 03 Dec 2022, 12:20.

    Comment


    • #3
      Hi Nick,
      this is the date variable. Basically I'd like to keep the empty values as empty, and create two vars with date and time.

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input str26 created_date                    
      ""                                                                          
      ""                          
      "2021-08-27 14:31:47"       
      "2022-01-11 20:04:50.105462"
      end
      The dataset B will have the date formatted properly (e.g. 2021-08-27) and will have another column with the numeric values I want to bring onto the other table (e.g. 100.15).

      Thank you,

      Cristiano

      Comment


      • #4
        Your daily date numeric variable is just


        Code:
        gen ddate = daily(word(created_date, 1), "YMD")

        Comment


        • #5
          Thank you, Nick. The code works.

          Would you happen to know how to match with the other dataset?
          Last edited by Cristiano Bellavitis; 03 Dec 2022, 12:54.

          Comment


          • #6
            I imagine that you mean to merge the datasets on daily date.

            Comment


            • #7
              I think I found the solution, thank you.
              Last edited by Cristiano Bellavitis; 03 Dec 2022, 14:58.

              Comment

              Working...
              X