Announcement

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

  • Creating two phone number variables depending on conditions

    Hello, I have three variables below, a CASENUM variable a PHONETYPE variable which list 4 types of telephone "Cell" "Work" "Home" "Other" I want to create two other variables, a primary_phone_number which has all the phone numbers of CASENUM who choose a cell phone in the PHONETYPE variable and a second variable named secondary_phone_number which lists the cell phone numbers of everyone who did NOT LIST CELL PHONE in the PHONETYPE variable. The issue is that CASENUM does not identify everyone properly as their are duplicated CASENUMs

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str12 CASENUM str10 PHONETYPE double PHONENUMBER
    "SAPD24118937" "Cell"  18304316624
    "SAPD24118889" "Cell"  12109791643
    "SAPD24118835" "Cell"  14024994996
    "SAPD24118755" "Cell"  17856141487
    "SAPD24118755" "Cell"  13038701951
    "SAPD24118519" "Cell"  12106053538
    "SAPD24118322" "Cell"  12106438156
    "SAPD24117355" "Cell"  12109622257
    "SAPD24117332" "Cell"  12107711890
    "SAPD24117332" "Home"     17547799
    "SAPD24117332" "Home"  12104452311
    "SAPD24117313" "Cell"  15095928290
    "SAPD24117309" "Cell"  12103866666
    "SAPD24117283" "Home"     16263285
    "SAPD24117283" "Cell"  12104541543
    "SAPD24117155" "Cell"  12108105758
    "SAPD24116684" "Cell"  12105178751
    "SAPD24116652" "Cell"  15614258338
    "SAPD24116633" "Cell"  12107179800
    "SAPD24116556" "Cell"  17262067496
    "SAPD24116238" "Work"  12106890915
    "SAPD24116238" "Cell"  12107168732
    "SAPD24116238" "Other" 12105733008
    "SAPD24115375" "Cell"  12102871475
    "SAPD24115319" "Other" 12104265972
    "SAPD24115247" "Cell"  12108484932
    "SAPD24114836" "Work"  12103662304
    "SAPD24114836" "Home"     14796002
    "SAPD24114836" "Other" 12102136075
    "SAPD24114836" "Other" 12102136975
    "SAPD24114824" "Home"  12102871383
    "SAPD24114824" "Cell"  13614459044
    "SAPD24114824" "Other" 18303050607
    "SAPD24114770" "Other" 12103855645
    "SAPD24114658" "Work"  12102062238
    "SAPD24114658" "Cell"  12103780789
    "SAPD24114544" "Cell"  17263339822
    "SAPD24114368" "Cell"  14328160330
    "SAPD24114255" "Cell"  12102515863
    "SAPD24113868" "Cell"  13526344163
    "SAPD24113859" "Cell"  19404358533
    "SAPD24113829" "Work"  12104106553
    "SAPD24113829" "Other" 18305708881
    "SAPD24113799" "Cell"  14328894618
    "SAPD24113799" "Cell"  14328892030
    "SAPD24113439" "Cell"  12102547088
    "SAPD24113306" "Cell"  12104730533
    "SAPD24113212" "Cell"  12102907162
    "SAPD24113212" "Cell"  12102942307
    "SAPD24111630" "Cell"  17267770236
    "SAPD24111630" "Cell"  12107945931
    "SAPD24111630" "Work"  12105341874
    "SAPD24111630" "Other" 12104655628
    "SAPD24111551" "Cell"  12108129810
    "SAPD24111414" "Cell"  18303189541
    "SAPD24111414" "Other" 12107794694
    "SAPD24111379" "Cell"  12103653666
    "SAPD24110660" "Cell"  12108595319
    "SAPD24110498" "Cell"  12105890399
    "SAPD24110011" "Cell"  12103320671
    "SAPD24109950" "Cell"  12104390646
    "SAPD24109677" "Cell"  12108834344
    "SAPD24108472" "Home"  19033609332
    "SAPD24108472" "Cell"  12103800924
    "SAPD24108472" "Cell"  12107276540
    "SAPD24108372" "Cell"  12105961760
    "SAPD24108372" "Cell"  12106678965
    "SAPD24107440" "Cell"  14694505791
    "SAPD24107432" "Cell"  12107392041
    "SAPD24107418" "Other" 12105500267
    "SAPD24107409" "Other" 12103223541
    "SAPD24107346" "Cell"  12104520683
    "SAPD24107346" "Cell"  12109191094
    "SAPD24105046" "Cell"  19153282289
    "SAPD24105046" "Cell"  12109311144
    "SAPD24104764" "Cell"  12107241253
    "SAPD24104764" "Cell"  12107106363
    "SAPD24104764" "Home"  12109148740
    "SAPD24103890" "Cell"  12106050037
    "SAPD24103825" "Cell"  12108378937
    "SAPD24103825" "Cell"  12109542626
    "SAPD24103825" "Home"     15322286
    "SAPD24103825" "Home"  12104091742
    "SAPD24103326" "Cell"  12108151500
    "SAPD24103232" "Other" 12109709964
    "SAPD24103232" "Other" 12107822892
    "SAPD24103232" "Other" 12106206927
    "SAPD24103232" "Work"  12102231021
    "SAPD24103232" "Cell"  12107402296
    "SAPD24103232" "Home"  12107723527
    "SAPD24103098" "Cell"  12107938622
    "SAPD24103051" "Cell"  14322326766
    "SAPD24103051" "Cell"  14323082799
    "SAPD24102251" "Cell"  12106249294
    "SAPD24102072" "Cell"  12109951656
    "SAPD24101837" "Other" 18322479638
    "SAPD24101837" "Cell"  18328183427
    "SAPD24099793" "Cell"  18175426211
    "SAPD24099751" "Cell"  12106939331
    "SAPD24098514" "Cell"  13615826528
    end

  • #2
    I'm sorry, I'm not entirely clear what you want. I am reproducing a part of your data extract below. For each of these observations, could you tell us what values you would like the new variables to take?

    Code:
    clear
    input str12 CASENUM str10 PHONETYPE double PHONENUMBER
    "SAPD24118755" "Cell"  17856141487
    "SAPD24118755" "Cell"  13038701951
    "SAPD24117332" "Cell"  12107711890
    "SAPD24117332" "Home"     17547799
    "SAPD24117332" "Home"  12104452311
    "SAPD24114836" "Work"  12103662304
    "SAPD24114836" "Home"     14796002
    "SAPD24114836" "Other" 12102136075
    "SAPD24114836" "Other" 12102136975
    end

    Comment


    • #3
      Originally posted by Hemanshu Kumar View Post
      I'm sorry, I'm not entirely clear what you want. I am reproducing a part of your data extract below. For each of these observations, could you tell us what values you would like the new variables to take?

      Code:
      clear
      input str12 CASENUM str10 PHONETYPE double PHONENUMBER
      "SAPD24118755" "Cell" 17856141487
      "SAPD24118755" "Cell" 13038701951
      "SAPD24117332" "Cell" 12107711890
      "SAPD24117332" "Home" 17547799
      "SAPD24117332" "Home" 12104452311
      "SAPD24114836" "Work" 12103662304
      "SAPD24114836" "Home" 14796002
      "SAPD24114836" "Other" 12102136075
      "SAPD24114836" "Other" 12102136975
      end
      Hi, I would like to create two separate columns, one with the phone number of everyone with a cell phone and another column with the phone number of everyone who didn't list a cell phone in PHONETYPE. I hope this makes more sense

      Comment


      • #4
        I'm afraid it's not quite sufficient. I gave the data extract for a reason. For instance, it is not clear to me:
        • do you want a single observation for each CASENUM, or should it remain as-is?
        • what do you want to do for cases where multiple cell numbers are listed? (such as for SAPD24118755)
        • does the second column contain a secondary number even when a cell number is listed? (such as for SAPD24117332)
        This repeated back-and-forth might be cut short if you showed us how you want the final data to look like, for the data extract I provided.

        Comment


        • #5
          Originally posted by Hemanshu Kumar View Post
          I'm afraid it's not quite sufficient. I gave the data extract for a reason. For instance, it is not clear to me:
          • do you want a single observation for each CASENUM, or should it remain as-is?
          • what do you want to do for cases where multiple cell numbers are listed? (such as for SAPD24118755)
          • does the second column contain a secondary number even when a cell number is listed? (such as for SAPD24117332)
          This repeated back-and-forth might be cut short if you showed us how you want the final data to look like, for the data extract I provided.
          Something like this
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input str12 CASENUM str10 PHONETYPE double PHONENUMBER
          "SAPD24118755" "Cell"  17856141487
          "SAPD24118755" "Cell"  13038701951
          "SAPD24117332" "Cell"  12107711890
          "SAPD24114836" "Work"  12103662304
          "SAPD24114836" "Home"     14796002
          "SAPD24114836" "Other" 12102136075
          "SAPD24114836" "Other" 12102136975
          end

          Keep all instances when there is a different cell-number, even for duplicates in the cell-phone number column, if the duplicate list more than cell phone number, so if there is a situation like "SAPD24117332" you would delete anything instance of non-cell phone number, any instance like "SAPD24114836" retain all the the other non-cell phone numbers.
          Last edited by Luis Mijares Castaneda; 11 Jul 2024, 07:40.

          Comment


          • #6
            What you've described in #5 can be achieved using this code:

            Code:
            gen byte is_cell = (PHONETYPE == "Cell")
            egen byte has_cell = max(is_cell), by(CASENUM)
            
            keep if is_cell | !has_cell
            drop has_cell is_cell
            which yields, for the cases we were discussing:

            Code:
            format PHONENUMBER %15.0f
            . list if inlist(CASENUM, "SAPD24118755", "SAPD24117332", "SAPD24114836"), noobs sepby(CASENUM) abbrev(11)
            
              +----------------------------------------+
              |      CASENUM   PHONETYPE   PHONENUMBER |
              |----------------------------------------|
              | SAPD24118755        Cell   17856141487 |
              | SAPD24118755        Cell   13038701951 |
              |----------------------------------------|
              | SAPD24117332        Cell   12107711890 |
              |----------------------------------------|
              | SAPD24114836        Work   12103662304 |
              | SAPD24114836        Home      14796002 |
              | SAPD24114836       Other   12102136075 |
              | SAPD24114836       Other   12102136975 |
              +----------------------------------------+
            Last edited by Hemanshu Kumar; 11 Jul 2024, 07:51.

            Comment


            • #7
              Originally posted by Hemanshu Kumar View Post
              What you've described in #5 can be achieved using this code:

              Code:
              gen byte is_cell = (PHONETYPE == "Cell")
              egen byte has_cell = max(is_cell), by(CASENUM)
              
              keep if is_cell | !has_cell
              drop has_cell is_cell
              which yields, for the cases we were discussing:

              Code:
              format PHONENUMBER %15.0f
              . list if inlist(CASENUM, "SAPD24118755", "SAPD24117332", "SAPD24114836"), noobs sepby(CASENUM) abbrev(11)
              
              +----------------------------------------+
              | CASENUM PHONETYPE PHONENUMBER |
              |----------------------------------------|
              | SAPD24118755 Cell 17856141487 |
              | SAPD24118755 Cell 13038701951 |
              |----------------------------------------|
              | SAPD24117332 Cell 12107711890 |
              |----------------------------------------|
              | SAPD24114836 Work 12103662304 |
              | SAPD24114836 Home 14796002 |
              | SAPD24114836 Other 12102136075 |
              | SAPD24114836 Other 12102136975 |
              +----------------------------------------+
              Sorry, I also need the non-cell Phone numbers to be their own variable so a second variable named Phonenumber2 should contain the PHONENUMBERS of non-cell phone

              Comment


              • #8
                Code:
                gen byte is_cell = (PHONETYPE == "Cell")
                egen byte has_cell = max(is_cell), by(CASENUM)
                
                keep if is_cell | !has_cell
                
                gen primary_phone_no = PHONENUMBER if is_cell
                gen secondary_phone_no = PHONENUMBER if !is_cell
                drop has_cell is_cell
                which yields:

                Code:
                . format PHONENUMBER primary_phone_no secondary_phone_no %15.0f
                
                . list if inlist(CASENUM, "SAPD24118755", "SAPD24117332", "SAPD24114836"), noobs sepby(CASENUM) abbrev(18)
                
                  +--------------------------------------------------------------------------------+
                  |      CASENUM   PHONETYPE   PHONENUMBER   primary_phone_no   secondary_phone_no |
                  |--------------------------------------------------------------------------------|
                  | SAPD24118755        Cell   17856141487        17856141312                    . |
                  | SAPD24118755        Cell   13038701951        13038701568                    . |
                  |--------------------------------------------------------------------------------|
                  | SAPD24117332        Cell   12107711890        12107711488                    . |
                  |--------------------------------------------------------------------------------|
                  | SAPD24114836        Work   12103662304                  .          12103662592 |
                  | SAPD24114836        Home      14796002                  .             14796002 |
                  | SAPD24114836       Other   12102136075                  .          12102135808 |
                  | SAPD24114836       Other   12102136975                  .          12102136832 |
                  +--------------------------------------------------------------------------------+

                Comment

                Working...
                X