Announcement

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

  • loop for combining information from wide format variables into long format variables

    Hi there,
    I have data on whether respondents took a particular medication, frequency of consumption (as needed/daily) and duration (less than 1 month, 1-6 months, more than 6 months) for each medication in wide form. The data was only collected once.
    How can I change these variables into long form i.e., one variable for medication type, frequency and duration using loops? Thank you in advance.

    Whether taking Medication 4,5 and 7 (Yes -1;No-2)
    MHA1x3r4
    MHA1x3r5
    MHA1x3r7

    Frequency of consumption of Medication 4,5 and 7 (As needed-1;Daily-5)
    MHA1x3X1r4
    MHA1x3X1r5
    MHA1x3X1r7

    Duration of consumption of Medication 4,5, and 7 (Less than 1 month - 1;1-6 months -6;More than 6 months-7)
    MHA1x3X2r4
    MHA1x3X2r5
    MHA1x3X2r7

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input byte(MHA1x3r4 MHA1x3r5 MHA1x3r7 MHA1x3X1r4 MHA1x3X1r5 MHA1x3X1r7 MHA1x3X2r4 MHA1x3X2r5 MHA1x3X2r7)
    . . . . . . . . .
    2 1 1 . 1 1 . 1 7
    2 2 2 . . . . . .
    2 2 2 . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    2 1 1 . 5 5 . 7 6
    2 1 1 . 1 5 . 6 7
    2 1 2 . 5 . . 6 .
    2 2 1 . . 1 . . 1
    . . . . . . . . .
    1 2 1 1 . 1 6 . 7
    2 1 1 . 1 1 . 6 6
    1 2 1 1 . 5 6 . 7
    2 2 2 . . . . . .
    . . . . . . . . .
    2 1 1 . 5 5 . 7 7
    . . . . . . . . .
    1 1 2 1 1 . 6 1 .
    1 1 2 1 5 . 1 7 .
    1 2 2 1 . . 6 . .
    . . . . . . . . .
    . . . . . . . . .
    2 2 1 . . 1 . . 6
    1 2 2 5 . . 1 . .
    2 2 2 . . . . . .
    2 1 1 . 1 1 . 6 6
    . . . . . . . . .
    2 1 1 . 1 5 . 7 6
    . . . . . . . . .
    2 2 2 . . . . . .
    2 2 2 . . . . . .
    . . . . . . . . .
    2 2 2 . . . . . .
    . . . . . . . . .
    2 2 1 . . 1 . . 7
    1 1 2 5 1 . 7 6 .
    2 2 2 . . . . . .
    2 2 1 . . 1 . . 6
    1 1 2 1 1 . 6 1 .
    1 2 1 1 . 5 1 . 6
    2 1 1 . 1 5 . 6 7
    . . . . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    1 2 1 1 . 1 7 . 7
    2 2 1 . . 5 . . 7
    2 2 1 . . 1 . . 1
    1 2 1 5 . 5 7 . 7
    2 1 2 . 1 . . 1 .
    2 1 1 . 1 5 . 1 6
    1 1 2 1 1 . 6 7 .
    2 1 1 . 1 5 . 6 6
    2 1 1 . 1 5 . 6 6
    1 2 2 5 . . 7 . .
    2 1 2 . 1 . . 1 .
    2 2 1 . . 1 . . 6
    2 2 2 . . . . . .
    . . . . . . . . .
    2 2 2 . . . . . .
    . . . . . . . . .
    . . . . . . . . .
    2 2 1 . . 1 . . 7
    . . . . . . . . .
    2 1 1 . 1 1 . 1 6
    2 1 1 . 5 1 . 1 7
    2 2 2 . . . . . .
    1 2 1 1 . 5 6 . 7
    . . . . . . . . .
    1 2 2 5 . . 7 . .
    1 1 2 1 1 . 1 6 .
    . . . . . . . . .
    . . . . . . . . .
    1 2 1 5 . 5 6 . 7
    2 1 2 . 5 . . 1 .
    1 1 1 5 1 5 6 7 7
    2 2 2 . . . . . .
    1 2 2 1 . . 6 . .
    1 1 1 1 5 1 6 6 7
    . . . . . . . . .
    2 1 2 . 5 . . 1 .
    1 1 1 5 1 5 6 7 6
    1 1 1 1 5 1 1 6 7
    . . . . . . . . .
    2 1 1 . 5 1 . 6 7
    2 2 2 . . . . . .
    1 2 1 1 . 5 1 . 6
    2 2 1 . . 1 . . 6
    1 1 1 5 1 5 7 6 6
    2 2 2 . . . . . .
    1 2 2 1 . . 1 . .
    1 1 2 1 1 . 6 6 .
    2 1 2 . 1 . . 7 .
    1 1 1 1 5 1 7 6 6
    1 2 1 1 . 1 6 . 6
    . . . . . . . . .
    1 1 1 1 1 1 1 6 6
    end

  • #2
    There is no need for explicit loops here. They would only opacify the code and slow execution.

    Code:
    rename MHA1x3X2r* duration*
    rename MHA1x3X1r* frequency*
    rename MHA1x3r* whether_taking*
    
    gen `c(obs_t)' obs_no = _n
    
    reshape long whether_taking frequency duration, i(obs_no) j(med_num)
    If your data set is very large, and if you are using version 18 with the latest updates, you can speed up the -reshape- command by adding the -favor(speed)- option. If you are using version 18 but it is not fully updated, this is a good reason to update now if your setup permits. If your setup does not permit you to do the update, this particular update is well worth applying pressure on your IT department to permit the update or make it for you. If you are using an older version of Stata, you can also consider installing -greshape- (part of -gtools-) or -tolong- from SSC and use one of those instead of -reshape- for a faster experience.

    Note: If your data set already contains a variable or set of variables that uniquely identify observations, then there is no need to create the obs_no variable. Instead just put the uniquely identifying variable(s) in the -i()- option.

    Comment


    • #3
      Thank you very much Clyde. How do you denote the med_num in your example?

      Comment


      • #4
        I don't understand the question. The med_num variable will be the medication number: in the example shown it will be 4, 5, or 7. Is that what you are asking?

        Comment

        Working...
        X