## Enumeration by group

Solved
Regular Contributor
Posts: 159

# Enumeration by group

Hi all;

I am trying to enumerate a group with a counter-

So for example if I have data that looks like this-

FAC        YEAR

A               2008

A              2009

A              2010

B              2008

B             2009

B              2010

What I want is

FAC        YEAR    CT

A               2008    1

A              2009     1

A              2010     1

B              2008    2

B             2009     2

B             2010   2

The code I have currently is:

DATA plots2;

CT+1;

SET plots;

BY FAC_ID;

IF FIRST.FAC_ID THEN CT=1;

RUN;

Which gives me:

FAC        YEAR    CT

A               2008    1

A              2009     2

A              2010     3

B              2008    1

B             2009     2

B             2010   3

Which is not what I want. Any help appreciated.  I have attempted to use lag & retain functions, much to the same effect.

Thanks

Lawrence

Accepted Solutions
Solution
‎04-23-2013 03:47 PM
Posts: 5,528

## Re: Enumeration by group

You were almost there...

DATA plots2;

SET plots;

BY FAC_ID;

IF FIRST.FAC_ID THEN CT+1;

RUN;

or simpler still

DATA plots2;

SET plots;

BY FAC_ID;

CT + FIRST.FAC_ID;

RUN;

PG

PG

All Replies
Solution
‎04-23-2013 03:47 PM
Posts: 5,528

## Re: Enumeration by group

You were almost there...

DATA plots2;

SET plots;

BY FAC_ID;

IF FIRST.FAC_ID THEN CT+1;

RUN;

or simpler still

DATA plots2;

SET plots;

BY FAC_ID;

CT + FIRST.FAC_ID;

RUN;

PG

PG
Regular Contributor
Posts: 159

Many Thanks PG!

Lawrence

Posts: 3,167

## Re: Enumeration by group

If using DOW:

DATA plots2;

do until (last.fac_id);

SET plots;

BY FAC_ID;

CT=_n_;

output;

end;

RUN;

Haikuo

Super User
Posts: 10,778

## Re: Enumeration by group

Or lag function.

IF FAC_ID ne lag(FAC_ID ) THEN CT+1;

Ksharp

Regular Contributor
Posts: 159

## Re: Enumeration by group

Thanks everyone.  I knew I was close but this helped !

Lawrence

🔒 This topic is solved and locked.