Announcement

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

  • Generating multiple variables with loops (repeated measures - longitudinal data)

    Hello,

    I have a dataset that is similar to the following:

    clear
    input str6 id byte(ja18aqol1 ja18aqol2 ja18aqol3 ja18aqol4 ja18aqol5 ja18aqol6 ja18aqol7 ja18aqol8 ja18aqol9 ja18aqol10 ja18aqol11 ja18aqol12)
    "Alex" 1 1 1 1 . . 1 1 . 1 1 1
    "Jack" 1 1 1 1 1 1 1 1 1 1 1 1
    "Sam" . . . . . . . . . . . .
    end
    [/CODE]

    I also have the following code (part of it):

    g Q1 = aqol1
    g Q2 = aqol2
    g Q3 = aqol3
    g Q4 = aqol4
    g Q5 = aqol5
    g Q6 = aqol6
    g Q7 = aqol7
    g Q8 = aqol8
    g Q9 = aqol9
    g Q10 = aqol10
    g Q11 = aqol11
    g Q12 = aqol12

    ************************************************** ***************
    ********* Imputing Missing Values in Database *********
    ************************************************** **************


    * Dimension 1. Independent Living

    egen IL_miss = rowmean(Q1 Q2 Q3)
    egen ILmissno = rowmiss(Q1 Q2 Q3)

    replace Q1=round(IL_miss) if missing(Q1) & ILmissno==1
    replace Q2=round(IL_miss) if missing(Q2) & ILmissno==1
    replace Q3=round(IL_miss) if missing(Q3) & ILmissno==1

    -----------------------------------------------------------------------------

    As you can see the code above starts with 'g Q1 = aqol1', but in my dataset I have ja18 as a prefix since I have a repeated measures dataset. The aqol survey questions are also repeated for april (ap18), july (ju18), october (oc18), january 2019 (ja19), and april 2019 (ap19).

    For each month, If I were to follow the code through until the end, I will end up with a single numeric score (e.g. I would have a score for january 2018, april 2018, july 2018, etc.). Is there a way to modify the code using loops/macros that will generate a score for each month?

  • #2
    So the key here is to go to a half-long layout, long on month but staying wide on question number. After you do that, you can score every month with a single set of commands. No loops needed.

    Code:
    clear
    input str6 id byte(ja18aqol1 ja18aqol2 ja18aqol3 ja18aqol4 ja18aqol5 ja18aqol6 ja18aqol7 ja18aqol8 ja18aqol9 ja18aqol10 ja18aqol11 ja18aqol12)
    "Alex" 1 1 1 1 . . 1 1 . 1 1 1
    "Jack" 1 1 1 1 1 1 1 1 1 1 1 1
    "Sam" . . . . . . . . . . . .
    end
    
    // PREPARE FOR RESHAPE BY CREATING A LIST OF QUESTIONSS
    ds ja18aqol*
    local vbles `r(varlist)'
    local vbles: subinstr local vbles "ja18" "@", all
    
    // RESHAPE LONG
    reshape long `vbles', i(id) j(month) string
    
    // SCORE EVERYTHING
    forvalues i = 1/12 {
        gen Q`i' = aqol`i'
    }
    egen IL_miss = rowmean(Q1 Q2 Q3)
    egen ILmissno = rowmiss(Q1 Q2 Q3)
    
    replace Q1=round(IL_miss) if missing(Q1) & ILmissno==1
    replace Q2=round(IL_miss) if missing(Q2) & ILmissno==1
    replace Q3=round(IL_miss) if missing(Q3) & ILmissno==1
    If you have some compelling reason to go back to wide layout after doing this, use the -reshape wide- command, including the newly created variables in it. But it is also true that almost anything you are likely to want to do with these results will also be better done in long layout, because that's what most Stata commands are designed to work with. So probably just stay with this (half-)long layout.



    Comment


    • #3
      In the first part of the code where you are preparing for a re-shape - it says ds ja18aqol*, will this cover all my variables? I also have ap18aqol*, ju18aqol*, oct18aqol*, ja19aqol*, ap19aqol*.

      When I enter the re-shape command I also receive 'invalid syntax'.

      Any thoughts?
      Last edited by CEdward; 15 Nov 2018, 18:47.

      Comment


      • #4
        It will cover all your variables provided that the * covers all the same things in each of those different months. If you look at the next two lines of code you will see that ja18 gets replaced by @. In -reshape-, @ becomes a placeholder for "whatever month". So as long as every *apqol* variable has a corresponding ja18apqol* variable, everything is covered. If that is not the case, then a different approach is:

        Code:
        ds *aqol*
        local vbles `r(varlist)'
        foreach x in ja18 ap18 ju18 oct18 ja19 ap19 {
            local vbles: subinstr local vbles "`x'" "@", all
        }
        local vbles: list uniq vbles
        Then go on to the -reshape- command from there.

        When I enter the re-shape command I also receive 'invalid syntax'.
        I suspect you are making the mistake of running this code in pieces. Code involving local macros, like vbles here, must be run in one block from beginning to end. If you interrupt the code somewhere along the way to -reshape-, then local macro vbles goes out of existence. Then, when you get to the -reshape- command `vbles' doesn't exist, so is replaced by an empty string, and the command -reshape long , i(id) j(month) string- is, indeed, a syntax error. So you have to take the entire code I gave you and run it all at once from a do-file.

        It runs without error messages using the example data you provided on my setup.

        If you run this without any interruptions from a do-file and still encounter that error, then post back after you insert -display `"`vbles'"'- on a separate line before the -reshape command-, and show the output here.

        Comment

        Working...
        X