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-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 328 views
  • 2 likes
  • 4 in conversation