Announcement

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

  • How to combine two variables to create a new one?

    Hello,

    I am working with a dataset with merged data from 2 survey rounds (2005 & 20011).

    The first column/variable "SURVEY" represents whether the observation is from survey 1 or 2
    The second column/variable "HHBASE" represents the unique Household Base ID which remains unchanged for the two surveys
    The third column/variable "PBASE" represents the unique Person Base ID which remains unchanged for the two surveys

    I would like to create a new id variable using HHBASE and PBASE in such a way that the numbers are not added but instead "combined". For example:
    HHBASE = 100
    PBASE = 1

    New var = 1001

    Below, I am pasting a sample of the data that I need help with.

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int SURVEY double HHBASE int PBASE
    1 1010201010  1
    1 1010201010  2
    1 1010201010  5
    1 1010201010  6
    1 1010201010  3
    1 1010201020 15
    2 1010201020  4
    2 1010201020 14
    2 1010201020  9
    2 1010201020  5
    2 1010201020  6
    1 1010201020 14
    2 1010201020 13
    1 1010201020 11
    2 1010201020 10
    1 1010201020  6
    2 1010201020  7
    1 1010201020  7
    1 1010201020  8
    2 1010201020  1
    1 1010201020  5
    2 1010201020 11
    1 1010201020  4
    2 1010201020 12
    1 1010201020 10
    2 1010201020  8
    2 1010201020  3
    2 1010201020 15
    1 1010201020  1
    1 1010201020 12
    1 1010201020  3
    1 1010201020 13
    1 1010201020  9
    1 1010201030  6
    2 1010201030  3
    1 1010201030  1
    2 1010201030  2
    1 1010201030  7
    2 1010201030  1
    2 1010201030  5
    2 1010201030  7
    2 1010201030  4
    1 1010201030  3
    1 1010201030  5
    2 1010201030  6
    1 1010201030  2
    1 1010201030  4
    2 1010201040  2
    1 1010201040  5
    2 1010201040  3
    2 1010201040  1
    1 1010201040  4
    1 1010201040  1
    2 1010201040  5
    2 1010201040  4
    1 1010201040  3
    1 1010201040  2
    1 1010201050  6
    2 1010201050  8
    2 1010201050  6
    1 1010201050  2
    1 1010201050 10
    1 1010201050  9
    1 1010201050  7
    2 1010201050  2
    1 1010201050  8
    2 1010201050  9
    1 1010201050  3
    2 1010201050 10
    2 1010201050  3
    2 1010201050  7
    1 1010201060 12
    2 1010201060  1
    1 1010201060 15
    2 1010201060 13
    2 1010201060 12
    2 1010201060 15
    1 1010201060 14
    1 1010201060  1
    2 1010201060 14
    1 1010201060 13
    1 1010201070  3
    2 1010201070  1
    1 1010201070  1
    2 1010201070  2
    2 1010201070  9
    2 1010201070  7
    1 1010201070  7
    2 1010201070  6
    2 1010201070  8
    1 1010201070  9
    1 1010201070  4
    1 1010201070  6
    2 1010201070  4
    1 1010201070  2
    1 1010201070  8
    2 1010201070  3
    2 1010201080  3
    2 1010201080  6
    1 1010201080  1
    end
    label values SURVEY SURVEY
    label def SURVEY 1 "IHDS1 1", modify
    label def SURVEY 2 "IHDS2 2", modify

    Thanks for your help!

    Regards,
    Abhishek Patil
    Last edited by Abhishek Patil; 29 Mar 2021, 02:28.

  • #2
    Try:

    Code:
    . egen id = concat( HHBASE PBASE )

    Comment


    • #3
      Hi Joro,

      Thanks for your reply.

      I tried what you suggested but it seems to make extremely large values, which I am not sure are right.

      Furthermore, if you could also help with how I can see the whole numeric value instead of the scientific notation, that would be highly appreciated.

      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input str10 id
      "1.01e+095" 
      "1.01e+096" 
      "1.01e+097" 
      "1.01e+098" 
      "1.01e+0910"
      "1.01e+0911"
      "1.01e+094" 
      "1.01e+095" 
      "1.01e+096" 
      "1.01e+097" 
      "1.01e+096" 
      "1.01e+097" 
      "1.01e+095" 
      "1.01e+094" 
      "1.01e+093" 
      "1.01e+098" 
      "1.01e+099" 
      "1.01e+096" 
      "1.01e+094" 
      "1.01e+094" 
      "1.01e+094" 
      "1.01e+093" 
      "1.01e+094" 
      "1.01e+093" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+096" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+094" 
      "1.02e+094" 
      "1.02e+096" 
      "1.02e+095" 
      "1.02e+093" 
      "1.02e+095" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+0910"
      "1.02e+099" 
      "1.02e+095" 
      "1.02e+098" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+096" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+093" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+096" 
      "1.02e+097" 
      "1.02e+098" 
      "1.02e+099" 
      "1.02e+096" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+092" 
      "1.02e+095" 
      "1.02e+094" 
      "1.02e+096" 
      "1.02e+097" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+096" 
      "1.02e+0913"
      "1.02e+095" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+099" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+097" 
      "1.02e+093" 
      "1.02e+095" 
      "1.02e+096" 
      "1.02e+097" 
      "1.02e+092" 
      "1.02e+095" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+095" 
      "1.02e+096" 
      "1.02e+094" 
      "1.02e+093" 
      "1.02e+094" 
      "1.02e+095" 
      "1.02e+096" 
      end

      Comment


      • #4
        We have a problem there, -egen, concat- did not do what it was supposed to do. We can probably make it work properly, but it is easier just to do this thing manually.

        Do the following:

        Code:
        . tostring HHBASE PBASE, gen(HHBASEstr PBASEstr)
        HHBASEstr generated as str10
        PBASEstr generated as str2
        
        . gen id = HHBASEstr + PBASEstr
        
        . list in 1/10, sep(0)
        
             +--------------------------------------------------------------------+
             |  SURVEY      HHBASE    HHBASEstr   PBASE   PBASEstr             id |
             |--------------------------------------------------------------------|
          1. | IHDS1 1   1.010e+09   1010201010       1          1    10102010101 |
          2. | IHDS1 1   1.010e+09   1010201010       2          2    10102010102 |
          3. | IHDS1 1   1.010e+09   1010201010       5          5    10102010105 |
          4. | IHDS1 1   1.010e+09   1010201010       6          6    10102010106 |
          5. | IHDS1 1   1.010e+09   1010201010       3          3    10102010103 |
          6. | IHDS1 1   1.010e+09   1010201020      15         15   101020102015 |
          7. | IHDS2 2   1.010e+09   1010201020       4          4    10102010204 |
          8. | IHDS2 2   1.010e+09   1010201020      14         14   101020102014 |
          9. | IHDS2 2   1.010e+09   1010201020       9          9    10102010209 |
         10. | IHDS2 2   1.010e+09   1010201020       5          5    10102010205 |
             +--------------------------------------------------------------------+

        Comment


        • #5
          Hi Joro!

          Once again thanks a lot for your swift reply.

          I have just run your code and it seems to have done what I needed. I will write back in case I experience problems.

          Thanks and kind regards,
          Abhishek Patil

          Comment


          • #6
            Very good then.

            As to your subquestion "if you could also help with how I can see the whole numeric value instead of the scientific notation," you can read

            Code:
            help format
            however it is a long and involved story.

            Originally posted by Abhishek Patil View Post
            Hi Joro!

            Once again thanks a lot for your swift reply.

            I have just run your code and it seems to have done what I needed. I will write back in case I experience problems.

            Thanks and kind regards,
            Abhishek Patil

            Comment


            • #7
              Thanks, Joro, that really helped.

              I was hoping you could guide me with another challenge.

              I have one dataset with observations from two different survey rounds.

              Now I want to run a condition that - If the observation is from round 1 survey, then I want to keep only observations where the number of children in the household is greater than zero.

              More specifically: for SURVEY == 1 keep NCHILD > 0

              Clearly, I am doing something wrong with this code since I keep getting error codes for invalid name or syntax every time I try to fix it. Do let me know if I should provide a sample.

              Thanks a lot in advance

              Comment


              • #8
                You might be looking for

                Code:
                keep if SURVEY == 1 & NCHILD > 0

                Comment


                • #9
                  That seems to work but not exactly the way I need it.

                  Following the code provided by you ends up only with observations from Survey 1.

                  I would still like to have my entire dataset with both Survey 1 and Survey 2 observations. However, I would only like the condition of NCHILD > 0 to apply to Survey 1.

                  Is that possible to carry out in some way?

                  Comment


                  • #10
                    Try
                    Code:
                    gen condition = (SURVEY == 1 & NCHILD > 0)  | SURVEY == 2
                    then whatever you do, you end it with if condition, e.g.,

                    Code:
                    summarize wage if condition
                    will summarize you the wage for all people in survey 2, plus people in survey one who have NCHILD>0.

                    Originally posted by Abhishek Patil View Post
                    That seems to work but not exactly the way I need it.

                    Following the code provided by you ends up only with observations from Survey 1.

                    I would still like to have my entire dataset with both Survey 1 and Survey 2 observations. However, I would only like the condition of NCHILD > 0 to apply to Survey 1.

                    Is that possible to carry out in some way?

                    Comment


                    • #11
                      On #4 and the suggestion that egen, concat() did not do what it was supposed to do: the option format() is provided for such cases.

                      Comment


                      • #12
                        I figured that much, Nick, but I did not feel like diving into Stata formats and how we pass them through -egen, concat- options.

                        So how do we accomplish the task through -egen, concat- and is there such a general format where we tell Stata "do not use scientific notation but show the numbers as they are"?


                        Originally posted by Nick Cox View Post
                        On #4 and the suggestion that egen, concat() did not do what it was supposed to do: the option format() is provided for such cases.

                        Comment


                        • #13
                          Brilliant! did the job perfectly!
                          Highly appreciate your help Thanks a lot!

                          Originally posted by Joro Kolev View Post
                          Try
                          Code:
                          gen condition = (SURVEY == 1 & NCHILD > 0) | SURVEY == 2
                          then whatever you do, you end it with if condition, e.g.,

                          Code:
                          summarize wage if condition
                          will summarize you the wage for all people in survey 2, plus people in survey one who have NCHILD>0.


                          Comment


                          • #14
                            "show the numbers as they are" is a simple question, with a simple answer only if the numbers are moderate or small integers.

                            In practice, most sensible applications of this function would be to numeric components that are integers but the code won't look inside your variables to detect whether any of your numbers have fractional parts, let alone prohibit you from working with such variables.

                            As the record shows, I first wrote concat() but it's long since (Stata 7) been folded into official Stata. That accident of origin lets me recall the rationale for this function as I wrote it, which hasn't changed in broad terms over 20 years of official ownership.

                            Concatenation of string variables or of string constants is just a matter of applying PLUS (+) and was not the motivation for writing this function. It was inspired by problems exactly like that in this thread, in which some or even all the components are numeric.

                            The rules of concat() start with

                            1. String variables are added as presented.

                            2. String constants are added as presented. The syntax allows any constant presented as "punctuation", which will be added between variables, and which will often be a space or a comma.although there are no rules on that.

                            3. Numeric variables may have value labels and if you want the value labels, not the values, to be concatenated, you must specify the decode option.

                            4. Otherwise numeric variables are pushed through string() -- Stata now prefers the name strofreal() -- which is what is biting here. The nub of the matter is shown by

                            Code:
                            . di string(1010201010)
                            1.01e+09
                            
                            . di strofreal(1010201010)
                            1.01e+09
                            
                            . di strofreal(1010201010, "%10.0f")
                            1010201010


                            The default for string conversion of integers of the order of 1 billion is to use "scientific" format with powers of 10. The Stataish response to "That's not what I want!" is in effect "OK, so tell me what you do want by specifying a display format". In writing concat() originally I followed Stata's practice of using a bare string() call but allowing a format to be specified by an option.

                            There could be a clone of concat() in which the code tries harder to guess what a user wants with each numeric component. I have written other programs for other purposes to try to guess what I usually want when I call up my own code, but it shouldn't be surprising that it is usually more coding work and quite easy to get wrong, even for myself.


                            Comment

                            Working...
                            X