Dear Statalist,

I am currently using a dataset that includes positional data of individuals in different buildings. For each individual, I have the x and y coordinates in a given building for a specific point in time. I am now interested in calculating the distance that a given individual covers in a specific building. For simplicity, I am assuming that each individual moves in a straight line.


Each individual starts from the entrance (x = 0 and y = 0). So the distance for the first observation is the length of the vector (x,y):

Code:
gen distance = sqrt((position_x-0)^2+(position_y-0)^2)
However, for the next subsequent lines in the dataset, I need to take the previous position of the individual into account as indicated in the previous row (i.e., the x,y coordinates of the previous row rather than the entrance coordinates). Hence, I am wondering how I could calculate this variable by referencing to the previous line. I posted the desired result in the column "distance" below:

Code:
* Example generated by -dataex-. To install: ssc install dataex
clear
input byte ind_id double(position_x position_y) str1 building int time double distance
1 612205.1 616313.1 "a" 172  868698.4066392777
1 612033.3 616526.2 "a" 773 273.72769315501506
2 623037.9 297557.4 "b"  56  690446.6895649295
2 618588.2 304253.1 "b" 123  8039.417179124367
2 624784.1 302962.8 "b" 199  6328.826976620569
2 621880.7 306320.6 "c"  37  693230.0592277069
end

I suppose this requires some form of loop. Any help with this problem would be highly appreciated.

Best,

Marvin