DATA Step, Macro, Functions and more

enumeration variable by group

Accepted Solution Solved
Reply
Frequent Contributor
Frequent Contributor
Posts: 103
Accepted Solution

enumeration variable by group

Have:

 

idv1v2v3v4v5
100.1110.1
10 200.1
15 200.1
110 200.1
115 200.1
120 200.1
125 200.1
130 200.1
100.1110.2
10 200.2
15 200.2
110 200.2
115 200.2
120 200.2
125 200.2
130 200.2

 

want:

 

idv1v2v3v4v5count
100.1110.11
10 200.11
15 200.11
110 200.11
115 200.11
120 200.11
125 200.11
130 200.11
100.1110.22
10 200.22
15 200.22
110 200.22
115 200.22
120 200.22
125 200.22
130 200.22

 

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

 

Please help me to derive count variable.

 

Thanks


Accepted Solutions
Solution
‎08-17-2016 08:58 AM
Super User
Posts: 7,832

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

View solution in original post


All Replies
Super User
Posts: 19,847

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
Frequent Contributor
Posts: 103

Re: enumeration variable by group

Thanks Reeza
Super User
Super User
Posts: 7,977

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
Frequent Contributor
Posts: 103

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
Frequent Contributor
Posts: 103

Re: enumeration variable by group

Thanks RW9

Solution
‎08-17-2016 08:58 AM
Super User
Posts: 7,832

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
Frequent Contributor
Frequent Contributor
Posts: 103

Re: enumeration variable by group

Posted in reply to KurtBremser

Thanks Kurt. 

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 7 replies
  • 496 views
  • 0 likes
  • 4 in conversation