Announcement

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

  • Cutting all but the first two digits of a numeric variable of varying length

    Hello,

    I have a numeric variable with values of varying lengths. I would like to cut all but the first two digits of each value (each observation has a value of at least two digits). This is likely very simple, but I can't find the right command. See my data below.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input long indnaics_num
        622
       5413
     443142
         23
     812112
        622
       6212
       6111
       7220
       7220
       5614
      22110
       8131
      42490
       7211
       7211
      56173
       3360
      56173
       6210
       5411
       6244
        481
        622
       6230
       9200
       8131
       9280
       9280
         23
        622
       6241
      44410
       8131
       7130
       6231
         23
       6212
       3221
       4451
         23
       5613
        310
        622
       7220
         23
         23
      51110
       7220
        447
       5170
       6214
      61101
       6211
      61103
       5202
     812112
       7220
       4481
       5170
       5413
       8123
       6231
     339900
       7220
         23
       6230
       7220
        220
        220
        622
       6111
        923
       9201
    3364101
       5170
       8129
       4231
       5220
       7220
       3231
         23
       6230
       5122
       7130
        622
      22110
       6214
      54190
       8131
        622
        337
      33330
        622
      56173
       6111
     811192
       9280
       6111
       3360
    end
    Best of thanks for your time and consideration

  • #2
    You could
    Code:
    replace indnaics_num = real(substr(string(indnaics_num), 1, 2))

    Comment


    • #3
      You must be an economist, as economists usually assume that everyone else knows their kind of economics, or should. NAICS I know only a smidgen about, as a non-economist. I guess these codes are integers and wildly I am going to guess that they don't go above a trillion.

      Code:
      gen wanted = substr(string(indnaics_num, "%12.0f"), 1, 2)
      should get the first two characters and if you want them as integers, you need to push them through real().

      Comment


      • #4
        Thanks Joseph Coveney, that worked perfectly.

        @Nick, thanks for parsing the command, I understand it better now. As for assumptions, I'm a sociologist, but will give more details of my variables next time.

        Comment


        • #5
          I am a geographer, and nobody even makes jokes about geographers unless it's about their supposed fashion sense (often in question, it's true) or their use of coloured pencils to draw maps by hand (no longer what anyone does).

          Comment

          Working...
          X