Date: Wed, 22 May 1996 13:51:38 GMT
Reply-To: Zhongwen Lai <zwlai@LEXIS.POP.UPENN.EDU>
Sender: "SAS(r) Discussion" <SAS-L@UGA.CC.UGA.EDU>
From: Zhongwen Lai <zwlai@LEXIS.POP.UPENN.EDU>
Organization: University of Pennsylvania
Subject: Re: help of sum. in SAS programming
Some one posted a programming question (as follows) but I lost the after
I first quit the newgroup thus his/her name and email address. So I have
to post my answer to the group and hope he/she can read it.
QUESTION
Suppose there is a variable X in SAS data set Q, with its values denoted
as x1, x2, ..., xn from the first obs to the last obs n, how to
construct a new variable Y which is defined as follows:
X Y
x1 y1=x1
x2 y2=x1+x2 (=y1+x2)
x3 y3=x1+x2+x3 (=y2+x3)
. . .
xn yn=x1+x2+,,,+xn (=y(n-1)+xn)
ANSWER
Y can also be expressed in the form of the third column, so the answer
is simple:
DATA A;
SET Q;
RETAIN Y;
Y=Y+X;
RUN;
This logic, in combination with FIRST.var and LAST.var, where var is the
variable by which the data is sorted (e.g, var is a ID variable for each
individual and each individual may have multiple obs in the data set),
you can also obtain the SUM of a given variable for each individual, or
simply count the number of occurences.
Zhongwen Lai at Upenn
|