Calcite | Level 5

## Enumeration of variable by repeating group

I have a vehicle count variable that I need to assign a vehicle number from. I think I need an enumeration of some type...the last column is the column I need to create.

VC VN
1 1
1 1
1 1
1 1
1 1
2 1
2 2
2 1
2 2
2 1
3 1
3 2
3 3
3 1

1 ACCEPTED SOLUTION

Accepted Solutions
Opal | Level 21

## Re: Enumeration of variable by repeating group

Based on your sample data something like below could work.

data have;
input vc vn;
datalines;
1 1
1 1
1 1
1 1
1 1
2 1
2 2
2 1
2 2
2 1
3 1
3 2
3 3
3 1
;

data want(drop=_:);
set have;
by vc;
if first.vc then
do;
_s2+1;
_s1=_s2;
end;
else _s1+1;
vn_calc=1+mod(_s1,_s2);
run;

proc print data=want;
run;

4 REPLIES 4
Super User

## Re: Enumeration of variable by repeating group

Is this a Rorschach test ?

I cannot see a pattern there.

So you you will need to describe the rule if you us to help you with code to implement it.

I have never considered Enumeration of a variable.  What does that mean?

I know how to number OBSERVATIONS.

data want;
set have;
obs_number + 1;
run;

I know how to number GROUPS.

data want;
set have;
by group_name;
group_number + first.group_name;
run;

I know how to number the members of a group.

data want;
set have;
by group_name;
member_number + 1;
if first.group_name then member_number=1;
run;

Opal | Level 21

## Re: Enumeration of variable by repeating group

Based on your sample data something like below could work.

data have;
input vc vn;
datalines;
1 1
1 1
1 1
1 1
1 1
2 1
2 2
2 1
2 2
2 1
3 1
3 2
3 3
3 1
;

data want(drop=_:);
set have;
by vc;
if first.vc then
do;
_s2+1;
_s1=_s2;
end;
else _s1+1;
vn_calc=1+mod(_s1,_s2);
run;

proc print data=want;
run;

Calcite | Level 5

## Re: Enumeration of variable by repeating group

Amazing, thank you so much!

Super User

## Re: Enumeration of variable by repeating group

• Set counter to 1 and first record of each VC
• Increment if less than VC, otherwise set back to 1
data want;
set have;
by vc;

retain counter;

if first.vc then counter=0;

counter = ifn(counter<VC, counter+1, 1);

/*if counter<VC then counter+1; else counter=1;*/

run;

@chsprogramming wrote:

I have a vehicle count variable that I need to assign a vehicle number from. I think I need an enumeration of some type...the last column is the column I need to create.

VC VN
1 1
1 1
1 1
1 1
1 1
2 1
2 2
2 1
2 2
2 1
3 1
3 2
3 3
3 1

Discussion stats
• 4 replies
• 328 views
• 2 likes
• 4 in conversation