I have a SAS dataset that looks like:
Obs qy Time beginning end
1 2008 Q1 2008Q1 1 1
2 2008 Q1 2008Q1 1 2
3 2008 Q2 2008Q2 1 1
4 2008 Q2 2008Q2 1 2
5 2008 Q2 2008Q2 1 1
6 2008 Q3 2008Q3 1 1
7 2008 Q4 2008Q4 1 1
8 2008 Q4 2008Q4 1 2
9 2008 Q4 2008Q4 1 1
10 2009 Q1 2009Q1 1 1
and I would like to have some new variables containing the accounts that had beginning=1 and end=2 per quarter, for the n previous quarters (n could be 1,2,3,4 etc). How can I do that?
Desired output:
Obs qy Time beginning end one_quarter_back
1 2008 Q1 2008Q1 1 1 0.25
2 2008 Q1 2008Q1 1 2 0.25
3 2008 Q2 2008Q2 1 1 0.5
4 2008 Q2 2008Q2 1 2 0.5
5 2008 Q2 2008Q2 1 1 0.5
6 2008 Q3 2008Q3 1 1 0.33
7 2008 Q4 2008Q4 1 1 0.4
8 2008 Q4 2008Q4 1 2 0.4
9 2008 Q4 2008Q4 1 1 0.4
10 2009 Q1 2009Q1 1 1 0.15
(The above assuming that the respective calculations led to these numbers, and we would have similar for 2,3,4 etc quarters back)
You can use the LAG function. LAG enables you to retrieve previous values.
If you use LAG or LAG1, you are looking for that column's value from the previous row. LAG2 gives you the previous value two rows back. LAG3 gives you the previous value three rows back, and so on.
one_quarter_back = lag1(qy)
Your first observation will be null, but the seccond will have the value from the previous observation. The third will have the value from the seccond and so on.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.