I'm probably not using the correct language to describe what I want, so apologies for that. Here is what I am trying to do:
I have students across terms and a variable giving how many credits they earned per term. I would like, for each term, to sum the credits for all prior terms, to give the credits earned at the beginning of the term. I can think of a way to do this with egen and total in theory, but I get stuck because the conditional that I need to sum only those credits for those terms that are less than the current term is beyond what I can remember seeing in these kinds of conditionals before, my searching online isn't quite returning what I am looking for, and I can't figure out how to implement this condition in Stata. Here is an example of what I want:
The priorcreds variable is what I want to generate. Essentially I want to sum all the credsbyterm for the same student_id as long as the term number is lower than the current term number (or return a zero if there are no prior terms), but it is that last conditional that I am struggling with. I'm hoping someone has a suggestion? Thanks in advance for any help!
I have students across terms and a variable giving how many credits they earned per term. I would like, for each term, to sum the credits for all prior terms, to give the credits earned at the beginning of the term. I can think of a way to do this with egen and total in theory, but I get stuck because the conditional that I need to sum only those credits for those terms that are less than the current term is beyond what I can remember seeing in these kinds of conditionals before, my searching online isn't quite returning what I am looking for, and I can't figure out how to implement this condition in Stata. Here is an example of what I want:
student_id | term | credsbyterm | priorcreds |
1 | 1 | 10 | 0 |
1 | 2 | 6 | 10 |
1 | 3 | 7 | 16 |
2 | 2 | 12 | 0 |
2 | 3 | 16 | 12 |
2 | 5 | 15 | 28 |
Comment