Announcement

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

  • converting float variable to integer

    Hi,

    I'd like to convert a float variable to an integer. I've tried "recast int var" and it did not work. Can someone please help?

  • #2
    "Did not work" is not helpful. There is only one way a command can "work," but a large number of ways it can fail. Did you get an error message? If so, what was it? Did Stata crash? Did it run but produce results different from what you expected? (If so, how did the results differ from what you expected?) Usually the best way to pose a question like this is to show, by direct copy and paste from the Results window or your log file, the exact command you typed and the exact output/results that Stata gave you. It is also usually help to provide example data the exhibits the problem.

    Comment


    • #3
      in addition to what Clyde Schechter said, all of which I agree with, you might want to try
      Code:
      compress var
      as it sometimes gives more informative messages; see
      Code:
      help compress

      Comment


      • #4
        Code:
        recast int varname
        Will issue a warning when values cannot be converted from float/double to int. If there are no issues, then nothing is displayed. -compress- would also automatically recast from float/long to byte/int/long as long as no issues such as a decimals are detected, which is Rich's suggestion.

        Since it is likely you do not have only integer values, you can explore them with something like the following, where you replace x with your own variable name.

        Code:
        list x if mod(x,1) != 1
        tab x if mod(x,1) != 1

        Comment


        • #5
          After running compress numsexpart, it is still stored as a float variable. I've copy and pasted the output below.


          compress numsexpart
          (0 bytes saved)

          . describe numsexpart

          storage display value
          variable name type format label variable label
          -------------------------------------------------------------------------------------------------
          numsexpart float %3.0f How many sexual partners have you had




          I also ran recast int numsexpart and again, it still saved as a float variable.


          recast int numsexpart
          numsexpart: 5 values would be changed; not changed

          . describe numsexpart

          storage display value
          variable name type format label variable label
          -------------------------------------------------------------------------------------------------
          numsexpart float %3.0f How many sexual partners have you had



          It also appears that they are all integer values?


          tab numsexpart if mod(numsexpart,1) != 1

          How many |
          sexual |
          partners |
          have you |
          had | Freq. Percent Cum.
          ------------+-----------------------------------
          0 | 5 4.76 4.76
          1 | 10 9.52 14.29
          2 | 8 7.62 21.90
          3 | 3 2.86 24.76
          4 | 4 3.81 28.57
          5 | 9 8.57 37.14
          6 | 5 4.76 41.90
          7 | 5 4.76 46.67
          8 | 5 4.76 51.43
          10 | 12 11.43 62.86
          11 | 2 1.90 64.76
          12 | 2 1.90 66.67
          15 | 3 2.86 69.52
          16 | 1 0.95 70.48
          17 | 1 0.95 71.43
          18 | 1 0.95 72.38
          20 | 7 6.67 79.05
          22 | 1 0.95 80.00
          23 | 2 1.90 81.90
          24 | 1 0.95 82.86
          25 | 3 2.86 85.71
          30 | 4 3.81 89.52
          40 | 1 0.95 90.48
          50 | 1 0.95 91.43
          52 | 1 0.95 92.38
          70 | 1 0.95 93.33
          75 | 2 1.90 95.24
          100 | 3 2.86 98.10
          250 | 1 0.95 99.05
          300 | 1 0.95 100.00
          ------------+-----------------------------------
          Total | 105 100.00

          .

          Comment


          • #6
            This is a better view of the tab numsexpart if mod(numsexpart,1) != 1 command
            Attached Files

            Comment


            • #7
              There is a difference between what you see on screen and what is stored inside the computer. What counts is what is stored inside the computer, not what is shown on the screen. The display format %3.0f of your variable hides all fractions. Given the command you executed those numbers you see have to be non-integers, it is just the display format that hides the fractional part. So type in Stata

              format numsexpart %9.3f

              and repeat that tab command again. This won't solve your problem, but allows you to diagnose what the problem might be.
              ---------------------------------
              Maarten L. Buis
              University of Konstanz
              Department of history and sociology
              box 40
              78457 Konstanz
              Germany
              http://www.maartenbuis.nl
              ---------------------------------

              Comment


              • #8
                My apologies, I had a typo in #4 that may have led you a bit astray. The if qualifier should have been

                Code:
                mod(x,1) != 0
                Which means, the remainder after repeatedly dividing out by 1 is not equal to zero (i.e., a fraction).

                The following result you showed in #5 shows you do have 5 non-integer values which you can try to resolve.

                Code:
                . recast int numsexpart
                numsexpart: 5 values would be changed; not changed
                Perhaps a cleaner way to do the same thing:

                Code:
                clear
                input float x
                2.4
                4.8
                  3
                  .
                end
                
                list if int(x) != x
                Result

                Code:
                     +-----+
                     |   x |
                     |-----|
                  1. | 2.4 |
                  2. | 4.8 |
                     +-----+

                Comment

                Working...
                X