Announcement

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

  • Error when using generate to copy variable

    Hi,

    I am failing at a surprisingly simple task. In a csv file I have a date variable (mess_datum) in the format yyyymmdd. When I import the csv to Stata (15), the variable will be imported in long format and displayed correctly. Then all I want is copy this variable, so what I do is just
    Code:
    gen date = mess_datum
    But then, for example for all the values 19470123, 19470124, and 19470125 in mess_datum, I will get the value of 19470124 in the new variable date. The pattern repeats so that for three days I get only one common value.
    I figured out that clonevar does the trick but I am still wondering what went wrong with generate? I tried several things, for example changing mess_datum to double format first, but no success.

    Thanks for your help!

    Click image for larger version

Name:	Unbenannt.PNG
Views:	1
Size:	2.6 KB
ID:	1504212


  • #2
    With your syntax the default storage type is float -- which is inadequate for holding such integers accurately. You need to spell out long.

    You have another problem. Dates like that are useless for most Stata purposes, as can be seen by thinking about 19471231 and 19480101. People can make sense of those as successive daily dates but Stata sees only integers that differ by much more than 1:

    Code:
    . display 19480101 - 19471231
    8870


    You need to convert to numeric date variables.

    Code:
    .. clear
    
    . set obs 3
    number of observations (_N) was 0, now 3
    
    . gen long date = 19470122 + _n
    
    . list
    
         +----------+
         |     date |
         |----------|
      1. | 19470123 |
      2. | 19470124 |
      3. | 19470125 |
         +----------+
    
    .
    . gen betterdate = daily(string(date, "%8.0f"), "YMD")
    
    . format betterdate %td
    
    .
    . list
    
         +----------------------+
         |     date   betterd~e |
         |----------------------|
      1. | 19470123   23jan1947 |
      2. | 19470124   24jan1947 |
      3. | 19470125   25jan1947 |
         +----------------------+
    PS Please don't show data as images. Show data using dataex as the FAQ Advice you were asked to read does explain.

    Comment


    • #3
      Thank you for the quick and helpful response. Specifying "long" with generate solved the problem and using the date format is a good suggestion.
      Thanks!

      Comment


      • #4
        Also have a look at clonevar:
        https://www.stata.com/manuals13/dclonevar.pdf

        Comment

        Working...
        X