Announcement

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

  • Creating loop and generating change variable for multiple variables

    Hello,

    I am trying to create change scores (T1 - T0 ) for multiple variables and just add the extension _dlt1 to the end of each variable to delineate that it is a variable representing a change score. I am a bit lost in how to create this loop so not really sure where to start. I appreciate any suggestions. Below is the data.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte record_id float(time_rc dd1_knowledge_rc dd2_causes_rc dd3_physical_rc dd4_psych_rc dd5_riskfactors_rc dd6_counsel_rc dd7_advise_rc dd8_questrights_rc dd9_askinfo_rc dd10_persondiff_rc dd11_personrespon_rc dd12_approach_rc)
     1 0 3 6 5 6 3 6 6 2 2 6 6 6
     1 1 2 2 2 2 2 2 2 2 2 2 2 2
     2 0 5 5 6 5 6 6 6 4 4 5 6 5
     2 1 3 3 2 4 2 3 3 2 2 4 4 4
     3 0 6 6 7 7 7 7 7 1 1 2 2 2
     3 1 1 1 1 1 1 1 1 1 1 1 1 1
     4 0 3 4 3 3 4 5 4 4 4 3 4 4
     4 1 2 2 2 2 2 3 3 3 3 2 2 2
     5 0 4 6 4 5 6 5 5 2 4 3 6 5
     5 1 2 3 3 3 2 3 2 1 1 1 2 2
     6 0 2 3 5 5 3 6 2 2 3 2 2 1
     6 1 2 2 2 2 2 3 2 2 2 1 1 1
     7 0 5 5 5 3 3 6 6 3 3 3 3 3
     7 1 2 2 2 2 2 2 2 1 1 1 1 1
     8 0 3 4 4 5 3 5 5 1 1 7 2 6
     8 1 1 1 1 1 1 2 2 1 1 3 2 2
     9 0 3 4 5 5 3 5 5 2 3 6 6 6
     9 1 3 3 3 3 3 4 3 2 2 5 4 4
    10 0 3 5 6 6 5 5 6 1 2 1 2 3
    10 1 2 3 3 3 3 3 3 1 1 1 2 2
    11 0 5 6 . 6 5 6 6 5 5 6 6 6
    11 1 1 1 1 1 1 2 2 1 1 2 2 1
    12 0 5 5 3 4 5 6 5 2 2 2 3 4
    12 1 2 2 2 2 2 3 2 1 1 1 1 1
    13 0 4 3 3 3 1 5 5 2 2 1 2 4
    13 1 2 1 2 3 3 3 2 2 2 3 3 3
    end

  • #2
    Code:
    xtset record_id time_rc
    foreach v of varlist dd* {
        gen `v'_dlt1 = D1.`v'
    }

    Comment


    • #3
      Thank you, Clyde. Works great. Can you please explain how D1.`v' executes the subtraction between the variables?

      Comment


      • #4
        See -help fvvarlist-.

        Comment


        • #5
          Thank you.

          Comment

          Working...
          X