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

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

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User
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

3 REPLIES 3
MikeZdeb
Rhodochrosite | Level 12

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

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

ciro
Quartz | Level 8

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

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!

What is Bayesian Analysis?

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.

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
  • 3 replies
  • 665 views
  • 4 likes
  • 3 in conversation