Reputation: 228
I want to change data of the form
id value
1 1
1 1
1 2
2 7
2 7
2 7
2 5
. .
. .
. .
to
id value
1 1
1 1
1 1
2 7
2 7
2 7
2 7
. .
. .
. .
That is, the last value by group should be the first value by group. I have tried the following code
data want;
set have;
by id;
last.value=first.value;
run;
But that didn't work. Could someone help me out?
Upvotes: 0
Views: 652
Reputation: 5417
The problem here is that first.value
and last.value
:
last.value =
is not valid syntaxSecondly, first.value
and last.value
only get set if the value
variable is stated in the by
statement. You should use first.id
and last.id
instead.
What we need to do here is:
id
value
variable until the last id
value is reachedid
value then set the value from step 1.Alexey's answer covers the actual syntax required to do this. Here's what the first.id
/last.id
values look like. (You can always view them by adding put _all_;
into your datastep):
id value first.id last.id tValue
1 1 1 0 1
1 1 0 0 1
1 2 0 1 1
2 7 1 0 7
2 7 0 0 7
2 7 0 0 7
2 5 0 1 7
. .
. .
. .
Upvotes: 3
Reputation: 1770
You should save first.id
value in variable and retain
it.
data want(drop=tValue);
set have;
by id;
retain tValue;
if first.id then tValue=value;
if last.id then value=tValue;
run;
Upvotes: 4