Announcement

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

  • showing the whole number for numerical variables

    Hi there,

    I have to combine the values of two numerical variables into a value in a new numerical variable, the new one will have 11 or 12 digits. For example, put 8497 and 2171205 together, the new numerical variable should show as 84972171205, but it shows as scientific notation, like 8.497e+10. I tried command "format var %12.0f", it turns out to be 84972167168.

    I cannot do "destring" because this variable has to be numerical, and I do not need this variable get round up or down, because it just means ID number for an observation, is there any command can help me to show the whole number in the data editor without generating a new variable? Just replace the scientific notation with the whole number.

    Thanks a lot!

  • #2
    Hi Bingbin
    Just couple of comments.
    1. What you did is the right way to "see" the content of the variable. using Format, you can change the scientific notation, to show you exactly how many digits you want to see, and how many decimals. In your example that is 12 digits zero decimals.
    2. My guess is you created new id using something like "gen id=var1*100000+var2". Unless you say otherwise, this will create a variable in float format. When you are interested in long digits like the one you indicate, i would create it with formats long or double. "gen long id =var1*1000000+var".
    3. In my experience, when working with long identifiers, it is better to use Strongs, rather than numbers. So perhaps it would be better if you transform your var1 and var2 into strings before you create the longer ID variable. Even Long and Double have "limits" into how many digits can be stored without loss of precision.
    Best
    Fernando

    Comment


    • #3
      Thanks for your reply, FernandoRios, I tried the formats "long", it shows "long not allowed", same problem with double. So I just tried the command "gen id=var1*1000000+var2", but the result still not the one I want to get. For example, var1=3580, var2=3010721, the ID var I get is 3583010816.

      Comment


      • #4
        So what exactly happens when you type this:
        Code:
        gen double id=var1*1000000+var2

        Comment


        • #5
          FernandoRios
          I type:
          replace double id = var1*1000000+ var2 if ***

          I get:
          "double not allowed"

          "replace" is to replace the original id number I typed in
          "if ***" is that only some of the observations that meet the requirement will be assigned for this special id number.

          And do you know any way to show the id number if I want to input the id number manually? Thanks!

          Comment


          • #6
            I see.
            So, you need to "create" a new variable:
            Code:
            drop id
            gen double id = var1*1000000+ var2
            or change the format of id and replace it:
            Code:
            recast double id
            replace double id = var1*1000000+ var2

            Comment


            • #7
              Code:
              egen long id = group(var1 var2), label
              may be an alternative, but the limit in your Stata on the number of value labels may bite.

              You say "this variable has to be numerical" (#1) and you need it to be an identifier: both are satisfied otherwise.

              Comment


              • #8
                Hi Nick,
                that was a very nice trick I wasn't aware of. Thank you
                Fernando

                Comment


                • #9
                  You’re welcome.

                  Code:
                  search dm0034, entry
                  points to a longer discussion.

                  Comment


                  • #10
                    I use the code "recast double id" to fully present my id number with 12 digits without rounding problem, thanks for your guys help!

                    Comment

                    Working...
                    X