| Date: | Thu, 11 May 2000 08:26:19 -0400 |
| Reply-To: | Howard Schreier <Howard_Schreier@ITA.DOC.GOV> |
| Sender: | "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU> |
| From: | Howard Schreier <Howard_Schreier@ITA.DOC.GOV> |
| Subject: | Re: subtracting down columns |
| Content-Type: | text/plain; charset=US-ASCII |
SAS has the DIF function for this purpose.
data given;
input id visit score;
cards;
1 1 15
1 2 20
1 3 30
1 4 40
2 1 10
2 2 15
2 3 28
2 4 32
;
data diffs;
set given; by id;
diff = dif(score);
if first.id then diff = 0;
run;
proc print; run;
Note that DIF functions are mechanically similar to LAG functions, so
one has to be very careful about referencing them conditionally.
>Date: Thu, 11 May 2000 00:57:02 -0400
>From: Greg Dubrow <dubrow@UNDERGROUND.IRHE.UPENN.EDU>
>Subject: subtracting down columns
>
>Hi -
>
>Given the following data layout
>
>id visit score
>1 1 15
>1 2 20
>1 3 30
>1 4 40
>2 1 10
>2 2 15
>2 3 28
>2 4 32
>
>
>I need to create a running total subtracting the core from visit 2
>from visit 1, then 3 from 2, etc by id. Teh result should look like
>
>id visit score difference
>1 1 15 0
>1 2 20 5
>1 3 30 10
>1 4 40 10
>2 1 10 0
>2 2 15 5
>2 3 28 13
>2 4 32 4
>
>I've done similar operations (using retain, if first., etc) to run a
>summed total down, but not a discrete subtraction where I'm
>subtracting the current score from the previous score.
>
>Help would be greatly appreciated.
>
>regards,
>
>Greg
|