Announcement

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

  • creating numeric variable

    I have a dataset with surey respondents' congressional districts and their traits. I want to create a set that matches the traits with thedistrics. But the districts are string and do notmatch the traits. I tried to create a numeric variable for the districts ut continually got the message of a type mismatch since I cannot convert a string to a numericl Is there a way to make this conversion? Here is the example:
    and any help would be appreciated.

    Ric Uslaner

    . dataex cd1

    ----------------------- copy starting from the next line -----------------------
    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str5 cd1
    "AL-01"
    "AL-02"
    "AL-03"
    "AL-04"
    "AL-05"
    "AL-06"
    "AL-07"
    "AK-AL"
    "AZ-01"
    "AZ-02"
    "AZ-03"
    "AZ-04"
    "AZ-05"
    "AZ-06"
    "AZ-07"
    "AZ-08"
    "AZ-09"
    "AR-01"
    "AR-02"
    "AR-03"
    "AR-04"
    "CA-01"
    "CA-02"
    "CA-03"
    "CA-04"
    "CA-05"
    "CA-06"
    "CA-07"
    "CA-08"
    "CA-09"
    "CA-10"
    "CA-11"
    "CA-12"
    "CA-13"
    "CA-14"
    "CA-15"
    "CA-16"
    "CA-17"
    "CA-18"
    "CA-19"
    "CA-20"
    "CA-21"
    "CA-22"
    "CA-23"
    "CA-24"
    "CA-25"
    "CA-26"
    "CA-27"
    "CA-28"
    "CA-29"
    "CA-30"
    "CA-31"
    "CA-32"
    "CA-33"
    "CA-34"
    "CA-35"
    "CA-36"
    "CA-37"
    "CA-38"
    "CA-39"
    "CA-40"
    "CA-41"
    "CA-42"
    "CA-43"
    "CA-44"
    "CA-45"
    "CA-46"
    "CA-47"
    "CA-48"
    "CA-49"
    "CA-50"
    "CA-51"
    "CA-52"
    "CA-53"
    "CO-01"
    "CO-02"
    "CO-03"
    "CO-04"
    "CO-05"
    "CO-06"
    "CO-07"
    "CT-01"
    "CT-02"
    "CT-03"
    "CT-04"
    "CT-05"
    "DE-AL"
    "FL-01"
    "FL-02"
    "FL-03"
    "FL-04"
    "FL-05"
    "FL-06"
    "FL-07"
    "FL-08"
    "FL-09"
    "FL-10"
    "FL-11"
    "FL-12"
    "FL-13"
    end
    ------------------ copy up to and including the previous line ------------------

    Listed 100 out of 486 observations
    Use the count() option to list more

    .


  • #2
    Your example is of one variable for congressional districts in one dataset. What does the other variable recording traits in the other dataset look like?

    Comment


    • #3
      dataset vaeiables:

      cd str5 %9s Code
      black str5 %9s
      latino str6 %9s
      asian str26 %26s
      cdnew str20 %20s
      part1 str10 %10s
      part2 str2 %9s
      cdfull str16 %16s
      cd1 str5 %9s
      portion1 str6 %9s
      portion2 str2 %9s
      cdnew1 str20 %20s
      congdist str5 %9s

      Comment


      • #4
        I can't see that #3 answers my question. You are telling me what other variables exist in the same dataset, I think.

        I assume that match means using the Stata command merge here. If so, then how is AZ-1 (for example) to be matched with a trait?

        Quite likely, I am just failing to understand your question. I don't know what trait means in this context, for a start.

        Comment


        • #5
          trying to create new variable

          I have

          clist name state_abbrev state_abbrev district_code party vote

          name state_a~v state_a~v distri~e party vote
          1. BUSH, George Herbert Walker USA USA 0 democrat 1
          2. CALLAHAN, Herbert Leon (Sonny) AL AL 1 democrat 0
          3. DICKINSON, William Louis AL AL 2 democrat 1
          4. BROWDER, John Glen AL AL 3 republican 0
          5. BEVILL, Tom AL AL 4 republican 0
          6. FLIPPO, Ronnie Gene AL AL 5 republican 0
          7. ERDREICH, Ben AL AL 6 republican 0


          303. GILLMOR, Paul Eugene OH OH 5 democrat 1
          304.
          332.
          and I type, trying to create a new variable from state and district_code:

          . des district_code

          storage display value
          variable name type format label variable label
          -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
          district_code byte %8.0g

          . replace district_code = 0x if x < 10
          0x invalid name
          r(198);

          . gen cd =
          invalid syntax
          r(198);

          . encode state_abbrev,gen(state)

          . gen statecd = state + district_code

          . clist name statecd state_abbrev state_abbrev party vote

          name statecd state_a~v state_a~v party vote
          1. BUSH, George Herbert Walker 44 USA USA democrat 1
          2. CALLAHAN, Herbert Leon (Sonny) 3 AL AL democrat 0
          3. DICKINSON, William Louis 4 AL AL democrat 1
          4. BROWDER, John Glen 5 AL AL republican 0
          5. BEVILL, Tom 6 AL AL republican 0
          6. FLIPPO, Ronnie Gene 7 AL AL republican 0
          7.
          but I did not get state+ district_code. What did I do wrong? Thanks for any help.

          .

          Comment


          • #6
            What did you do wrong? #5 is peppered with small mistakes and irrelevancies, whether in the use of Stata or in the way that the question is posed,

            You did not use dataex to show a data example. Nothing you show is easily reproducible without a lot of work (including guesswork).

            You did not select # in the message editor, which has the merit of ensuring a font that makes code and data easy to read.

            You used describe -- which would have been helpful if issued for all variables -- but only for one variable.

            You used clist without explaining that it is an obsolete command. That is a distraction.

            You used gen cd = which triggered a syntax error becuse Stata did not see a recipe for calculating a new variable.

            You typed

            Code:
            replace district_code = 0x if x < 10
            which triggered a syntax error, but is in any case puzzling: what is x in your data and what are you trying to do there?

            More generally, your post is hard work: you include many details that aren't relevant to your question, present all poorly, and don't use best practices as long since explained in the FAQ and evident in threads that do get good answers quickly.

            If this was a student assignment, you would give it a poor grade. Don't we deserve better?

            But let's disinter the question, or what it should have been.

            1. district_code is numeric. That is clear from the describe.

            2. The command

            Code:
            encode state_abbrev,gen(state)
            creates state as a numeric variable with value labels. That is what it is designed to do.

            3. The command

            Code:
            gen statecd = state + district_code
            is therefore an instruction to add two numeric variables together. It worked (from Stata's point of view) although the result will usually be useless,
            as the default coding of encode, which you used, just uses alphabetical order to map to integers 1 up, which is not helpful for what you did.

            So, the complaint

            I did not get state + district_code
            really means (I guess)

            I did not get what I expected from that addition.
            and that is so. You want (I think) concatenation, not addition.

            Here is a silly reproducible example. Suppose you want a string concatenation of make, a string identifier, and repair record, a numeric variable, in the auto data.


            Code:
            . sysuse auto, clear
            (1978 automobile data)
            
            . keep if foreign
            (52 observations deleted)
            
            . keep make rep78
            
            . list
            
                 +------------------------+
                 | make             rep78 |
                 |------------------------|
              1. | Audi 5000            5 |
              2. | Audi Fox             3 |
              3. | BMW 320i             4 |
              4. | Datsun 200           4 |
              5. | Datsun 210           5 |
                 |------------------------|
              6. | Datsun 510           4 |
              7. | Datsun 810           4 |
              8. | Fiat Strada          3 |
              9. | Honda Accord         5 |
             10. | Honda Civic          4 |
                 |------------------------|
             11. | Mazda GLC            4 |
             12. | Peugeot 604          . |
             13. | Renault Le Car       3 |
             14. | Subaru               5 |
             15. | Toyota Celica        5 |
                 |------------------------|
             16. | Toyota Corolla       5 |
             17. | Toyota Corona        5 |
             18. | VW Dasher            4 |
             19. | VW Diesel            5 |
             20. | VW Rabbit            4 |
                 |------------------------|
             21. | VW Scirocco          4 |
             22. | Volvo 260            5 |
                 +------------------------+
            
            . gen id1 = make + " " + strofreal(rep78)
            
            . egen id2 = concat(make rep78), punct(" ")
            
            . list 
            
                 +--------------------------------------------------------------+
                 | make             rep78                id1                id2 |
                 |--------------------------------------------------------------|
              1. | Audi 5000            5        Audi 5000 5        Audi 5000 5 |
              2. | Audi Fox             3         Audi Fox 3         Audi Fox 3 |
              3. | BMW 320i             4         BMW 320i 4         BMW 320i 4 |
              4. | Datsun 200           4       Datsun 200 4       Datsun 200 4 |
              5. | Datsun 210           5       Datsun 210 5       Datsun 210 5 |
                 |--------------------------------------------------------------|
              6. | Datsun 510           4       Datsun 510 4       Datsun 510 4 |
              7. | Datsun 810           4       Datsun 810 4       Datsun 810 4 |
              8. | Fiat Strada          3      Fiat Strada 3      Fiat Strada 3 |
              9. | Honda Accord         5     Honda Accord 5     Honda Accord 5 |
             10. | Honda Civic          4      Honda Civic 4      Honda Civic 4 |
                 |--------------------------------------------------------------|
             11. | Mazda GLC            4        Mazda GLC 4        Mazda GLC 4 |
             12. | Peugeot 604          .      Peugeot 604 .      Peugeot 604 . |
             13. | Renault Le Car       3   Renault Le Car 3   Renault Le Car 3 |
             14. | Subaru               5           Subaru 5           Subaru 5 |
             15. | Toyota Celica        5    Toyota Celica 5    Toyota Celica 5 |
                 |--------------------------------------------------------------|
             16. | Toyota Corolla       5   Toyota Corolla 5   Toyota Corolla 5 |
             17. | Toyota Corona        5    Toyota Corona 5    Toyota Corona 5 |
             18. | VW Dasher            4        VW Dasher 4        VW Dasher 4 |
             19. | VW Diesel            5        VW Diesel 5        VW Diesel 5 |
             20. | VW Rabbit            4        VW Rabbit 4        VW Rabbit 4 |
                 |--------------------------------------------------------------|
             21. | VW Scirocco          4      VW Scirocco 4      VW Scirocco 4 |
             22. | Volvo 260            5        Volvo 260 5        Volvo 260 5 |
                 +--------------------------------------------------------------+
            I have shown two ways to do it, and there are at least two more, to use egen's group() function and to use tostring.

            All this is documented, and there are specific sources too, such as

            Code:
            SJ-7-4  dm0034  . . . Stata tip 52: Generating composite categorical variables
                    . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  N. J. Cox
                    Q4/07   SJ 7(4):582--583                                 (no commands)
                    tip on how to generate categorical variables using
                    tostring and egen, group()

            Comment

            Working...
            X