Announcement

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

  • R (109) error code

    Hello all,

    apologies if this is a silly Q but I am a very newbie in Stata and just learning it!

    I wish to use the following commands :

    gen month=month(date)
    gen year=year(date

    gen order_month=01 if month==03 & year==2016
    replace order_month=02 if month==04 & year==2016
    replace order_month=03 if month==05 & year==2016


    I've used the: gen month=month(date) & gen year=year(date) to generate the month and year but when I am trying to use the rest ones I get an error R(109) saying :

    " In an expression, you attempted to combine a string and numeric
    subexpression in a logically impossible way. For instance, you
    attempted to subtract a string from a number or you attempted
    to take the substring of a number."

    the month variable is a str2 type and the year str4 type if that helps.

    I don't know what I am doing wrong and how to resolve it.

    Apologies again if this is a silly Q. If anyone wishes to spend some time and help me I really appreciate it.

    thank you,
    Maria

  • #2
    if those are really string variables then their values (e.g., 03 or 2016) need to be surrounded by double quotation marks; further, note that unless you use a special format, you will not get numeric values of your new variable with a leading "0"; if you want it to be a string you need to tell Stata that; in addition to reading the FAQ, please see
    Code:
    help generate
    help format

    Comment


    • #3
      Thank you very much for your response. I will read it.

      Comment


      • #4
        Let me add to Rich's advice the following.

        The month() and year() functions assume the argument is a numeric Stata Internal Format date. If date is a string variable, you have some work to do.

        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 that, 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 (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu.

        Here is some sample code that may point you toward an effective solution to your problem.
        Code:
        . list
        
             +-----------+
             |      var1 |
             |-----------|
          1. | 12sep2016 |
          2. |  8jul2017 |
             +-----------+
        
        . generate date = daily(var1,"DMY")
        
        . format date %td
        
        . generate dateym = mofd(date)
        
        . format dateym %tm
        
        . generate order_month = dateym - tm(2016-03) + 1
        
        . list, abbreviate(12)
        
             +----------------------------------------------+
             |      var1        date   dateym   order_month |
             |----------------------------------------------|
          1. | 12sep2016   12sep2016   2016m9             7 |
          2. |  8jul2017   08jul2017   2017m7            17 |
             +----------------------------------------------+
        
        .

        Comment


        • #5
          Dear William,

          Apologies for the late reply and thank you very much for taking the time to respond. I've realized that I have a lot to learn especially for the dates .

          Once again thank you.
          Have a good day,
          Maria

          Comment

          Working...
          X