Announcement

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

  • obtaining the first three digits

    I have naics codes that have six digits, and I want to get the first three digits in each of them and form a new variable named three_digits

    I tried using the "gen threedigit=int(Naics_1/1000)" (this gave me a list of zeros in the newly generated variable)

    Looking at answers to similar posts, I used "gen three_digit=substr(naics, 1,2,3)" and it was an Invalid Syntax

    Thank you.

  • #2
    Welcome to the Stata Forum / Statalist.

    Please read the FAQ. There you'll learn how to share data/command/ output.

    Below you'll have an example I need to type from scratch:

    Code:
    . input str20 myvar
    
                        myvar
      1. 123456789
      2. 34523
      3. 55567890
      4. end
    
    . gen threedvar = substr(myvar,1,3)
    
    . list
    
         +----------------------+
         |     myvar   threed~r |
         |----------------------|
      1. | 123456789        123 |
      2. |     34523        345 |
      3. |  55567890        555 |
         +----------------------+
    
    .
    Hopefully that helps.
    Best regards,

    Marcos

    Comment


    • #3
      Thank you Marcos, This worked perfectly !!

      Comment


      • #4
        Just as a side note: in case the variable is not "string", you may first use - tostring myvar, gen(myvar2) - command and proceed from this point.
        Best regards,

        Marcos

        Comment


        • #5
          Thanks Marcos, the previous one worked ! it is a string variable

          Comment

          Working...
          X