Announcement

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

  • Convert tex numeric variable without value label to string variable

    Hi Statalisters,

    I have a basic question. I want to convert variable "srcdate" from numeric to string. However, because this numeric variable is text-form without any value label, so I cannot use -decode- and -tostring- code. Does anyone have a more decent solution?

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long srcdate
    19358
    21549
    21184
    18382
    19358
    18627
    18992
    18474
    18839
    21549
    end
    format %d srcdate
    Thanks in advanced.

  • #2
    Hi, when I use -dataex- to demonstrate the data, above, the observations of srcdate are shown in messy code. The correct form that I see in my Stata is the following.

    Click image for larger version

Name:	1583408235614.jpg
Views:	1
Size:	76.2 KB
ID:	1539826

    Comment


    • #3
      What you saw is not "messy code", it is the numbers that Stata uses internally (the days since January 1st 1960). The line format %d srcdate controls how those numbers are displayed, in your case it is a date. So 19,358 days after January 1st 1960 is 31st December 2012. Once you have your dates in Stata readable form you almost never want to reverse that. So the big question is: Why do you want to turn this into strings? Maybe we can find an easier, saver way of achieving your final goal.
      ---------------------------------
      Maarten L. Buis
      University of Konstanz
      Department of history and sociology
      box 40
      78457 Konstanz
      Germany
      http://www.maartenbuis.nl
      ---------------------------------

      Comment


      • #4
        Originally posted by Maarten Buis View Post
        What you saw is not "messy code", it is the numbers that Stata uses internally (the days since January 1st 1960). The line format %d srcdate controls how those numbers are displayed, in your case it is a date. So 19,358 days after January 1st 1960 is 31st December 2012. Once you have your dates in Stata readable form you almost never want to reverse that. So the big question is: Why do you want to turn this into strings? Maybe we can find an easier, saver way of achieving your final goal.
        Hi, Maarten. I want to merge this dataset with another dataset that contains srcdate variable in string form.

        Comment


        • #5
          I tried the following code but it does not work.

          gen srcdate_s = date(srcdate, "DMY")

          Comment


          • #6
            Hi guys, I made it. Here is the code.

            generate srcdate_s = string(srcdate,"%td")

            Comment


            • #7
              I think that that is a very dangerous solution, so I would not do that. The problem is that this requires the string in the other dataset always follows the exact same convention. There are so many ways that this can go wrong. "31dec2015" is not the same as "31Dec2015" and not the same as "31 dec 2015" and not the same as "31dec2015 ", etc. etc. etc. The safer solution is to transfer the string in the other dataset to Stata readable format using the date() function. The result will be the "messy code" you saw before. This is a much safer identifier for date, then a a string.
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment

              Working...
              X