Announcement

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

  • How to generate a new variable to convert data of acres unit variable to hectares unit? (1 acre=0.4hectare)

    The variable on acres unit was a string variable. I encoded it and created a new numeric (float) variable. I have data in acres and it is "blank" in some observations. replaced the blank values("") values to 0. Created a new hectare_variable=0.4. Formated it to %3.0g. Then multiplied acrea_unitvariable*hectare variable. But, the values are not correct.

  • #2
    Try this
    Code:
    gen hectare_variable = 0.4*acres_variable

    Comment


    • #3
      Originally posted by Joro Kolev View Post
      Try this
      Code:
      gen hectare_variable = 0.4*acres_variable
      I have tried this. But, it does not work. It produces incorrect answers.
      For example for the value of 0 acres it multiplies correctly and the hectare equivalent is 0 but for value of 3*0.4 it gives an answer of 20 instead of 1.2.

      Comment


      • #4
        Show a sample of your data using -dataex- where the problem arises.

        Originally posted by quester just View Post

        I have tried this. But, it does not work. It produces incorrect answers.
        For example for the value of 0 acres it multiplies correctly and the hectare equivalent is 0 but for value of 3*0.4 it gives an answer of 20 instead of 1.2.

        Comment


        • #5
          The reason it produces incorrect answer is found in

          The variable on acres unit was a string variable. I encoded it and created a new numeric (float) variable.
          The result of encode was not what you wanted. The encode command assigns arbitrary numbers to strings, whether or not they can be represented as a number.

          The output of help encode tells us

          Do not use encode if varname contains numbers that merely happen to be stored as strings
          You need to review the output of help destring and then use it to create a correct version of your acres variable.

          Further careful reading of the output of help encode will enlighten you as to what it does. Here is an example.
          Code:
          . encode s1, generate(n1)
          
          . destring s2, generate(n2)
          s2: all characters numeric; n2 generated as double
          (1 missing value generated)
          
          . order s1 n1 s2 n2
          
          . list
          
               +-----------------------+
               |  s1    n1    s2    n2 |
               |-----------------------|
            1. | 1.4   1.4   1.4   1.4 |
            2. | dog   dog           . |
               +-----------------------+
          
          . list, nolabel
          
               +----------------------+
               |  s1   n1    s2    n2 |
               |----------------------|
            1. | 1.4    1   1.4   1.4 |
            2. | dog    2           . |
               +----------------------+
          
          .
          Last edited by William Lisowski; 07 Oct 2020, 08:01.

          Comment

          Working...
          X