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;
Why is the first row 101 - 102 but the 4/5 is 104-104 not 104-110?
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;
Thanks very much for your help.
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.
Ready to level-up your skills? Choose your own adventure.