Announcement

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

  • Help with creating multiple indicator variable from a string variable

    I want to generate a multiple indicator based on the values of the variable "handwashing_five_moments". For example, if the input = "1, 2, 5, 10" , I want each of the number(since it is category) to have its own separate dummy variable. My current result is as following:

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input str18 handwashing_five_moments float(handwash_0 handwash_1 handwash_2 handwash_3 handwash_4 handwash_5 handwash_6 handwash_7 handwash_10)
    "1,2,3,5"  0 1 1 1 0 1 0 0 0
    "2,5,6"    0 0 1 0 0 1 1 0 0
    "2,3,5"    0 0 1 1 0 1 0 0 0
    "1,2,5,10" 0 1 1 0 0 1 0 0 1
    "2,5,10"   0 1 1 0 0 1 0 0 1
    end
    I have run the following code:

    Code:
    foreach s in 0 1 2 3 4 5 6 7 8 9 10 { 
         gen handwash_`s' = strpos(handwashing_five_moments, "`s'") > 0 ,
    }
    The issue is that it is replacing the value for handwashing_1 == 1 even where the handwashing_five_moments does not have "1" and contains "10". How can I fix this?

  • #2
    Nothing is being replaced. But your code will see the characters 0 or 1 as part of the characters 10 even when 0 and 1 are not present as themselves. One fix for that is to change 10 to any single character other than 0 to 9, at least temporarily.

    Here is another fix. Look for a value surrounded by spaces in a version of the variable that always presents values in such manner.


    Code:
    clear
    input str18 handwashing_five_moments float(handwash_0 handwash_1 handwash_2 handwash_3 handwash_4 handwash_5 handwash_6 handwash_7 handwash_10)
    "1,2,3,5"  0 1 1 1 0 1 0 0 0
    "2,5,6"    0 0 1 0 0 1 1 0 0
    "2,3,5"    0 0 1 1 0 1 0 0 0
    "1,2,5,10" 0 1 1 0 0 1 0 0 1
    "2,5,10"   0 1 1 0 0 1 0 0 1
    end
    
    gen work = " " + subinstr(handwashing_five_moments, ",", " ", .) + " "
    
    forval x = 0/10 {
        gen ind`x' = strpos(work, " `x' ") > 0
    }
    
    l work ind1-ind3 ind5 ind6 ind10
    
         +-------------------------------------------------------+
         |       work   ind1   ind2   ind3   ind5   ind6   ind10 |
         |-------------------------------------------------------|
      1. |   1 2 3 5       1      1      1      1      0       0 |
      2. |     2 5 6       0      1      0      1      1       0 |
      3. |     2 3 5       0      1      1      1      0       0 |
      4. |  1 2 5 10       1      1      0      1      0       1 |
      5. |    2 5 10       0      1      0      1      0       1 |
         +-------------------------------------------------------+
    See also the detailed discussion at https://journals.sagepub.com/doi/pdf...6867X221141068

    Last edited by Nick Cox; 17 Jan 2023, 02:44.

    Comment


    • #3
      Try this:
      Code:
      foreach s in 0 1 2 3 4 5 6 7 8 9 10 {
           gen handwash_`s' = strpos(","+handwashing_five_moments+",", ",`s',") > 0 ,
      }
      which produces:
      Code:
      . li , noobs
        +-----------------------------------------------------------------------------------------------------------------------------------+
        | handwa~s   handw~_0   handwa~1   handwa~2   handwa~3   handwa~4   handwa~5   handwa~6   handwa~7   handwa~8   handwa~9   handw~10 |
        |-----------------------------------------------------------------------------------------------------------------------------------|
        |  1,2,3,5          0          1          1          1          0          1          0          0          0          0          0 |
        |    2,5,6          0          0          1          0          0          1          1          0          0          0          0 |
        |    2,3,5          0          0          1          1          0          1          0          0          0          0          0 |
        | 1,2,5,10          0          1          1          0          0          1          0          0          0          0          1 |
        |   2,5,10          0          0          1          0          0          1          0          0          0          0          1 |
        +-----------------------------------------------------------------------------------------------------------------------------------+
      Last edited by Hemanshu Kumar; 17 Jan 2023, 02:47.

      Comment


      • #4
        Thank you, Nick Cox & Hemanshu Kumar! Both of the suggestions worked perfectly fine.

        Comment

        Working...
        X