Announcement

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

  • Generate new variable pasting numbers or text

    Hello,
    I'm working with U.S natality birth data, and I need to merge this data by year and county with some other data of economic characteristics of the county. In the first data set (natality) the county variable is composed by the number of the state, plus the number of the county. For example, for TX, which is the state number 44 in the natality dataset, the county variables are 44001, 44002, 44003,... etc. In the other data set I only have the county data: 001, 002, 003... etc.
    I need to create a new variable that pastes "44" in front of every value of the county variable in my economic characteristic dataset, for me to be able to do the merge.

    This is how it looks now:
    county state
    001 48
    002 48
    003 48
    004 49
    This is how it should look with the new variable:
    county state newvar
    001 48 44001
    002 48 44002
    003 48 44003
    004 48 44004

    Thank you very much!

  • #2
    This is confusing. If the state is 48, and the county is 001, why is the other variable 44001 and not 48001? Is there a general formula relating the state number to the number that needs to be prefixed to the county?

    Also, the code will depend critically on whether we are working with string variables or numeric variables. Please post back showing examples from both data sets, using the -dataex- command to do so. If you are running version 16 or a fully updated version 15.1 or 14.2, -dataex- is already part of your official Stata installation. If not, run -ssc install dataex- to get it. Either way, run -help dataex- to read the simple instructions for using it. -dataex- will save you time; it is easier and quicker than typing out tables. It includes complete information about aspects of the data that are often critical to answering your question but cannot be seen from tabular displays or screenshots. It also makes it possible for those who want to help you to create a faithful representation of your example to try out their code, which in turn makes it more likely that their answer will actually work in your data.

    Comment


    • #3
      I'm sorry. I'm a bit new to statalist and -dataex-
      in the example that I showed the State is 48 because it's different in both datasets, but it's not an issue because I created a new state variable with the number 44. I just realized that the county numbers are not as they are supposed to be according to the code description, and they are missing the "0" or "00" in front of numbers of 1 digit or 2 digits, respectively. I was trying to use -egen concat- but I can't make it work. Heres an examen with -dataex-. newvar is the variable I'm trying to create, all of them numeric variables:
      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input int county byte state long newvar
        1 44 44001
        2 44 44002
        3 44 44003
        4 44 44004
        5 44 44005
       10 44 44010
       11 44 44011
       12 44 44012
       13 44 44013
       14 44 44014
      100 44 44100
      101 44 44101
      102 44 44102
      103 44 44103
      104 44 44104
      end
      thank you very much!!

      Comment


      • #4
        If the county codes are always less than 1000, then

        Code:
        gen wanted = state*1000 + county

        Comment


        • #5
          Wow! thank you Andrew!!! that was so much easier that what I was imagining in my head to do. Thanks!!!!!

          Comment

          Working...
          X