Announcement

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

  • Keeping only two first digits of a numeric variable

    Hello!

    I am currently working with a pretty big dataset, and I am trying to change one variable (which is a regional indicator) so it would contain only first digit / first two digits.

    The thing is that this variable has different lengths - it is either 10 digits (ex. 7412000000) or 11 digits (ex. 45333000000) long, and so in the case when it is 10 digits, I want to keep only the first digit, and when it is 11 digits, I want to keep the first two digits.

    I am also not sure whether I should replace the values of the original variable, or try to generate a new one.

    Thank you for your suggestions in advance!
    Last edited by Iuliia Svetetskaia; 05 Mar 2022, 03:52.

  • #2
    Is your identifier really numeric, or perhaps string? The solution will be different.

    Code:
    clear
    input double nid
    7412000000
    45333000000
    end
    
    format nid %12.0f
    gen sid = strofreal(nid, "%12.0f")
    
    gen wanted_s = cond(length(sid) == 11, substr(sid, 1, 2), substr(sid, 1,1))
    
    gen wanted_n = floor(nid/1e9)
    
    list
    
         +-------------------------------------------------+
         |         nid           sid   wanted_s   wanted_n |
         |-------------------------------------------------|
      1. |  7412000000    7412000000          7          7 |
      2. | 45333000000   45333000000         45         45 |
         +-------------------------------------------------+
    Last edited by Nick Cox; 05 Mar 2022, 04:13.

    Comment


    • #3
      Originally posted by Nick Cox View Post
      Is your identifier really numeric, or perhaps string? The solution will be different.

      Code:
      clear
      input double nid
      7412000000
      45333000000
      end
      
      format nid %12.0f
      gen sid = strofreal(nid, "%12.0f")
      
      gen wanted_s = cond(length(sid) == 11, substr(sid, 1, 2), substr(sid, 1,1))
      
      gen wanted_n = floor(nid/1e9)
      
      list
      
      +-------------------------------------------------+
      | nid sid wanted_s wanted_n |
      |-------------------------------------------------|
      1. | 7412000000 7412000000 7 7 |
      2. | 45333000000 45333000000 45 45 |
      +-------------------------------------------------+

      Hello, Nick!

      It is numeric.

      I've run your code, it worked perfectly.

      Thank you so much!

      Comment

      Working...
      X