BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LB
Quartz | Level 8 LB
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
PGStats
Opal | Level 21

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

View solution in original post

5 REPLIES 5
PGStats
Opal | Level 21

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
LB
Quartz | Level 8 LB
Quartz | Level 8

Many Thanks PG!

Lawrence

Haikuo
Onyx | Level 15

If using DOW:

DATA plots2;

do until (last.fac_id);

   SET plots;

   BY FAC_ID;

   CT=_n_;

    output;

end;

RUN;


Haikuo

Ksharp
Super User

Or lag function.

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

Ksharp

LB
Quartz | Level 8 LB
Quartz | Level 8

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

Lawrence

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 5 replies
  • 1652 views
  • 0 likes
  • 4 in conversation