Announcement

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

  • Compute New Variable - Based on Responses to Other Variables


    Hello Listservers,


    i have 3 variables:

    X (use of anal-condom)
    Y (use of oral condom)
    Z (use of vaginal condom)

    Each as is coded 1=Yes, 2=No, 0,8,9=Missing.

    I want to create a new variable that is set to 1 (CONDUSE = 1), if the respondent uses condom for any type of sex, 2, if not. Here is an example of the scratch data

    Code:
           | ANALCOND   ORALCOND   VAGCOND   conduse |
           |-----------------------------------------|
    221. |        0          0         0         . |
    222. |        0          0         0         . |
    223. |        0          0         0         . |
    224. |        0          0         0         . |
    225. |        0          0         0         . |
         |-----------------------------------------|
    226. |        0          2         2         2 |
    227. |        0          0         0         . |
    228. |        0          1         1         1 |
    229. |        0          2         1         2 |
    230. |        0          0         0         . |
         |-----------------------------------------|
    231. |        0          0         0         . |
    232. |        0          0         0         . |
    233. |        0          2         2         2 |
    234. |        0          0         0         . |
    235. |        0          1         1         1 |
         |-----------------------------------------|

    I tried the following command:


    Code:
    /* Create conduse = use of condom */
    gen conduse=1 if ANALCOND==1 | ORALCOND==1 | VAGCOND==1
    replace conduse=2 if ANALCOND==2 | ORALCOND==2 | VAGCOND==2
    fre conduse
    However but I am not sure if it if counting those who are not using condoms appropriately. For example, as you can see Case # 229 uses condom both orally and vaginally, but it is still coded 2 (not using condom).

    I will appreciate some help from you all.

    thanks - Cy






  • #2
    Cy:
    you may need something along the following lines:
    Code:
     egen conduse=rowmax(ANALCOND   ORALCOND   VAGCOND)
    This code will report -conduse-= 0 for those units who answered 0 to all the questions.
    As far as #229 is concerned, the responder reported 2 and 1; so according to your research hypothesis (ie, safe sex in all the proposed scenarios) she/he should be classified as 2 in -conduse-.
    Kind regards,
    Carlo
    (Stata 19.0)

    Comment


    • #3
      My guess is that you can code in terms of these rules:

      Rule 1. If any of the variables is 1, return 1.

      Rule 2. Otherwise, if all are the variables are 2, return 2.

      Rule 3. Otherwise return missing.

      My code for this is as below. Please notice that I had to conduct surgery on your data example, which is helpful but could be more so. FAQ Advice #12 does spell out our request that you use dataex.


      Code:
      * Example generated by -dataex-. To install: ssc install dataex
      clear
      input float(ANALCOND ORALCOND VAGCOND conduse)
      0 0 0 .
      0 0 0 .
      0 0 0 .
      0 0 0 .
      0 0 0 .
      0 2 2 2
      0 0 0 .
      0 1 1 1
      0 2 1 2
      0 0 0 .
      0 0 0 .
      0 0 0 .
      0 2 2 2
      0 0 0 .
      0 1 1 1
      end
      
      gen wanted = .
      
      quietly foreach v of var *COND {
          replace wanted = min(wanted, `v') if inlist(`v', 1, 2)
      }
      
      list
       
           +--------------------------------------------------+
           | ANALCOND   ORALCOND   VAGCOND   conduse   wanted |
           |--------------------------------------------------|
        1. |        0          0         0         .        . |
        2. |        0          0         0         .        . |
        3. |        0          0         0         .        . |
        4. |        0          0         0         .        . |
        5. |        0          0         0         .        . |
           |--------------------------------------------------|
        6. |        0          2         2         2        2 |
        7. |        0          0         0         .        . |
        8. |        0          1         1         1        1 |
        9. |        0          2         1         2        1 |
       10. |        0          0         0         .        . |
           |--------------------------------------------------|
       11. |        0          0         0         .        . |
       12. |        0          0         0         .        . |
       13. |        0          2         2         2        2 |
       14. |        0          0         0         .        . |
       15. |        0          1         1         1        1 |
           +--------------------------------------------------+
      Note how the min() function will select 1 rather than 2 and that the if condition ignores 0.

      All that said, it's much, much easier to store indicators as 1 if true and 0 if false as then much in Stata and statistical computing is really easy.

      See e.g.

      https://www.stata.com/support/faqs/d...rue-and-false/

      https://www.stata.com/support/faqs/d...ble-recording/




      Last edited by Nick Cox; 20 Mar 2018, 12:39.

      Comment


      • #4
        Thanks so much for all your assistance - they all worked. Best, cY

        Comment

        Working...
        X