Date: Tue, 13 Apr 2004 18:46:15 GMT
Reply-To: LWn <lars.wahlgren.pleasenospam@STAT.LU.SE>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: LWn <lars.wahlgren.pleasenospam@STAT.LU.SE>
Organization: Telia Internet
Subject: Re: How To Add A Count Variable in A Data Step
or even shorter if you after Proc Sort use this data step
data c ;
set b ; /* sorted data set */
by a1 ;
if first.a1 then count=1 ;
else count+1 ;
run ;
/LWn
"Evert Carton" <me@you.he> skrev i meddelandet
news:pan.2003.12.10.02.26.27.665448@you.he...
> On Mon, 24 Nov 2003 04:34:52 -0800, momo zeus wrote:
>
> > Hi, Everyone
> > I meet with such a problem when dealing with my data as follows:
> > Suppose I have a DATA SET a that
> > Data a;
> > input a1 $3. a2 8.;
> > cards;
> > 11 34
> > 11 14
> > 11 24
> > 22 13
> > 22 24
> > ;
> > Proc sort data = a out= b;
> > by a1 descending a2;
> > run;
> > Then I can get such a DATA SET b as follows:
> > Data b;
> > input a1 $3. a2 8.;
> > cards;
> > 11 34
> > 11 24
> > 11 14
> > 22 24
> > 22 13
> > ;
> > The last thing I want to get is the DATA SET as follows:
> > Data b;
> > input a1 $3. a2 8. count 8.;
> > cards;
> > 11 34 1
> > 11 24 2
> > 11 14 3
> > 22 24 1
> > 22 13 2
> > ;
> > The Above just a sample from my real data ,there are many A1s and
I want
> > to know how to add variable COUNT in data set c.
> > Many thanks.
> > MOMO
>
> Hi !
>
> This should the trick (except that I do everything numeric)
> That's the one thing you will have to change.
> Let's start from situation dataset b:
>
> data b;
> input a1 a2;
> cards;
> 11 34
> 11 24
> 11 14
> 22 24
> 22 13
> ;
> run;
>
> And this what you're looking for:
>
> data c(drop=help);
> length a1 a2 count help 8;
> retain help;
> set b;
> if help=a1 then count+1;
> else do;
> help=a1;
> count=1;
> end;
> run;
>
> Verify with:
>
> proc print data=c; run;
>
> Regards,
> Evert
> PS: Please no email !!
|