SAS Programming

DATA Step, Macro, Functions and more
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-white.png

Join us for our biggest event of the year!

Four days of inspiring keynotes, product reveals, hands-on learning opportunities, deep-dive demos, and peer-led breakouts. Don't miss out, May 6-9, in Orlando, Florida.

 

View the full agenda.

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