My dataset includes information about tests students have taken on their last year in school.
Here's an example of my dataset of four 12th grade student from the same school.

Code:
* Example generated by -dataex-. For more info, type help dataex
clear
input double(id test_date) float year
   7332 15131 2001
   7332 15132 2001
   7332 15136 2001
   7332 15150 2001
 318745 14741 2000
 318745 14738 2000
 318745 14746 2000
 318745 14748 2000
 318745 14761 2000
 318745 14774 2000
 318745 14782 2000
 318752 14741 2000
 318752 14751 2000
 318752 14759 2000
 318752 14765 2000
 318752 14774 2000
 318752 14782 2000
 318752 14786 2000
 318752 14800 2000
 318752 14801 2000
1055356 15102 2001
1055356 15125 2001
1055356 15138 2001
1055356 15143 2001
1055356 15147 2001
end
format %td test_date

I would like to create two variable who get the difference of days between each person tests:
1. before calculates the difference between the current test and the previous test, according to the following conditions:
a. If it is the first test for this student, assign the difference in days between the date this test was taken and the date of the first test in this school overall (regardless of course and student).
For instance, the date of the first test has taken in this school in 2000 is 08may2000 (14738) and in 2001 is 07may2001 (15102).
Thus, the first observation if each student should get:
id first test before
318745 08may2000 08may2000-08may2000=0
318752 11may2000 11may2000-08may2000=3
1055356 07may2001 07may2001-07may2001=0
7332 05jun2001 05jun2001-07may2001=29

b. If it is not the first test for this student, after calculates the difference between the current test and the previous test: test(n)-test(n-1), when n is the vector of tests for each student. For example, for id==7332, before gets 4 when test_date==10jun2001 because the previous test is 06jun2001.
2. after calculates the difference between the current test and the next test, according to the following conditions:
a. If it is the last test for this student, assign the difference in days between the date this test was taken and the date of the last test in this school overall (regardless of course and student).
For instance, the date of the last test has taken in this school in 2000 is 10jul2000 (14801) and in 2001 is 24jun2001 (15150).
Therfore, the last observation if each student should get:
id last test before
318745 21jun2000 10jul2000-21jun2000=19
318752 10jul2000 10jul2000-10jul2000=0
1055356 21jun2001 24jun2001-21jun2001=3
7332 24jun2001 24jun2001-24jun2001=0
b. If it is not the first test for this student, after calculates the difference between the current test and the next test: test(n)-test(n+1), when n is the vector of tests for each student. For example, for id==7332, before gets 1 when test_date==05jun2001 because the next test is 06jun2001.

For better understanding I suggest this chart for id==7332:
test_date before after
05jun2001 05jun2001-07may2001 = 29 06jun2001-05jun2001= 1
06jun2001 06jun2001-05jun2001= 1 10jun2001-06jun2001 = 4
10jun2001 10jun2001-06jun2001 = 4 24jun2001-10jun2001 = 14
24jun2001 24jun2001-10jun2001 = 14 24jun2001-24jun2001=0
Many thanks!