Announcement

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

  • Generating New Variable with several values of one existing variable using "Wildcards"

    Hello Statalist,

    I am trying to generate a new variable based on an existing variable. The existing variable (for economic sectors) has 1,300 individual observations, and consists of a number from 01 to 99000. To make sectors more manageable I want to create a new variable which uses only the first two numbers of the sector. So if the old variable is 01 or 011 or 01110 or 011120 or 0113 or 01113, the new one should be "01". The same for all values in my old variable starting with "02" etc.

    What I would need is a sort of wildcard, to do for instance:
    gen sector_new = 1 if sector_old == 01*
    replace sector_new = 2 if sector_old == 02*
    etc

    However as I understand it wildcards can only be used for variables and not for singular values, so the above command does not work and I am looking for a way to implement this.

    The problem why I can't use the inrange command or inlist command (or simply tell state to gen a new var b if a (my old var) is between 0 and for instance 01900) is that the value of my old variable can be 2,3,4 or 5 digits. So if I would say "generate new variable if the value is between 0 and 01900", it would also include for instance 02, 021, 022 etc, which I do not want as these need to go in a separate category.

    I hope the explanation is understandable, and would very much appreciate your help.

    Kind regards
    Elisabeth

  • #2
    this is confusing since, at least in general, Stata will not keep a first digit of 0 - is this already a string variable? do you want the new variable to be string or numeric? try the following (assumes current variable is a string and the desired new variable is to be numeric) - please provide data examples using -dataex- (read the FAQ)
    Code:
    gen byte newvar=real(substr(oldvar,1,2))
    substitute your actual variable name for "oldvar" and use a name of your choosing instead of "newvar"

    Comment


    • #3
      thanks a lot, using substring worked for me!

      Comment

      Working...
      X