Announcement

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

  • String Into numeric

    I have a date Variable, Date = 07-april-2022 which is in string format and I want to convert that string variable into numeric,

    I am using command

    gen date= date(date1, "DMY")

    to convert date string into numeric but I am getting missing all observation.

    help me out.
    Last edited by Pratibha Singh; 07 Apr 2022, 00:25.

  • #2
    I cannot replicate the problem

    Code:
    . display date("07-april-2022", "DMY")
    22742
    
    . display %td date("07-april-2022", "DMY")
    07apr2022
    Please show example data using dataex and actual code.
    Last edited by daniel klein; 07 Apr 2022, 00:41.

    Comment


    • #3
      I have date Variable dob which is in string format, which are given below:

      dob
      02-DEC-2019
      05-DEC-2019
      05-DEC-2019
      01-DEC-2019
      29-NOV-2019
      01-DEC-2019
      03-DEC-2019
      03-DEC-2019
      04-DEC-2019

      the command i am giving for changing string variable into numeric:

      gen dob1 = date(dob, "DMY")
      format dob1%td


      after running this command I got output which looks like

      9 missing values generated

      Comment


      • #4
        That can't be reproduced either.

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str11 dob
        "02-DEC-2019"
        "05-DEC-2019"
        "05-DEC-2019"
        "01-DEC-2019"
        "29-NOV-2019"
        "01-DEC-2019"
        "03-DEC-2019"
        "03-DEC-2019"
        "04-DEC-2019"
        end
        
        . gen wanted = date(dob, "DMY")
        
        . format %td wanted
        
        . l
        
             +-------------------------+
             |         dob      wanted |
             |-------------------------|
          1. | 02-DEC-2019   02dec2019 |
          2. | 05-DEC-2019   05dec2019 |
          3. | 05-DEC-2019   05dec2019 |
          4. | 01-DEC-2019   01dec2019 |
          5. | 29-NOV-2019   29nov2019 |
             |-------------------------|
          6. | 01-DEC-2019   01dec2019 |
          7. | 03-DEC-2019   03dec2019 |
          8. | 03-DEC-2019   03dec2019 |
          9. | 04-DEC-2019   04dec2019 |
             +-------------------------+
        As daniel klein indicated you should use dataex which you should install by

        Code:
        ssc install dataex
        I don't have access any longer to Stata 14 to experiment. A wild guess is that

        Code:
        gen wanted2 = date(lower(dob), "DMY")
        may fix the problem. Perhaps Stata did not like JAN ... DEC back then,

        Comment


        • #5
          Originally posted by Nick Cox View Post
          I don't have access any longer to Stata 14 to experiment. A wild guess is that

          Code:
          gen wanted2 = date(lower(dob), "DMY")
          may fix the problem. Perhaps Stata did not like JAN ... DEC back then,

          That does not seem to be neccessary. Here are results from Stata 12.1

          Code:
          . gen wanted = date(dob, "DMY")
          
          . format %td wanted
          
          . list
          
               +-------------------------+
               |         dob      wanted |
               |-------------------------|
            1. | 02-DEC-2019   02dec2019 |
            2. | 05-DEC-2019   05dec2019 |
            3. | 05-DEC-2019   05dec2019 |
            4. | 01-DEC-2019   01dec2019 |
            5. | 29-NOV-2019   29nov2019 |
               |-------------------------|
            6. | 01-DEC-2019   01dec2019 |
            7. | 03-DEC-2019   03dec2019 |
            8. | 03-DEC-2019   03dec2019 |
            9. | 04-DEC-2019   04dec2019 |
               +-------------------------+

          Comment


          • #6
            Thanks Nick, But it is not working, again I am sharing example using dataex.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input str9 n_dob
            "07-Jan-20"
            "03-Dec-19"
            "27-Nov-19"
            "26-Nov-19"
            "24-Nov-19"
            "03-Dec-19"
            "26-Nov-19"
            "24-Feb-20"
            "02-Dec-19"
            "22-Nov-19"
            "21-Dec-19"
            "12-Jan-20"
            "03-Dec-19"
            "10-Dec-19"
            "23-Nov-19"
            "11-Dec-19"
            "09-Feb-20"
            "10-Dec-19"
            "30-Nov-19"
            "05-Dec-19"
            "20-Feb-20"
            "26-Dec-19"
            "01-Mar-20"
            "30-Jan-20"
            "15-Dec-19"
            "02-Feb-20"
            "13-Jan-20"
            "12-Feb-20"
            "01-Jan-20"
            "31-Jan-20"
            "08-Feb-20"
            "09-Dec-19"
            "08-Jan-20"
            "21-Dec-19"
            "10-Feb-20"
            "10-Feb-20"
            "02-Jan-20"
            "17-Mar-20"
            "19-Dec-19"
            "08-Dec-19"
            "15-Feb-20"
            "30-Jan-20"
            "19-Feb-20"
            "17-Nov-19"
            "02-Feb-20"
            "30-Jan-20"
            "25-Nov-19"
            "05-Feb-20"
            "15-Dec-19"
            "18-Dec-19"
            "01-Feb-20"
            "08-Mar-20"
            "17-Jan-20"
            "29-Nov-19"
            "30-Nov-19"
            "05-Feb-20"
            "09-Feb-20"
            "25-Jan-20"
            end
            I am using This command for the given data
            gen dob = date(n_dob, "DMY")
            (58 missing values generated)

            I tried but still it gives missing values

            Comment


            • #7
              Try

              Code:
              generate wanted = date(n_dob, "DMY", 2020)

              Note that there is a difference between the format 07-Apr-2022, which you have claimed to have in your data, and 07-Apr-22, which you really have in your data.

              Edit: And, if the latest date that you have is indeed 07-Apr-22, then you want

              Code:
              generate wanted = date(n_dob, "DMY", 2022)
              Last edited by daniel klein; 07 Apr 2022, 03:48.

              Comment


              • #8
                I imagine this is now solved, but see how much time and effort was wasted because of inadequate and inaccurate information.

                Comment


                • #9
                  Thanks Nick and Daniel for you patience and kind help. Now Code is running successfully and date is converted numerically from string without giving missing values.

                  Nick Information was accurate, Previously I don't know about the dataex function, I learned from you, after using dataex function date 7-april-2022,
                  converted into 7-april-22

                  Comment


                  • #10
                    We are all pleased that the problem is solved -- but frankly #9 makes little sense.

                    It's a matter of record that #1 did not show any data (inadequate information) and #3 gave an example that did not show the problem (inaccurate information). It was only when you posted the data in #6 that the problem became clear.

                    Every new message prompt asks you to read the FAQ before posting and the FAQ Advice explains about dataex (incidentally, a command not a function). So evidently you didn't do that as requested. Not a big deal, but we're making the point that these requests are in your own best interests.

                    Comment

                    Working...
                    X