## enumeration variable by group

Solved
Frequent Contributor
Posts: 106

# enumeration variable by group

Have:

 id v1 v2 v3 v4 v5 1 0 0.1 1 1 0.1 1 0 2 0 0.1 1 5 2 0 0.1 1 10 2 0 0.1 1 15 2 0 0.1 1 20 2 0 0.1 1 25 2 0 0.1 1 30 2 0 0.1 1 0 0.1 1 1 0.2 1 0 2 0 0.2 1 5 2 0 0.2 1 10 2 0 0.2 1 15 2 0 0.2 1 20 2 0 0.2 1 25 2 0 0.2 1 30 2 0 0.2

want:

 id v1 v2 v3 v4 v5 count 1 0 0.1 1 1 0.1 1 1 0 2 0 0.1 1 1 5 2 0 0.1 1 1 10 2 0 0.1 1 1 15 2 0 0.1 1 1 20 2 0 0.1 1 1 25 2 0 0.1 1 1 30 2 0 0.1 1 1 0 0.1 1 1 0.2 2 1 0 2 0 0.2 2 1 5 2 0 0.2 2 1 10 2 0 0.2 2 1 15 2 0 0.2 2 1 20 2 0 0.2 2 1 25 2 0 0.2 2 1 30 2 0 0.2 2

I am trying to get an enumeration variable (count) which indicates number of different v5 groups by ID.

Thanks

Accepted Solutions
Solution
‎08-17-2016 08:58 AM
Super User
Posts: 10,280

## Re: enumeration variable by group

``````proc sort data=have;
by id v5;
run;
/* just to be sure */

data want;
set have;
by id v5;
retain count;
if first.id then count = 0;
if first.v5 then count + 1;
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code

All Replies
Super User
Posts: 23,776

## Re: enumeration variable by group

[ Edited ]

You need clear rules on where to restart/increment your counts. Based on sample data this could work, but it depends on how closely it matxhes your real data.

Data want;

set have;

by id v1;

Retain count 0;

if v1=0 and v2=0.1 then count+1;

run;

Frequent Contributor
Posts: 106

Thanks Reeza
Super User
Posts: 9,599

## Re: enumeration variable by group

Hi,

```data want;
set have;
by id;
retain count;
if _n_=1 then count=0;
if first.id then count=count+1;
run;```
Frequent Contributor
Posts: 106

## Re: enumeration variable by group

HI RW9,

this code gives the enumeration for the id but doesnt give the enumeration for the variable 'V5'

Frequent Contributor
Posts: 106

## Re: enumeration variable by group

Thanks RW9

Solution
‎08-17-2016 08:58 AM
Super User
Posts: 10,280

## Re: enumeration variable by group

``````proc sort data=have;
by id v5;
run;
/* just to be sure */

data want;
set have;
by id v5;
retain count;
if first.id then count = 0;
if first.v5 then count + 1;
run;
``````
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Frequent Contributor
Posts: 106