Announcement

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

  • Convert float to integer

    Hi, I have a variable name "age" However, there are values like 6.01. How will I convert all 6.01 to just 6? Thank you.

  • #2
    So, it depends on how you want to handle values like 6.6. Do you want that to go to 6, or to 7? There are different ways, that work differently:


    Code:
    input x
    6
    6.2
    6.5
    6.7
    7
    end
    
    gen y1 = floor(x)
    gen y2 = ceil(x)
    gen y3 = round(x)
    list, noobs clean

    Comment


    • #3
      Nice. Thank you!

      Comment


      • #4
        Many would agree that from the body of your post one can guess what you want, but the body _with_ the title, strictly speaking, is ambiguous.

        Pedantically, notice the following:

        1) float and int are storage types in Stata. Clyde's code will shave off decimals but variables will still have storage type float (the default) [nothing wrong with that; see -help float- anyway]:

        Code:
        . clear
        
        . set more off
        
        .
        . input x
        
                     x
          1. 6
          2. 6.2
          3. 6.5
          4. 6.7
          5. 7
          6. end
        
        .
        . gen y1 = floor(x)
        
        . gen y2 = ceil(x)
        
        . gen y3 = round(x)
        
        .
        . describe
        
        Contains data
          obs:             5                          
         vars:             4                          
         size:            80                          
        ----------------------------------------------------------------------------
                      storage  display     value
        variable name   type   format      label      variable label
        ----------------------------------------------------------------------------
        x               float  %9.0g                  
        y1              float  %9.0g                  
        y2              float  %9.0g                  
        y3              float  %9.0g                  
        ----------------------------------------------------------------------------
        2) From -help data types-: "Numbers are stored as byte, int, long, float, or double, with the default being float. byte, int, and long are said to be of integer type in that they can hold only integers."

        3) You can create integer types, specifying so with the -gen- command:

        Code:
        . gen int y1 = floor(x)
        
        . gen int y2 = ceil(x)
        
        . gen int y3 = round(x)
        
        .
        . describe
        
        Contains data
          obs:             5                          
         vars:             4                          
         size:            50                          
        ----------------------------------------------------------------------------
                      storage  display     value
        variable name   type   format      label      variable label
        ----------------------------------------------------------------------------
        x               float  %9.0g                  
        y1              int    %8.0g                  
        y2              int    %8.0g                  
        y3              int    %8.0g                  
        ----------------------------------------------------------------------------
        4) Be careful when doing just that, as different storage types can hold more or less numbers. Again see, -help data types-.

        The whole purpose of this, is really nothing more than to point out some linguistic features of your post in relation to Stata terminology. Maybe the clarification will prove helpful in the future.


        You should:

        1. Read the FAQ carefully.

        2. "Say exactly what you typed and exactly what Stata typed (or did) in response. N.B. exactly!"

        3. Describe your dataset. Use list to list data when you are doing so. Use input to type in your own dataset fragment that others can experiment with.

        4. Use the advanced editing options to appropriately format quotes, data, code and Stata output. The advanced options can be toggled on/off using the A button in the top right corner of the text editor.

        Comment

        Working...
        X