Announcement

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

  • How to remove last 3 digits from a numerical variable in STATA

    Hello Everyone! I have a variable HHPBASE, from which I need to chop off last 3 digits, How do I do that? I tried dividing it by 1000 with intention to round it but, after division instead of decimals, the number display has changed. Thanks in advance.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double HHPBASE
    101020101005
    101020102001
    101020103004
    101020104002
    101020105010
    end

  • #2
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input double HHPBASE
    101020101005
    101020102001
    101020103004
    101020104002
    101020105010
    end
    
    gen long wanted = floor(HHPBASE/1000)
    format wanted HHPBASE %12.0f 
    
    list 
    
         +--------------------------+
         |      HHPBASE      wanted |
         |--------------------------|
      1. | 101020101005   101020101 |
      2. | 101020102001   101020102 |
      3. | 101020103004   101020103 |
      4. | 101020104002   101020104 |
      5. | 101020105010   101020105 |
         +--------------------------+

    Comment


    • #3
      Nick Cox will correct me if I'm wrong, but presumably his answer here gets us 2 thirds of the way there. Note that I'm not at my computer, so I cannot test it myself.


      EDIT: Nevermind, I misunderstood the question to mean "split at the last 3 characters"
      Last edited by Jared Greathouse; 17 Apr 2023, 18:43.

      Comment


      • #4
        Use int() function to obtain the integer:
        Code:
        gen double NEW_HHPBASE=int(HHPBASE/1000)

        Comment


        • #5
          Jared Greathouse I think the question is about ignoring the last 3 digits, not isolating them.

          Comment


          • #6
            Nick Cox That perfectly worked. Thankyou so much! And Chen Samulsion That also worked but it was only the display format that was concerning to me. Perhaps specifying long before the newly generated variable could do the job. Thankyou

            Comment

            Working...
            X