Announcement

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

  • Creating composite variable leveraging three existing variables/timepoints

    Hello,

    I have a dataset with three variables/timepoints (as below). I need to create a composite variable, Midlife Music Play (midmusic), based on the following:

    Midlife Music Play:
    ---Continuous Play (value==1) is instrument engagement at all follow-up time points;
    ---Intermittent Play (value==2) is play at one point or another, A or B or C (but not continuously); and
    ---Never Play (value==3) is no play at any of the three timepoints
    ---Unknown (value==.)

    Note: I cannot use dataex because my data is stored on a confidential server; there is genetic data attached to my analysis.




    Variable A (2003): During the past year, how many hours per month did you play a musical instrument?

    . = missing
    -3 = refused
    -1 = don't know
    0-200



    Variable B (1994): How often did you play a musical instrument about 10 years ago?

    . = missing
    -3 = refused
    1 = Often
    2 = Rarely
    3 = Never



    Variable C (1974): How often did you play a musical instrument when you were about 35?

    . = missing
    -3 = refused
    1 = Often
    2 = Rarely
    3 = Never

    I'm struggling with the most efficient way to get arrive at what I need. I was able to brute force it, but I'd like to understand a more elegant approach.

    Thank you!


  • #2
    You can set up your conditions and use the -cond()- function. Do read https://journals.sagepub.com/doi/10....867X0500500310. Three conditions are limiting:

    ---Continuous Play (value==1) is instrument engagement at all follow-up time points;
    ---Never Play (value==3) is no play at any of the three timepoints
    and

    ---Unknown (value==.)
    You can thus concentrate on these.

    Code:
    gen wanted = ///
        cond(inrange(A, 1, 200) & B==1 & C==1, 1, ///
            cond(A==0 & B==3 & C==3, 3, ///
                cond(!inrange(A, 0, 200) & !inrange(B, 1, 3) & !inrange(C, 1, 3), .a, 2)))
    
    label values wanted wanted
    lab def  wanted 1 "Continuous Play" 2 "Intermittent Play" 3 "Never Play"  .a "Unknown", modify
    The above assumes that there are no positive nonmissing values outside the range [0, 200] for variable "A". You need to verify this. Also, you can change the number of hours of play for "continuous" to a higher value. I start at 1 hour.
    Last edited by Andrew Musau; 09 Apr 2024, 05:42.

    Comment


    • #3
      Thank you so much! My numbers are off slightly and I'm trying to understand why that is, but the code is super helpful.

      Continuous and Never Play are perfect. Interestingly, ~200 extra observations are categorized as Intermittent than I was expecting. I will continue to explore - it has to be in the values somewhere.

      Comment


      • #4
        #2 defines conditions for "Continuous", "Never Play" and "Unknown". Any other cases are categorized as "Intermittent".

        Interestingly, ~200 extra observations are categorized as Intermittent than I was expecting.
        Let us see a few of these. Since you expect them to be categorized differently, you can explain your expectation and it may be possible to revise some of the conditions subsequently.

        Comment

        Working...
        X