Announcement

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

  • Cannot change string variable to int

    Hi,

    I am struggling to change my date variable from string to an int variable:

    Code:
    input str8 date float(ukis01 ukis02 ukis05 ukis1)
    "07/01/04"           .           .           .           .
    "07/02/04"   -.2180105   -.2180105   -.2180105  -.27911562
    "07/03/04"    .3280379    .3280379    .3280379    .4196255
    "07/04/04"   -.1164731   -.1164731   -.1164731   -.1743723
    "07/05/04"   -.5526805   -.5526805   -.5526805   -.5802588
    "07/06/04"   .25532183   .25532183   .25532183   .23302527
    "07/07/04"     .381967     .381967     .381967    .3566458
    "07/08/04"   -.3521871   -.3521871   -.3521871  -.29046065
    "07/09/04"    .1600098    .1600098    .1600098    .1515166
    "07/10/04"  .016313126  .016313126  .016313126  .026019905
    "07/11/04"  -.11036982  -.11036982  -.11036982  -.14586627
    "07/12/04" -.024156913 -.024156913 -.024156913  -.02692535
    "07/13/04"    .1126608    .1126608    .1126608   .16186243
    "07/14/04"   -.1054983   -.1054983   -.1054983  -.13780381
    "07/15/04"   .16306344   .16306344   .16306344   .20752813
    "07/16/04"  .067925915  .067925915  .067925915    .0958494
    "07/17/04"   .06046037   .06046037   .06046037   .04831808
    "07/18/04"  -.19248436  -.19248436  -.19248436   -.2422607
    "07/19/04"   .08850786   .08850786   .08850786   .11104414
    "07/20/04"  .014100888  .014100888  .014100888    .0443783
    "07/21/04"    .2632147    .2632147    .2632147     .220012
    I am using recast-
    Code:
     recast int date
    date:  int invalid
    r(109);
    Not sure what is going wrong here?

  • #2
    What is going wrong is that you can't recast across the string-numeric divide. You have to go just one small step beyond the help to read this by clicking on Remarks and Examples:

    Note that recast is not a command to change, or to map, string variables to numeric variables or numeric variables to string variables.
    recast can't help you with dates in any circumstance I can recall. (Dates aren't a variable or storage type in Stata.)

    More generally, there is no way to work with dates in any software without some understanding of how dates are held in that software.

    In Stata that means reading or at least consulting

    Code:
    help datetime
    but in your case you can make a good start with

    Code:
    gen ndate = daily(date, "MDY", 2030) 
    format ndate %td

    Comment


    • #3
      Nick gave you the code you need to accomplish what you asked in post #1. He also pointed you to the help file that would explain the command.

      I want to strongly encourage you to further improve your understanding of how dates and times are handled in Stata.

      Stata's "date and time" variables are complicated and there is a lot to learn. If you have not already read the very detailed Chapter 24 (Working with dates and times) of the Stata User's Guide PDF, do so now. If you have, it's time for a refresher. After starting with that to obtain a thorough understanding, the help datetime documentation will usually be enough to point the way. You can't remember everything; even the most experienced users end up referring to the help datetime documentation or back to the manual for details. But at least you will get a good understanding of the basics and the underlying principles. An investment of time that will be amply repaid.

      All Stata manuals are included as PDFs in the Stata installation and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

      Comment


      • #4
        Thank you William and Nick, that was super helpful!

        Comment

        Working...
        X