DATA Step, Macro, Functions and more

Find max continuity 1 and 0

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 10
Accepted Solution

Find max continuity 1 and 0

[ Edited ]

Hello everybody,

I have this task that I can't find a easy solution and I know you will.

 

continuity_example.PNG

 

I need to calculate the max amount of continuos 1, like in the image below. (the var continuity is the desire one)

Thanks.

 


Accepted Solutions
Solution
‎06-13-2017 10:03 AM
Super User
Posts: 6,946

Re: Find max continuity 1 and 0

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers

View solution in original post


All Replies
Super User
Posts: 6,946

Re: Find max continuity 1 and 0

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.

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 10

Re: Find max continuity 1 and 0

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.

Solution
‎06-13-2017 10:03 AM
Super User
Posts: 6,946

Re: Find max continuity 1 and 0

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;
---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
Occasional Contributor
Posts: 10

Re: Find max continuity 1 and 0

Thanks! This works! 

Trusted Advisor
Posts: 1,131

Re: Find max continuity 1 and 0

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;
Thanks,
Jag
Super User
Posts: 9,682

Re: Find max continuity 1 and 0

[ Edited ]
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;
Respected Advisor
Posts: 3,124

Re: Find max continuity 1 and 0

Can't skip this party Smiley LOL

Spoiler
 

 

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;
Super User
Posts: 9,682

Re: Find max continuity 1 and 0

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;
☑ This topic is SOLVED.

Need further help from the community? Please ask a new question.

Discussion stats
  • 8 replies
  • 166 views
  • 7 likes
  • 5 in conversation