SAS Helpful Hints
Carrying forward data(Retain)
To carry data from one observation to the next, values must be retained.
* Sort the data appropriatly;
Proc Sort Data=work.A; By id visit; Run;
Data work.B;
Retain first_x;
Set work.A;
By id visit;
* To ensure that we do not carry data to where it
* does not belong, set first_x to missing;
* Without this, a previous ID's X could be used
* in the next ID, if visit was never equal to 1.;
If first.id Then first_x=.;
* Save the first x when visit=1;
If visit=1 Then first_x=x;
change_x=x-first_x;
Run;
Results
| id |
visit |
x |
change_x |
first_x |
| 1 |
1 |
35 |
0 |
35 |
| 1 |
2 |
40 |
5 |
35 |
| 1 |
3 |
50 |
15 |
35 |
| 1 |
4 |
80 |
45 |
35 |
| 2 |
0 |
4 |
. |
. |
| 2 |
1 |
8 |
0 |
8 |
| 2 |
2 |
12 |
4 |
8 |
| 2 |
3 |
16 |
8 |
8 |
| 2 |
4 |
20 |
12 |
8 |
Please send your comments and suggestions about this web page to A. Watts
(watts@bst.rochester.edu)
|