Announcement

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

  • from string variable to data variable

    Good evening,
    I have a string variable setted in this way
    Code:
    27-JAN-2022 08:19:43
    , how can I convert it data variable?
    I had already broke it in two part and managed until days, but I still have some problems with hours...

    Code:
    gen DAYstring = word( StartDate, 1)
    gen DAYdate = date(DAYstring, "DMY")
    
    gen Hstring = word( StartDate, 2) 
    gen hdate = clock(Hstring)
    but the last command return me some errors, I am sure that there is a quickly way without break the variable.

    Many thanks in advance for your time

  • #2
    Chiara:
    you may want to try:
    Code:
    . generate double wanted = clock( date , "DMYhms")
    
    . format wanted %tc
    
    . list
    
         +-------------------------------------------+
         |                 date               wanted |
         |-------------------------------------------|
      1. | 27-JAN-2022 08:19:43   27jan2022 08:19:43 |
         +-------------------------------------------+
    
    .
    Kind regards,
    Carlo
    (StataNow 18.5)

    Comment


    • #3
      The first calculation is correct but can be compressed. Otherwise the errors here arise from not assigning a double for the date-time variable and not specifying a second argument for clock(). See help datetime and search for double in the text.


      Code:
      clear
      input str21 StartDate
      "27-JAN-2022 08:19:43"
      end
      
      gen ddate = daily(word(StartDate, 1), "DMY")
      format ddate %td
      
      gen double ctime = clock(StartDate, "DMY hms")
      format ctime %tc
      
      gen double time_of_day = clock(word(StartDate, 2), "hms")
      format time_of_day %tcHH:MM:SS
      
      list
      
           +------------------------------------------------------------------+
           |            StartDate       ddate                ctime   time_o~y |
           |------------------------------------------------------------------|
        1. | 27-JAN-2022 08:19:43   27jan2022   27jan2022 08:19:43   08:19:43 |
           +------------------------------------------------------------------+
      Last edited by Nick Cox; 31 Jan 2022, 13:28.

      Comment


      • #4
        Dear Cox and Lazzaro,
        many thanks for your time and help.
        Now it works!
        Wishing you a good day!!

        Comment


        • #5
          I have another question concerning the same variable: I need to created new class or operation depending on this dates:
          for example

          Code:
          generate School = 1 if StartDate < 27-JAN-2022 08:19:43
          I understand that this syntax is not correct because "27-JAN-2022 08:19:43" is like a "label" and I should insert the corresponding number, but I saw that it's also possible to work with this pseudolabel by adding "td" or "tc" like this example
          Code:
          gen age2000 = (td(1jan2000)-birthday)/365.25
          .
          I can't succeed in typing the right syntax...

          Once again many thanks for your time
          Last edited by Chiara Tasselli; 01 Feb 2022, 03:11.

          Comment


          • #6
            Chiara:
            you may want to consider:
            Code:
            . set obs 1
            Number of observations (_N) was 0, now 1.
            
            . g my_date="27-JAN-2022 08:19:43"
            
            . generate double wanted = clock( my_date , "DMYhms")
            
            . format wanted %tc
            
            . g start_date="26-JAN-2022 08:19:43"
            
            . generate double start_date_num = clock( start_date , "DMYhms")
            
            . format start_date_num %tc
            
            . gen whatyouwant=1 if start_date_num<=wanted
            
            . list
            
                 +--------------------------------------------------------------------------------------------------+
                 |              my_date               wanted             start_date       start_date_num   whatyo~t |
                 |--------------------------------------------------------------------------------------------------|
              1. | 27-JAN-2022 08:19:43   27jan2022 08:19:43   26-JAN-2022 08:19:43   26jan2022 08:19:43          1 |
                 +--------------------------------------------------------------------------------------------------+
            
            .
            Kind regards,
            Carlo
            (StataNow 18.5)

            Comment

            Working...
            X