Announcement

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

  • Calculating change scores in long format

    Hello, I am trying to calculate the change between 2 timepoints for an outcome variable in a long format data file.

    I have used the following code to calculate the change between consecutive time points.

    xtset record_id time_rc
    capt drop ddppq_tot_dlt
    gen ddppq_tot_dlt = D.ddppq_tot
    replace ddppq_tot_dlt = ddppq_tot_dlt[_n+1] if missing(ddppq_tot_dlt) & record_id == record_id
    order ddppq_tot_dlt, after (ddppq_tot)

    HOWEVER, I want to now calculate the ddppq_tot change (time_rc == 1 - time_rc == 0 AND time_rc == 2 - time_rc == 0)

    I can get the following code to run but, of course, the new variable is deleted from the dataset after running.

    preserve
    Keep record_id time_rc ddppq_tot
    sort record_id time_rc
    reshape wide ddppq_tot, i(record_id) j(time_rc)
    gen ddppq_tot_d1=ddppq_tot1 - ddppq_tot0
    restore

    When I run (I have many variables in the dataset):

    sort record_id time_rc
    reshape wide ddppq_tot, i(record_id) j(time_rc)
    gen ddppq_tot_d1=ddppq_tot1 - ddppq_tot0

    I get the error: There are variables other than a, b, record_id, time_rc in your data. They must be constant within record_id because that is
    the only way they can fit into wide data without loss of information.

    The variable or variables listed above are not constant within record_id. Perhaps the values are in error. Type reshape error
    for a list of the problem observations.

    Either that, or the values vary because they should vary, in which case you must either add the variables to the list of xij
    variables to be reshaped, or drop them.


    I appreciate any advice.

    Code:
    * Example generated by -dataex-. For more info, type help dataex
    clear
    input byte record_id float(time_rc freq1_ask_add1)
     1 0 2
     1 1 4
     1 2 5
     2 0 1
     2 1 2
     2 2 2
     3 0 1
     3 1 2
     3 2 2
     4 0 1
     4 1 3
     4 2 3
     5 0 2
     5 1 4
     5 2 4
     6 0 2
     6 1 4
     6 2 6
     7 0 2
     7 1 3
     7 2 3
     8 0 2
     8 1 3
     8 2 3
     9 0 2
     9 1 3
     9 2 4
    10 0 1
    10 1 2
    10 2 2
    11 0 2
    11 1 5
    11 2 4
    12 0 2
    12 1 5
    12 2 6
    13 0 3
    13 1 4
    13 2 4
    end
Working...
X