BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.
chsprogramming
Calcite | Level 5

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
Patrick
Opal | Level 21

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;

Patrick_0-1694054564995.png

 

View solution in original post

4 REPLIES 4
Tom
Super User Tom
Super User

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;

 

Patrick
Opal | Level 21

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;

Patrick_0-1694054564995.png

 

chsprogramming
Calcite | Level 5

Amazing, thank you so much!

Reeza
Super User
  • 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

 


 

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

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