Hello everybody,
I have this task that I can't find a easy solution and I know you will.
I need to calculate the max amount of continuos 1, like in the image below. (the var continuity is the desire one)
Thanks.
This should do it:
data want;
set test;
array nums{*} m1-m12;
count = 0;
continuity = 0;
i = 1;
do until(i > dim(nums));
if nums{i} = 1
then count + 1;
else do;
continuity = max(continuity,count);
count = 0;
end;
i + 1;
end;
continuity = max(continuity,count);
drop i count;
run;
Use an array; while scanning through the array, count a current counter for 1's, and when getting a 0 or reaching the end of the array, do a max() to set the final continuous variable (and reset the current counter).
If you want a code example, provide your data in a data step for copy/pasting.
Thanks for your answer and your time.
There is an example (I need to get use to bring one haha sorry)
data test;
input id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
datalines;
3424 0 1 0 0 0 0 1 1 1 0 1 1
5432 1 1 1 1 1 1 0 0 1 0 1 0
3930 0 1 0 1 1 1 1 0 0 1 1 1
6053 0 1 0 1 0 0 0 0 0 0 0 0
9493 1 1 1 1 1 0 0 0 0 0 1 1
4984 1 1 0 0 0 1 1 0 0 0 1 1
2039 0 1 1 1 1 0 0 0 0 1 0 0
;
Thanks a lot.
This should do it:
data want;
set test;
array nums{*} m1-m12;
count = 0;
continuity = 0;
i = 1;
do until(i > dim(nums));
if nums{i} = 1
then count + 1;
else do;
continuity = max(continuity,count);
count = 0;
end;
i + 1;
end;
continuity = max(continuity,count);
drop i count;
run;
Thanks! This works!
another logic a bit lengthy
data have;
input id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
datalines;
3424 0 1 0 0 0 0 1 1 1 0 1 1
5432 1 1 1 1 1 1 0 0 1 0 1 0
3930 0 1 0 1 1 1 1 0 0 1 1 1
6053 0 1 0 1 0 0 0 0 0 0 0 0
9493 1 1 1 1 1 0 0 0 0 0 1 1
4984 1 1 0 0 0 1 1 0 0 0 1 1
2039 0 1 1 1 1 0 0 0 0 1 0 0
;
data want;
set have;
array c(*) m1-m12;
do i = 1 to dim(c);
m=c(i);
output;
end;
run;
data want2;
set want;
by id notsorted;
retain mc;
if first.id then mc=.;
if m eq 0 then mc=m;
else mc+m;
drop i m ;
run;
proc sql;
create table want3 as select distinct * from want2 group by id having mc=max(mc);
quit;
data test;
input id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
datalines;
3424 0 1 0 0 0 0 1 1 1 0 1 1
5432 1 1 1 1 1 1 0 0 1 0 1 0
3930 0 1 0 1 1 1 1 0 0 1 1 1
6053 0 1 0 1 0 0 0 0 0 0 0 0
9493 1 1 1 1 1 0 0 0 0 0 1 1
4984 1 1 0 0 0 1 1 0 0 0 1 1
2039 0 1 1 1 1 0 0 0 0 1 0 0
2039 0 0 0 0 0 0 0 0 0 0 0 0
;
data temp;
set test;
array x{*} m:;
continuity=0;n=0;
do i=1 to dim(x);
if x{i}=1 then do;
n=1;
do j=i+1 to dim(x);
if x{j}=1 then n+1;
else leave;
end;
end;
continuity=max(continuity,n);
end;
drop n i j;
run;
proc print noobs;run;
Can't skip this party
How about this non-orthodox solution:
options missing='';
data test;
input id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
datalines;
3424 0 1 0 0 0 0 1 1 1 0 1 1
5432 1 1 1 1 1 1 0 0 1 0 1 0
3930 0 1 0 1 1 1 1 0 0 1 1 1
6053 0 1 0 1 0 0 0 0 0 0 0 0
9493 1 1 1 1 1 0 0 0 0 0 1 1
4984 1 1 0 0 0 1 1 0 0 0 1 1
2039 0 1 1 1 1 0 0 0 0 1 0 0
2039 0 0 0 0 0 0 0 0 0 0 0 0
;
data want;
set test;
length _3 $12 _4 $12 _1 $12;
_1=cats(of m:);
do _i=1 by 1 until (missing(_3));
_3=scan(_1,_i,'0');
_4=max(_4,_3);
end;
continuity=lengthn(strip(_4));
drop _:;
run;
HaiKuo's code remind me to use CALL SCAN.
data test;
input id m1 m2 m3 m4 m5 m6 m7 m8 m9 m10 m11 m12;
datalines;
3424 0 1 0 0 0 0 1 1 1 0 1 1
5432 1 1 1 1 1 1 0 0 1 0 1 0
3930 0 1 0 1 1 1 1 0 0 1 1 1
6053 0 1 0 1 0 0 0 0 0 0 0 0
9493 1 1 1 1 1 0 0 0 0 0 1 1
4984 1 1 0 0 0 1 1 0 0 0 1 1
2039 0 1 1 1 1 0 0 0 0 1 0 0
2039 0 0 0 0 0 0 0 0 0 0 0 0
;
data want;
set test;
temp=cats(of m:);
continutity=0;
do i=1 to countw(strip(temp),'0');
call scan(strip(temp),i,p,l,'0');
continutity=max(continutity,l);
end;
drop i p l temp;
run;
proc print ;run;
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 16. Read more here about why you should contribute and what is in it for you!
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.
Select SAS Training centers are offering in-person courses. View upcoming courses for: