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.

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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.

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