BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
ari
Quartz | Level 8 ari
Quartz | Level 8

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

1 ACCEPTED SOLUTION

Accepted Solutions
7 REPLIES 7
Reeza
Super User

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;

 

ari
Quartz | Level 8 ari
Quartz | Level 8
Thanks Reeza
RW9
Diamond | Level 26 RW9
Diamond | Level 26

Hi,

 

data want;
  set have;
  by id;
  retain count;
  if _n_=1 then count=0;
  if first.id then count=count+1;
run;
ari
Quartz | Level 8 ari
Quartz | Level 8

HI RW9,

 

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

ari
Quartz | Level 8 ari
Quartz | Level 8

Thanks RW9

Kurt_Bremser
Super User
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;
ari
Quartz | Level 8 ari
Quartz | Level 8

Thanks Kurt. 

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

Register Now

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

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