Announcement

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

  • Creating Dummy Variable r(109) error

    Hi Everyone,

    I am trying to solve r(109) error after used the following code.

    Could someone please help me to fix this problem?


    type: byte

    code:

    gen Quality_Cert=0
    replace Quality_Cert=1 if QualityCertificate="Yes"

  • #2
    Use == for comparison, use = for assigning a value.
    Code:
    replace Quality_Cert=1 if QualityCertificate=="Yes"

    Comment


    • #3
      Hi,

      Thank you very much but I received the same error (r109) for the following code:
      replace Quality_Cert=1 if QualityCertificate=="Yes"

      Comment


      • #4
        Please show example data. Use the -dataex- command to do that. If you are running version 17, 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

        Comment


        • #5
          Well, that means you have made two errors, and I only saw the syntax error in your code.

          I see that on your three previous topics nobody has advised you about the preferred way of presenting problems on Statalist.

          Please take a few moments to review the Statalist FAQ linked to from the top of the page, as well as from the Advice on Posting link on the page you used to create your post. Note especially sections 9-12 on how to best pose your question. It is particularly helpful to copy commands and output from your Stata Results window and paste them into your Statalist post using code delimiters [CODE] and [/CODE], and to use the dataex command to provide sample data, as described in section 12 of the FAQ.

          The more you help others understand your problem, the more likely others are to be able to help you solve your problem.

          With that said, I am now going to guess what your other problem might be, and it is a guess because you gave no example data and you did not include the diagnostic error message that appeared on the line before r(109).

          In post #1 you have

          type: byte
          I will guess that you are describing your QualityCertificate variable, and in particular telling us that it is a numeric variable, because string variables would be a type starting with str. For details see
          Code:
          help data types
          So you have a numeric variable, but when you view it, it is displayed as "Yes" (or presumably "No") rather than as a number. That is because it has a value label assigned that displays numeric values as strings. For details see
          Code:
          help value label
          If you run
          Code:
          codebook QualityCertificate
          you will see what the values are, and what their labels are. For details see
          Code:
          help codebook
          and use it to be sure you understand your data before you set about writing code to make use of it.

          The values of QualityCertificate are perhaps 0 and 1 labelled "No" and "Yes", respectively, because that is the most common way of coding No and Yes. So your Stata code could become
          Code:
          gen Quality_Cert=0
          replace Quality_Cert=1 if QualityCertificate==1
          But you don't need to do that because QualityCertificate is already coded as 0 or 1.

          Added as I finish: note that my advice mirrors that from Clyde, which was posted while I was writing this. So if my guess isn't sufficient to guide you to a solution, please follow his and my advice.

          Comment


          • #6
            Much appreciated. These notes are really helpful. Thank you very much.

            Comment


            • #7
              A roundabout two-step for indicator variables (some say dummy variables: more about that in the second link below) is curiously popular.


              Code:
              gen female = 0 
              replace female = 1 if sex == "female"
              Such solutions miss a direct alternative

              Code:
              gen female = sex == "female"
              A one-liner remains possible if the wish is to separate missing values directly

              Code:
              gen female = sex == "female" if !missing(sex)
              See also

              https://www.stata.com/support/faqs/d...rue-and-false/

              https://www.stata-journal.com/articl...article=dm0099

              https://www.stata-journal.com/articl...article=dm0087

              Comment


              • #8
                Nick Cox -

                On the other hand, the two-command syntax shown in these posts is an improvement on the even more popular one command version that omits the first command initializing the result to zero, thereby creating what is truly a "dummy" variable in that it is coded 1/missing and this is always "true" in Stata's interpretation of Boolean arithmetic defined on a field of more than two values.

                The two-command syntax is popular, I hypothesize, for a number of reasons not unlike the reasons that elementary Stata string functions are more popular than the regular expression functions I find so elegant.

                Comment

                Working...
                X