SAS - Using the retain statement
I have a dataset with ids, dates and amounts. Each id may have a few rows depending on the transaction amounts and the number of transactions performed. I need to use a retain statement t开发者_如何转开发o find the difference in time between the first change in amount. I know I need to retain all 3 variables but am unsure how to go about this? Any help would be great
Here's a basic use of a retain statement. I can't tell exactly what you need from your description. Maybe you need to retain
a second variable and then use if ... then ... else ...
(dates match, then ...., else....) Anyway, maybe this will get you started...
DATA in;
INPUT id var1 ;
DATALINES;
1 12
1 24
1 26
2 20
2 25
2 11
;
DATA out (drop=var1_lag);
set in;
by id;
if first.id then diff=.;
else diff = var1 - var1_lag;
var1_lag = var1;
retain var1_lag;
PROC PRINT data=out;
RUN;
Output:
Obs id var1 diff
1 1 12 .
2 1 24 12
3 1 26 2
4 2 20 .
5 2 25 5
6 2 11 -14
精彩评论