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

Hello there,

 

I would appreciate if you could help me to calculate the number of consecutives occurences.

Here is an example

 

Have

IDGroup
10
21
30
41
51
60
70

 

Want

IDGroupCount
100
211
300
412
512
600
700

 

Thanks in advance!

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

Here's an approach:

 

data want;
   counter = 0;
   do until (last.group);
      set have;
      by group notsorted;
      counter + 1;
   end;
   do until (last.group);
      set have;
      by group notsorted;
      if group = 0 then count = 0;
      else count = counter;
      output;
   end;
   drop counter;
run;

It's untested code, so you will have to try it to see if it needs any adjustments.

View solution in original post

2 REPLIES 2
Astounding
PROC Star

Here's an approach:

 

data want;
   counter = 0;
   do until (last.group);
      set have;
      by group notsorted;
      counter + 1;
   end;
   do until (last.group);
      set have;
      by group notsorted;
      if group = 0 then count = 0;
      else count = counter;
      output;
   end;
   drop counter;
run;

It's untested code, so you will have to try it to see if it needs any adjustments.

Amine_Khemiri
Obsidian | Level 7

Thanks a lot! I tested it and it works fine.

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
  • 2 replies
  • 871 views
  • 0 likes
  • 2 in conversation