Date: Mon, 31 Mar 2003 13:01:42 -0500
Reply-To: diskin.dennis@KENDLE.COM
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: diskin.dennis@KENDLE.COM
Subject: Re: Counting occurances of data
Content-type: text/plain; charset=us-ascii
Brett,
The simple way is to sort the dataset on VarA and then whatever other
variables the dataset is currently ordered on:
proc sort data=in;
by VarA otherIDs;
run;
data in;
set in;
by VarA;
if first.VarA then n_VarA = 0;
n_VarA+1;
run;
then possibly put back in the original order:
Proc sort data=in;
by otherIDs n_VarA;
run;
Other approaches are possible using an array to keep track of the counts
for each distinct value of VarA, but I doubt they would be practical since
you would need an array of at least Number of records divided by 4.
HTH,
Dennis Diskin
From: Brett Plummer <plummer@HSMGROUP.COM>@LISTSERV.UGA.EDU> on 03/31/2003
12:32 PM
Please respond to Brett Plummer <plummer@HSMGROUP.COM>
Sent by: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
To: SAS-L@LISTSERV.UGA.EDU
cc:
Subject: Counting occurances of data
I have a large dataset where a specific instance of Variable A occurs up to
4 times. The dataset is sorted by date. I want to assign a new variable
that describes whether a specific instance of Variable A is the first,
second, third, or fourth time is has occured in the data.
Example of what I have:
RECORD VarA
1 1
2 2
3 3
4 1
5 1
6 2
7 2
8 3 etc.
Example of what I want to create:
RECORD VarA NewVar
1 1 1
2 2 1
3 3 1
4 1 2 (in record 4, VarA=1 for the second time)
5 1 3 (in record 5, VarA=1 for the third time)
6 2 2 (in record 6, VarA=2 for the second time)
7 2 3 (in record 7, VarA=2 for the third time)
8 3 2 etc.
Thank you,
Brett Plummer