Announcement

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

  • How to separate a multiple response variable of numbers without duplicating their respective instances

    Hello all, I want to separate multiple responses of numbers into binary variables. However, my attempt duplicates instances. For instance, if I want to extract number 1 from a multiple response of 1 5 10, the 1 in 10 also gets counted which is an error. How do I prevent this?

    I have about 20 multiple response variables amongst several other variables, so I am looking for a way to automate the process. I have tried ODKmeta and ODKsplit but both aren't doing it well as their counts are not tallying with my manual counting.

    Thanks.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str23 fpknow str14 preginfo
    "1 6 7 10 11"     "96" 
    "2 6 7 12 20"     "1 3 5 10" 
    "7"       "1 3 5" 
    "7"       "1 3 5 10" 
    ""        ""      
    end
    Below is my code
    Code:
    local mult fpknow preginfo
    foreach a of local mult {
        local num "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 96 98"
        display "`num'"
            foreach b of local num {
                cap drop `a'_`b'
                cap confirm numeric variable `a'
                if _rc{
                local `b'
                local `a'
                gen `a'_`b' = 0 if `a'!="" 
                replace `a'_`b' = 1 if strmatch(`a', "*`b'*") 
                }
            }
    }
    Observe that the number of 1's in the original fpknow and the binary extraction of 1 does not tally in the result below.

    Results
    Code:
    . ta fpknow
    
                     fpknow |      Freq.     Percent        Cum.
    ------------------------+-----------------------------------
                1 6 7 10 11 |          1       25.00       25.00
                2 6 7 12 20 |          1       25.00       50.00
                          7 |          2       50.00      100.00
    ------------------------+-----------------------------------
                      Total |          4      100.00
    
    . ta fpknow_1
    
       fpknow_1 |      Freq.     Percent        Cum.
    ------------+-----------------------------------
              0 |          2       50.00       50.00
              1 |          2       50.00      100.00
    ------------+-----------------------------------
          Total |          4      100.00

  • #2
    Do you want to indicate the presence of a number or count how many times it appears in an observation? If the former:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str23 fpknow str14 preginfo
    "1 6 7 10 11"     "96" 
    "2 6 7 12 20"     "1 3 5 10" 
    "7"       "1 3 5" 
    "7"       "1 3 5 10" 
    ""        ""      
    end
    
    foreach num of numlist 1 7 12 20{
        gen has`num'= ustrregexm(fpknow, "\b`num'\b")
    }
    Res.:

    Code:
    . l fp h*
    
         +-------------------------------------------+
         |      fpknow   has1   has7   has12   has20 |
         |-------------------------------------------|
      1. | 1 6 7 10 11      1      1       0       0 |
      2. | 2 6 7 12 20      0      1       1       1 |
      3. |           7      0      1       0       0 |
      4. |           7      0      1       0       0 |
      5. |                  0      0       0       0 |
         +-------------------------------------------+

    Comment


    • #3
      So, I resorted to using regexm as below but 1 is still a problem. regexm seem to be almost there but for number 1. Attached is the data table to show the result of regexm for fpknow

      Code:
      set trace on
      local mult fpknow preginfo
      foreach a of local mult {
          local num "1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 96 98"
          display "`num'"
              foreach b of local num {
                  cap drop `a'_`b'
                  cap confirm numeric variable `a'
                  if _rc{
                  local `b'
                  local `a'
                  gen `a'_`b' = 0 if `a'!=""
                  replace `a'_`b' = 1 if regexm(`a',"`b'") & `a'!=""
                  }
              }
      }
      Click image for larger version

Name:	Screenshot 2024-09-21 185722.jpg
Views:	1
Size:	33.3 KB
ID:	1764204
      Last edited by Kehinde Atoloye; 21 Sep 2024, 12:04.

      Comment


      • #4
        Originally posted by Andrew Musau View Post
        Do you want to indicate the presence of a number or count how many times it appears in an observation? If the former:

        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input str23 fpknow str14 preginfo
        "1 6 7 10 11" "96"
        "2 6 7 12 20" "1 3 5 10"
        "7" "1 3 5"
        "7" "1 3 5 10"
        "" ""
        end
        
        foreach num of numlist 1 7 12 20{
        gen has`num'= ustrregexm(fpknow, "\b`num'\b")
        }
        Res.:

        Code:
        . l fp h*
        
        +-------------------------------------------+
        | fpknow has1 has7 has12 has20 |
        |-------------------------------------------|
        1. | 1 6 7 10 11 1 1 0 0 |
        2. | 2 6 7 12 20 0 1 1 1 |
        3. | 7 0 1 0 0 |
        4. | 7 0 1 0 0 |
        5. | 0 0 0 0 |
        +-------------------------------------------+
        Thanks a million. That's it.

        I have modified it to cater for multiple variables.

        Code:
        local mult fpknow preginfo 
        foreach a of local mult{
            foreach num of numlist 1 7 12 20 {
                gen `a'`num'= ustrregexm(`a', "\b`num'\b") if `a'!="" 
            }
        }

        Comment

        Working...
        X