BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Feragon42
Fluorite | Level 6

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.

 

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

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;

View solution in original post

8 REPLIES 8
Kurt_Bremser
Super User

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.

Feragon42
Fluorite | Level 6

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.

Kurt_Bremser
Super User

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;
Feragon42
Fluorite | Level 6

Thanks! This works! 

Jagadishkatam
Amethyst | Level 16

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
Ksharp
Super User
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;
Haikuo
Onyx | Level 15

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;
Ksharp
Super User

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;

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

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
  • 8 replies
  • 1262 views
  • 7 likes
  • 5 in conversation