Help using Base SAS procedures

classifying a sequence of 0 and 1

Accepted Solution Solved
Reply
Contributor
Posts: 43
Accepted Solution

classifying a sequence of 0 and 1

Hi,

I have a variable, pattern, that is a sequence of 12 zeroes and/or ones. These indicate the presence or absence of a condition in twelve months.

what I would like to do is to flag different kinds of pattern according to whether there is an uninterrupted subsequence of ones filled with only zeroes.

for instance I want to flag with 'b' any sequence of 1 followed by only zeroes like:

100000000000

110000000000

111000000000

and so on.

with 'f' any sequence of final zeroes like

000000000001

000000000011

000000000111

....

with 'm' a sequence of ones in the middle like:

000001000000

000001100000

000011100000

.....

and with 'o' all other sequences, all zeroes or any mixed sequence of 0 and 1 like:

110001000000

111100000111

101011100100

....

Any suggestion for a as simple solution as possible?

thank you very much in advance


Accepted Solutions
Solution
‎11-01-2011 11:46 PM
Super User
Posts: 9,681

Re: classifying a sequence of 0 and 1

data x;
input months $12.;
datalines;
111111111111
000000000000
100000000000
110000000000
111000000000
000000000001
000000000011
000000000111
000001000000
000001100000
000011100000
110001000000
111100000111
101011100100
;
run;
data want(drop=mon con);
 set x;
 mon=translate(compbl(translate(months,' ','1')),'_',' ');
 con=countc(mon,'_'); 
 if con=1 then do;
                  select(findc(mon,'_'));
                   when(1) flag='B';
                   when(length(mon)) flag='F';
                   otherwise flag='M';
                  end;
                end;
    else flag='O';
run;

Ksharp

View solution in original post


All Replies
Valued Guide
Posts: 765

Re: classifying a sequence of 0 and 1

Hi ... an edited verison of previous posting.  Ksharp's method was so neat and succinct, I gave my previous posting a bit more thought.

I'm willing to live with a WARNING when all months are 0  ...

NOTE: Argument 2 to function REPEAT at line 1268 column 20 is invalid.

months=000000000000 group=o _ERROR_=1 _N_=0

data x;

input months $12.;

datalines;

111111111111

000000000000

100000000000

110000000000

111000000000

000000000001

000000000011

000000000111

000001000000

000001100000

000011100000

110001000000

111100000111

101011100100

;

run;

data x;

set x;

_n_ = find(months, repeat('1', countc(months,'1')-1));

select;

  when (_n_ and char(months, 1) eq '1') group = 'b';

  when (_n_ and char(months,12) eq '1') group = 'f';

  when (_n_) group = 'm';

  otherwise group = 'o';

end;

run;


   months       group

111111111111      b

000000000000      o

100000000000      b

110000000000      b

111000000000      b

000000000001      f

000000000011      f

000000000111      f

000001000000      m

000001100000      m

000011100000      m

110001000000      o

111100000111      o

101011100100      o

Solution
‎11-01-2011 11:46 PM
Super User
Posts: 9,681

Re: classifying a sequence of 0 and 1

data x;
input months $12.;
datalines;
111111111111
000000000000
100000000000
110000000000
111000000000
000000000001
000000000011
000000000111
000001000000
000001100000
000011100000
110001000000
111100000111
101011100100
;
run;
data want(drop=mon con);
 set x;
 mon=translate(compbl(translate(months,' ','1')),'_',' ');
 con=countc(mon,'_'); 
 if con=1 then do;
                  select(findc(mon,'_'));
                   when(1) flag='B';
                   when(length(mon)) flag='F';
                   otherwise flag='M';
                  end;
                end;
    else flag='O';
run;

Ksharp

Contributor
Posts: 43

Re: classifying a sequence of 0 and 1

thank you guys! very nice solutions! I learned a lot!

☑ This topic is SOLVED.

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

Discussion stats
  • 3 replies
  • 114 views
  • 4 likes
  • 3 in conversation