Hi,

I would appreciate if I could get help with creating new variables from rows of an existing variable. I am doing a survival analysis (recurrent events). I have nearly 45000 subjects who have intervals varying from 1 to 17. For each interval, I need to get the cumulative dose of the medication. The number of individual medication varies from 1 to 7. As I need one row for each interval per subject, I need to long format for each interval to wide format, meaning converting 'n' rows of variable drug code to 'n' columns of drug code (code1, code2, ... coden). I am unsuccessful in creating loop for each interval per subject id.

I am giving an example of dataset.

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input float(id interval code cdose)
1 1 10  100
1 1 15  150
1 1 50  200
1 2 10 1000
1 2 15 1000
2 1 10  500
2 1 30  250
2 2 25  245
2 2 30  250
2 3 25  250
2 3 30  500
3 1 10  150
3 1 20  200
3 2 10  150
4 1 15  150
4 2 25  300
4 2 30  300
end

As the intervals subject varies from subject to subject and also the number of medications used varies from interval to interval, I have been struggling to get single row for each interval.

I very much appreciate help.

With warm regards,
Sateesh