Announcement

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

  • Help with expand function

    Hello! I am new to the forum, and greatly appreciate your help. This is what my physical activity data looks like for 2 IDs: (the xx are actual numbers in my data)
    ID visit instance Changes_since_last_visit activity time_per_month mins Mets
    1 1 1 No
    1 2 1 No
    1 3 1 No
    1 4 1 No
    1 5 1 No
    1 6 1 Yes running with dog
    1 6 2 Yes walking to school
    1 6 3 Yes rowing machine
    1 6 4 Yes spin class
    1 screening 1 NA running with dog xx xx 3.4
    1 screening 2 NA walking to school xx xx 8
    1 screening 3 NA rowing machine xx xx 5
    1 screening 4 NA spin class xx xx 7
    3 screening 1 NA walking to school xx xx 2
    3 screening 2 NA speed walking xx xx 3.4
    3 screening 3 NA Housekeeping xx xx 2.9
    3 1 1 No
    3 2 1 Yes walking to school xx xx 2
    3 2 2 Yes speed walking xx xx 3.4
    3 2 3 Yes Housekeeping
    3 3 1 Yes walking to school xx xx 2
    3 3 2 Yes Housekeeping xx xx 2.9
    3 3 3 Yes speed walking
    3 4 1 No
    3 5 1 No
    3 6 1 No
    Each ID attended 6 visits and a screening visit, and each different physical activity has it's own row. The variables ID, visit and instance uniquely identify observations.

    My issue is with the 'Changes_since_last_visit' column. Essentially, at their screening visit we would ask them what activity they do, then at the next visit we would ask them if they still do that activity. What I would like to do seems simple, but it's not working. If there were no changes in physical activity since their last visit, I want to copy the rows of their last visit, replace the 'visit' column with the correct visit number, and delete the original visit column. for ID=1; the screening data should be copied to visit=1, 2, 3, 4, and 5. For ID=3; the screening data should be expanded to visit 1 and the visit 3 data should be expanded to visit 4, 5, and 6.

    Here is the code that is not working:

    Code:
    if visit==1 & Changes_since_last_visit=="No" {
            expand 2 if visit=="screening", generate (original_copy)
            sort BlindedID visit instance
    }
    replace visit = 1 if original_copy==1 
    drop if visit ==1 & original_copy==2 & Changes_since_last_visit=="No"
    (then I would repeat this code for all other visits)

    However, when I run this, nothing happens at all and I don't get any errors. Maybe I'm too tired and I've been looking at this problem for too long, but any help is greatly appreciated!

  • #2
    I cannot follow this and I suspect this is true for most other people. Input the table above into Stata and run

    Code:
    dataex
    [.] Then copy and paste the output here. In addition, input the desired table into Stata and again run dataex, also copying and pasting the output here. So in total, we need to see two tables, the original and how you want the table to look like. See FAQ Advice #12 for more details on the dataex command.

    Comment


    • #3
      Thank you for your comment, and apologies for the confusion! Here are the dataex output.

      What I have:

      ----------------------- copy starting from the next line -----------------------
      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte id str9 visit byte instance str3 changes_since_last_visit str17 activity str2(time_per_month mins) float mets
      1 "1"         1 "No"  ""                  ""   ""     .
      1 "2"         1 "No"  ""                  ""   ""     .
      1 "3"         1 "No"  ""                  ""   ""     .
      1 "4"         1 "No"  ""                  ""   ""     .
      1 "5"         1 "No"  ""                  ""   ""     .
      1 "6"         1 "Yes" "running with dog"  ""   ""     .
      1 "6"         2 "Yes" "walking to school" ""   ""     .
      1 "6"         3 "Yes" "rowing machine"    ""   ""     .
      1 "6"         4 "Yes" "spin class"        ""   ""     .
      1 "screening" 1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "screening" 2 "NA"  "walking to school" "xx" "xx"   8
      1 "screening" 3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "screening" 4 "NA"  "spin class"        "xx" "xx"   7
      3 "screening" 1 "NA"  "walking to school" "xx" "xx"   2
      3 "screening" 2 "NA"  "speed walking"     "xx" "xx" 3.4
      3 "screening" 3 "NA"  "Housekeeping"      "xx" "xx" 2.9
      3 "1"         1 "No"  ""                  ""   ""     .
      3 "2"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "2"         2 "Yes" "speed walking"     "xx" "xx" 3.4
      3 "2"         3 "Yes" "Housekeeping"      ""   ""     .
      3 "3"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "3"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
      3 "3"         3 "Yes" "speed walking"     ""   ""     .
      3 "4"         1 "No"  ""                  ""   ""     .
      3 "5"         1 "No"  ""                  ""   ""     .
      3 "6"         1 "No"  ""                  ""   ""     .
      end
      What I want:

      Code:
      * Example generated by -dataex-. For more info, type help dataex
      clear
      input byte id str9 visit byte instance str3 changes_since_last_visit str17 activity str2(time_per_month mins) float mets
      1 "1"         1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "1"         2 "NA"  "walking to school" "xx" "xx"   8
      1 "1"         3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "1"         4 "NA"  "spin class"        "xx" "xx"   7
      1 "2"         1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "2"         2 "NA"  "walking to school" "xx" "xx"   8
      1 "2"         3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "2"         4 "NA"  "spin class"        "xx" "xx"   7
      1 "3"         1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "3"         2 "NA"  "walking to school" "xx" "xx"   8
      1 "3"         3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "3"         4 "NA"  "spin class"        "xx" "xx"   7
      1 "4"         1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "4"         2 "NA"  "walking to school" "xx" "xx"   8
      1 "4"         3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "4"         4 "NA"  "spin class"        "xx" "xx"   7
      1 "5"         1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "5"         2 "NA"  "walking to school" "xx" "xx"   8
      1 "5"         3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "5"         4 "NA"  "spin class"        "xx" "xx"   7
      1 "6"         1 "Yes" "running with dog"  ""   ""     .
      1 "6"         2 "Yes" "walking to school" ""   ""     .
      1 "6"         3 "Yes" "rowing machine"    ""   ""     .
      1 "6"         4 "Yes" "spin class"        ""   ""     .
      1 "screening" 1 "NA"  "running with dog"  "xx" "xx" 3.4
      1 "screening" 2 "NA"  "walking to school" "xx" "xx"   8
      1 "screening" 3 "NA"  "rowing machine"    "xx" "xx"   5
      1 "screening" 4 "NA"  "spin class"        "xx" "xx"   7
      3 "1"         1 "NA"  "walking to school" "xx" "xx"   2
      3 "1"         2 "NA"  "speed walking"     "xx" "xx" 3.4
      3 "1"         3 "NA"  "Housekeeping"      "xx" "xx" 2.9
      3 "2"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "2"         2 "Yes" "speed walking"     "xx" "xx" 3.4
      3 "2"         3 "Yes" "Housekeeping"      ""   ""     .
      3 "3"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "3"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
      3 "3"         3 "Yes" "speed walking"     ""   ""     .
      3 "4"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "4"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
      3 "4"         3 "Yes" "speed walking"     ""   ""     .
      3 "5"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "5"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
      3 "5"         3 "Yes" "speed walking"     ""   ""     .
      3 "6"         1 "Yes" "walking to school" "xx" "xx"   2
      3 "6"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
      3 "6"         3 "Yes" "speed walking"     ""   ""     .
      3 "screening" 1 "NA"  "walking to school" "xx" "xx"   2
      3 "screening" 2 "NA"  "speed walking"     "xx" "xx" 3.4
      3 "screening" 3 "NA"  "Housekeeping"      "xx" "xx" 2.9
      end

      Comment


      • #4
        Thanks for the data example. I assume that the last visit is what is relevant.


        Code:
        * Example generated by -dataex-. For more info, type help dataex
        clear
        input byte id str9 visit byte instance str3 changes_since_last_visit str17 activity str2(time_per_month mins) float mets
        1 "1"         1 "No"  ""                  ""   ""     .
        1 "2"         1 "No"  ""                  ""   ""     .
        1 "3"         1 "No"  ""                  ""   ""     .
        1 "4"         1 "No"  ""                  ""   ""     .
        1 "5"         1 "No"  ""                  ""   ""     .
        1 "6"         1 "Yes" "running with dog"  ""   ""     .
        1 "6"         2 "Yes" "walking to school" ""   ""     .
        1 "6"         3 "Yes" "rowing machine"    ""   ""     .
        1 "6"         4 "Yes" "spin class"        ""   ""     .
        1 "screening" 1 "NA"  "running with dog"  "xx" "xx" 3.4
        1 "screening" 2 "NA"  "walking to school" "xx" "xx"   8
        1 "screening" 3 "NA"  "rowing machine"    "xx" "xx"   5
        1 "screening" 4 "NA"  "spin class"        "xx" "xx"   7
        3 "screening" 1 "NA"  "walking to school" "xx" "xx"   2
        3 "screening" 2 "NA"  "speed walking"     "xx" "xx" 3.4
        3 "screening" 3 "NA"  "Housekeeping"      "xx" "xx" 2.9
        3 "1"         1 "No"  ""                  ""   ""     .
        3 "2"         1 "Yes" "walking to school" "xx" "xx"   2
        3 "2"         2 "Yes" "speed walking"     "xx" "xx" 3.4
        3 "2"         3 "Yes" "Housekeeping"      ""   ""     .
        3 "3"         1 "Yes" "walking to school" "xx" "xx"   2
        3 "3"         2 "Yes" "Housekeeping"      "xx" "xx" 2.9
        3 "3"         3 "Yes" "speed walking"     ""   ""     .
        3 "4"         1 "No"  ""                  ""   ""     .
        3 "5"         1 "No"  ""                  ""   ""     .
        3 "6"         1 "No"  ""                  ""   ""     .
        end
        
        preserve
        gen long order=_n
        bys id (changes_since_last_visit order): gen which= visit[_N] if changes_since_last_visit=="Yes"
        keep if visit==which
        drop visit
        tempfile join
        save `join'
        restore
        contract id visit, nomiss
        joinby id using `join'
        replace changes_since_last_visit="NA" if visit !=which
        drop which order _freq
        Res.:

        Code:
        . sort id visit instance
        
        . l, sepby(id)
        
             +-----------------------------------------------------------------------------------+
             | id       visit   instance   change~t            activity   time_p~h   mins   mets |
             |-----------------------------------------------------------------------------------|
          1. |  1           1          1         NA    running with dog                        . |
          2. |  1           1          2         NA   walking to school                        . |
          3. |  1           1          3         NA      rowing machine                        . |
          4. |  1           1          4         NA          spin class                        . |
          5. |  1           2          1         NA    running with dog                        . |
          6. |  1           2          2         NA   walking to school                        . |
          7. |  1           2          3         NA      rowing machine                        . |
          8. |  1           2          4         NA          spin class                        . |
          9. |  1           3          1         NA    running with dog                        . |
         10. |  1           3          2         NA   walking to school                        . |
         11. |  1           3          3         NA      rowing machine                        . |
         12. |  1           3          4         NA          spin class                        . |
         13. |  1           4          1         NA    running with dog                        . |
         14. |  1           4          2         NA   walking to school                        . |
         15. |  1           4          3         NA      rowing machine                        . |
         16. |  1           4          4         NA          spin class                        . |
         17. |  1           5          1         NA    running with dog                        . |
         18. |  1           5          2         NA   walking to school                        . |
         19. |  1           5          3         NA      rowing machine                        . |
         20. |  1           5          4         NA          spin class                        . |
         21. |  1           6          1        Yes    running with dog                        . |
         22. |  1           6          2        Yes   walking to school                        . |
         23. |  1           6          3        Yes      rowing machine                        . |
         24. |  1           6          4        Yes          spin class                        . |
         25. |  1   screening          1         NA    running with dog                        . |
         26. |  1   screening          2         NA   walking to school                        . |
         27. |  1   screening          3         NA      rowing machine                        . |
         28. |  1   screening          4         NA          spin class                        . |
             |-----------------------------------------------------------------------------------|
         29. |  3           1          1         NA   walking to school         xx     xx      2 |
         30. |  3           1          2         NA        Housekeeping         xx     xx    2.9 |
         31. |  3           1          3         NA       speed walking                        . |
         32. |  3           2          1         NA   walking to school         xx     xx      2 |
         33. |  3           2          2         NA        Housekeeping         xx     xx    2.9 |
         34. |  3           2          3         NA       speed walking                        . |
         35. |  3           3          1        Yes   walking to school         xx     xx      2 |
         36. |  3           3          2        Yes        Housekeeping         xx     xx    2.9 |
         37. |  3           3          3        Yes       speed walking                        . |
         38. |  3           4          1         NA   walking to school         xx     xx      2 |
         39. |  3           4          2         NA        Housekeeping         xx     xx    2.9 |
         40. |  3           4          3         NA       speed walking                        . |
         41. |  3           5          1         NA   walking to school         xx     xx      2 |
         42. |  3           5          2         NA        Housekeeping         xx     xx    2.9 |
         43. |  3           5          3         NA       speed walking                        . |
         44. |  3           6          1         NA   walking to school         xx     xx      2 |
         45. |  3           6          2         NA        Housekeeping         xx     xx    2.9 |
         46. |  3           6          3         NA       speed walking                        . |
         47. |  3   screening          1         NA   walking to school         xx     xx      2 |
         48. |  3   screening          2         NA        Housekeeping         xx     xx    2.9 |
         49. |  3   screening          3         NA       speed walking                        . |
             +-----------------------------------------------------------------------------------+
        
        .
        Last edited by Andrew Musau; 04 May 2023, 08:08.

        Comment


        • #5
          Thank you so much! Your solution works for the 2 IDs I had as an example here, but falls apart when an ID has >1 changes_since_last_visit. For example:
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int ID str9 visit byte instance str8 par_changes byte ongoing str15 par_activity_name byte(par_no_times_month par_mins_times par_mets par_mets_01)
          6 "screening" 1 ""    . "walking dog" 15 20 2 .
          6 "screening" 2 ""    . "dancing"     15 20 8 .
          6 "V1"        1 "No"  . ""             .  . . .
          6 "V2"        1 "No"  . ""             .  . . .
          6 "V3"        1 "Yes" . "walking dog"  .  . . .
          6 "V3"        2 "Yes" . "dancing"      .  . . .
          6 "V4"        1 "Yes" . "walking dog" 30 13 2 .
          6 "V4"        2 "Yes" . "dancing"     30 13 8 .
          6 "V5"        1 "No"  . ""             .  . . .
          6 "V6"        1 "Yes" . "walking dog" 30 25 2 .
          6 "V6"        2 "Yes" . "dancing"     30 25 8 .
          end
          should look like:
          Code:
          * Example generated by -dataex-. For more info, type help dataex
          clear
          input int ID str9 visit byte instance str8 par_changes str15 par_activity_name byte(par_no_times_month par_mins_times par_mets par_mets_01)
          6 "1"         1 "No"  "walking dog" 15 20 2 .
          6 "1"         2 "No"  "dancing"     15 20 8 .
          6 "2"         1 "No"  "walking dog" 15 20 2 .
          6 "2"         2 "No"  "dancing"     15 20 8 .
          6 "3"         1 "Yes" "walking dog"  .  . . .
          6 "3"         2 "Yes" "dancing"      .  . . .
          6 "4"         1 "Yes" "walking dog" 30 13 2 .
          6 "4"         2 "Yes" "dancing"     30 13 8 .
          6 "5"         1 "No"  "walking dog" 30 13 2 .
          6 "5"         2 "No"  "dancing"     30 13 8 .
          6 "6"         1 "Yes" "walking dog" 30 25 2 .
          6 "6"         2 "Yes" "dancing"     30 25 8 .
          6 "screening" 1 ""    "walking dog" 15 20 2 .
          6 "screening" 2 ""    "dancing"     15 20 8 .
          end
          Thank you so much for your help, but I fear the only solution is to do this manually for each ID I have...
          Last edited by Sabrina Ayoub-Charette; 04 May 2023, 11:11.

          Comment


          • #6
            The issue isn't whether some code can be written. It is all about understanding your question, and we can only do this to the extent that your examples correspond to your actual problem. It appears that you want to copy previous observations given that par_changes= "No". From your title, note that expand is a command and not a function in Stata speak.

            Code:
            * Example generated by -dataex-. To install: ssc install dataex
            clear
            input int ID str9 visit byte instance str8 par_changes byte ongoing str15 par_activity_name byte(par_no_times_month par_mins_times par_mets par_mets_01)
            6 "screening" 1 ""    . "walking dog" 15 20 2 .
            6 "screening" 2 ""    . "dancing"     15 20 8 .
            6 "V1"        1 "No"  . ""             .  . . .
            6 "V2"        1 "No"  . ""             .  . . .
            6 "V3"        1 "Yes" . "walking dog"  .  . . .
            6 "V3"        2 "Yes" . "dancing"      .  . . .
            6 "V4"        1 "Yes" . "walking dog" 30 13 2 .
            6 "V4"        2 "Yes" . "dancing"     30 13 8 .
            6 "V5"        1 "No"  . ""             .  . . .
            6 "V6"        1 "Yes" . "walking dog" 30 25 2 .
            6 "V6"        2 "Yes" . "dancing"     30 25 8 .
            end
            
            gen group= cond(visit=="screening", 0, real(substr(visit, 2, 1)))
            drop if par_changes=="No"
            bys ID (group): gen toexpand=group[_n+1] - group
            by ID group: replace toexpand=toexpand[_N]
            expand toexpand, gen(new)
            bys ID group instance: replace group= group[_n-1] + 1 if _n>1
            replace visit=cond(!group, "screening", "V"+ string(group))
            replace par_changes="" if new
            drop toexpand new 
            sort ID visit instance
            Res.:

            Code:
            . l, sepby(ID)
            
                 +------------------------------------------------------------------------------------------------------------------+
                 | ID       visit   instance   par_ch~s   ongoing   par_activ~e   par_no~h   par_mi~s   par_mets   par_m~01   group |
                 |------------------------------------------------------------------------------------------------------------------|
              1. |  6          V1          1                    .   walking dog         15         20          2          .       1 |
              2. |  6          V1          2                    .       dancing         15         20          8          .       1 |
              3. |  6          V2          1                    .   walking dog         15         20          2          .       2 |
              4. |  6          V2          2                    .       dancing         15         20          8          .       2 |
              5. |  6          V3          1        Yes         .   walking dog          .          .          .          .       3 |
              6. |  6          V3          2        Yes         .       dancing          .          .          .          .       3 |
              7. |  6          V4          1                    .   walking dog         30         13          2          .       4 |
              8. |  6          V4          2                    .       dancing         30         13          8          .       4 |
              9. |  6          V5          1        Yes         .   walking dog         30         13          2          .       5 |
             10. |  6          V5          2        Yes         .       dancing         30         13          8          .       5 |
             11. |  6          V6          1        Yes         .   walking dog         30         25          2          .       6 |
             12. |  6          V6          2        Yes         .       dancing         30         25          8          .       6 |
             13. |  6   screening          1                    .   walking dog         15         20          2          .       0 |
             14. |  6   screening          2                    .       dancing         15         20          8          .       0 |
                 +------------------------------------------------------------------------------------------------------------------+

            Comment


            • #7
              thank you!

              Comment

              Working...
              X