Announcement

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

  • Replacing with a string variable

    Hi all,

    I am getting a type mismatch when I want to replace a variable with a string variable.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int deal_id float date_deal
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    1 .
    end
    I am trying
    Code:
    replace date_deal = "12oct2006" if deal_id == 1
    But I get a type mismatch. Is this because I am attempting to create a string variable?
    Thanks,
    Chris

  • #2
    Yes indeed. replace won't change storage type in this way. But there is essentially no point to storing dates as strings any way. You can't do much with them. They won't even sort in the order you want. So, you should replace with mdy(10, 12, 2006) or whatever.

    Comment


    • #3
      Thanks for your reply.

      Code:
      replace date_deal = date("12oct2006", "DMY") if deal_id == 1
      Using that code gives me


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int deal_id float date_deal
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      1 17086
      end
      A value of 17086, which isn't workable. Is there a program I should install for it to work properly?

      Comment


      • #4
        You need to format date_deal :

        Code:
        format date_deal %td
        list
        Stata/MP 14.1 (64-bit x86-64)
        Revision 19 May 2016
        Win 8.1

        Comment


        • #5
          Chris: Earlier today you used date display formats given advice in https://www.statalist.org/forums/for...dmmyyyy-format

          If you didn't understand what they did

          help format
          help datetime display formats

          Comment

          Working...
          X