Help using Base SAS procedures

Trying to obtain a count of sequential numbers with the same model

Reply
Occasional Contributor
Posts: 10

Trying to obtain a count of sequential numbers with the same model

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;   

Super User
Posts: 19,770

Re: Trying to obtain a count of sequential numbers with the same model

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

Respected Advisor
Posts: 3,156

Re: Trying to obtain a count of sequential numbers with the same model

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;

Occasional Contributor
Posts: 10

Re: Trying to obtain a count of sequential numbers with the same model

Thanks very much for your help.

Ask a Question
Discussion stats
  • 3 replies
  • 185 views
  • 3 likes
  • 3 in conversation