Announcement

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

  • generating a new variable by combining several variables

    I have been working on generating a new variable where 1 group would be divided into 2 subtypes and have tried several iterations, but code is not generating correct #s.

    I have 1 variable with presence or absence of movements or sounds: MS where MS==0 means not present and MS==1 means present.

    I also have 2 separate variables for severity of the movements or sounds: m_only (range 0-25) and s_only (range 0-25)

    I want to create a new variable with individuals with MS only==1 and divide the group into categories based on their severity scores. 1= individuals with MS with motor only scores and 2= MS with vocal only scores and 3=MS individuals that answered both motor and vocal severity categories.

    motor
    generate m_New=.
    replace m_New=1 if m_only==0 | m_only>=1.
    replace m_New=. if m_only==.

    (I had tried replace m_onlyN=1 if m_only>=0 and #s were not correct. above code generated correct total N for motor category)

    sounds
    generate s_New=.
    replace s_New=1 if s_only==0 | s_only>=1
    replace s_New=. if s_only==.

    A few of the codes I have tried
    #1
    generate MS_new=.
    replace MS_new=1 if MS==1 & m_NEW==1
    replace MS_new=2 if MS==1 & s_NEW==1
    replace MS_new=3 if MS==1 & m_NEW==1 & s_NEW==1

    #2
    generate MS_new=.
    replace MS_new=1 if MS==1 & m_NEW==1 & m_NEW!=. & s_NEW!=1
    replace MS_new=2 if MS==1 & s_NEW==1 & s_NEW!=. & m_NEW!=1
    replace MS_new=3 if MS==1 & m_NEW==1 & s_NEW==1

    #3 tried to generate new category without the MS category
    generate m_vs_s=.
    replace m_vs_s=1 if m_only>=0
    replace m_v_s=2 if s_only>=0


    I have tried different iterations and code not generating what I am hoping to do. I appreciate anyone's help/suggestions.


  • #2
    does this give the desired result,
    Code:
    g wanted = .
    replace wanted = 1 if MS & m_only & !s_only
    replace wanted = 2 if MS & s_only & !m_only
    replace wanted = 3 if MS & m_only & s_only
    ?

    Comment


    • #3
      I thought it would work, but it didn't. I appreciate the help/thought.

      Comment


      • #4
        I think the reason you have not yet gotten a solution here is that your description in #1 is unclear and at points seems to contradict itself about what is wanted. I suggest that you post back showing a brief example data set that is reasonably representative of the full data set, and then show what you want the new variable's values to be in the example. When you do that, have the example include some observations where the code in #2 produces incorrect results. That is likely to be more fruitful than further attempts to explain it in words.
        Last edited by Clyde Schechter; 09 Dec 2021, 16:13.

        Comment

        Working...
        X