BookmarkSubscribeRSS Feed
scttttt
Calcite | Level 5

Trying to obtain a count of sequential numbers with the same model type.  Want is the desired report output listing and Have is the available dataset.

Want:

Model       First                   Last               Count

xx        100000001        100000002         2

yy        100000003        100000003         1

xx        100000004        100000004         1

xx        100000010        100000010         1

yy        100000011        100000011         1

xx        100000012        100000012         1

xx        100000101        100000102         2

yy        100000103        100000104         2

xx        100000105        100000108         4

data have;                                                                                                                             

input @1 number @11 model $2.;                                                                                                         

cards;                                                                                                                                 

100000001 XX                                                                                                                           

100000002 XX                                                                                                                           

100000003 YY                                                                                                                           

100000004 XX                                                                                                                           

100000010 XX                                                                                                                           

100000011 YY                                                                                                                           

100000012 XX                                                                                                                           

100000101 XX                                                                                                                           

100000102 XX                                                                                                                           

100000103 YY                                                                                                                           

100000104 YY                                                                                                                           

100000105 XX                                                                                                                           

100000106 XX

100000107 XX

100000108 XX                                                                                                                           

;                                                                                                                                      

run;   

3 REPLIES 3
Reeza
Super User

Why is the first row 101 - 102 but the 4/5 is 104-104 not 104-110?

Haikuo
Onyx | Level 15

A quick and dirty solution:

data have;

     input @1 number @11 model $2.;

     cards;

100000001 XX

100000002 XX

100000003 YY

100000004 XX

100000010 XX

100000011 YY

100000012 XX

100000101 XX

100000102 XX

100000103 YY

100000104 YY

100000105 XX

100000106 XX

100000107 XX

100000108 XX

;

run;

data have1;

     set have;

     if model ne lag(model) or dif(number)>1 then

           grp+1;

run;

data want;

     do count=1 by 1 until (last.grp);

          set have1;

           by model grp notsorted;

           if first.grp then

                first=number;

           if last.grp then

                last=number;

     end;

run;

scttttt
Calcite | Level 5

Thanks very much for your help.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

What is Bayesian Analysis?

Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.

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
  • 3 replies
  • 676 views
  • 3 likes
  • 3 in conversation