Announcement

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

  • joinby issue

    I have a dataset with variables listing amember of Congress, his/her state and Congressional district number. I want to combine the state and Congressional district into one variable. I typed:

    state and Congressional district

    but joinby can onlybe used when both variables are in different data set. How could I unite the state and Congressional district variables into one variable in the same dataset. Thanks,

    Rc Uslaner

  • #2
    -joinby- and -merge- require whatever the key variables are to be in both datasets and of compatible types (e.g., one key can't be a string in one dataset and a number in the other). You should be able to use state and district as key variables.

    Have you tried to make yourself a toy example to test how this might work? I would suggest doing that as a starting point.

    Comment


    • #3
      not sure I completely understand but it sounds like you want to concatenate the values of two variables into one new variable; is that correct? if yes, then all depends on whether both are string variables (which is why you should always present a dataex example of your data); assuming they are both string variables, here is sample code (obviously you should replace "newvar" with a variable name that is meaningful to you):
      Code:
      gen str newvar=state+district
      note that since I don't know what the actual varnames are, you should replace "state" and "district" with the actual names

      if one or both of your current variables are numeric, use -tostring- to change them into string variables; see
      Code:
      h tostring

      Comment


      • #4
        I want to drop cases where the N for Congressional districts is less than 40. I typed:


        . des VCF0900c presvote VCF0707 VCF0708,full

        storage display value
        variable name type format label variable label
        -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
        VCF0900c str4 %4s State and Congressional District- Postal Abbrev and CD
        presvote float %9.0g
        VCF0707 double %12.0g VCF0707 Vote for Congressman
        VCF0708 double %12.0g VCF0708 Vote for Senator

        . encode VCF0900c,gen(cd)

        . keep cd presvote VCF0707 VCF0708 if cd > 40
        invalid syntax
        r(198);

        . fsum cd

        Variable | N Mean SD Min Max
        ----------+---------------------------------------------
        cd | 17558 131.03 82.59 1.00 263.00

        . drop cd presvote VCF0707 VCF0708 if cd > 40
        invalid syntax
        r(198);

        . drop * if cd > 40
        invalid syntax
        r(198);

        what did I do wrong? Thanks.

        Comment


        • #5
          Sorry I also need to break down a variable in a companion data set:

          California, District 6

          to read California 06 or better CA06. Thanks.

          Comment


          • #6
            #4 Look back at the thread you started on “deleting observations” on 24 August. You got explanations of your misunderstanding of the drop command.

            Comment


            • #7
              Many of these misunderstandings or questions can be resolved by giving the Getting Started guide a thorough read, and then relevant command documentation (+/- making toy examples to demonstrate your understanding).

              Comment

              Working...
              X