Hello
I have a data set with char field that contain 6 figures 1/0.
I want to identify the following pattern (If this pattern then Indicator=1 else 0).
Block A- block of '1's in the right side
In the right side there will be continuous of one or several '1's and last number must be 1
Block B- block of '0's in the left side of block A
There can be one zero or multiple zeros in consecutive
Block C- block of '1's in the left side of block A
There can be one '1' or multiple '1's in consecutive
examples:
010101 -This pattern meet the criteria because Block A-1 Block B-0 Block C-1
000001--This pattern doesn't meet the criteria
000011--This pattern doesn't meet the criteria
101101-This pattern meet the criteria because Block A-1 Block B-0 Block C-11
101001-This pattern meet the criteria because Block A-1 Block B-00 Block C-1
010011-This pattern meet the criteria because Block A-11 Block B-00 Block C-1
What is the way to recognize this pattern?
Data have;
input X $;
cards;
010101
000001
000011
000111
001111
011111
111111
110011
110111
111001
100001
001001
001011
010011
101001
101101
;
Run;
Data have;
input X $;
cards;
010101
000001
000011
000111
001111
011111
111111
110011
110111
111001
100001
001001
001011
010011
101001
101101
;
Run;
data want;
set have;
y=prxchange('s/0+/0/',-1,x);
z=prxchange('s/1+/1/',-1,y);
Indicator=ifn(find(z,'101'),1,0);
drop y z;
run;
Data have;
input X $;
cards;
010101
000001
000011
000111
001111
011111
111111
110011
110111
111001
100001
001001
001011
010011
101001
101101
;
Run;
data want;
set have;
y=prxchange('s/0+/0/',-1,x);
z=prxchange('s/1+/1/',-1,y);
Indicator=ifn(find(z,'101'),1,0);
drop y z;
run;
Thanks
May you please explain
y=prxchange('s/0+/0/',-1,x);
Hi,
Thanks for supplying the input data in a data step. Please supply the expected output data in a data step too.
Please also clarify the following if the examples "000001" & "000011" don't meet the criteria because they do not satisfy Block C or is it something else?
Thanks & kind regards,
Amir.
/*
Sorry.
I found a problem in my code.
if you want 010100 be NOT matched, try this one:
*/
Data have;
input X $;
cards;
010101
010100
;
Run;
data want;
set have;
y=prxchange('s/0+/0/',-1,x);
z=prxchange('s/1+/1/',-1,y);
Indicator=ifn(prxmatch('/101$/',strip(z)),1,0);
drop y z;
run;
You already have an answer but, just for fun, one more:
Data have;
input X $;
cards;
010101
000001
000011
000111
001111
011111
111111
110011
110111
111001
100001
001001
001011
010011
101001
101101
101100
;
Run;
data want;
set have;
marker =
mod(X,2) /* not ends with 0 */
*
(countw(X,"0","B") > 1) /* has at least 2 sequences of 1s (counting from right) */
;
run;
proc print;
run;
{EDIT:}
Of course if zeros on right aren't a problem (e.g., 010110 should be marked) then it reduces just to:
marker = (countw(X,"0","B") > 1);
{EDIT 2:}
To avoid ugly "types conversion" note in the log, the MOD() can be replaced with CHAR() function:
marker =
char(X,6) = "1" /* not ends with 0 */
*
(countw(X,"0","B") > 1) /* has at least 2 sequences of 1s (counting from right) */
;
Bart
ok, so it looks like my code does what you need.
Both codes provide same result which is perfect:)
Data have;
input X $ 20.;;
cards;
010101
000001
000011
000111
001111
011111
111111
110011
110111
111001
100001
001001
001011
010011
101001
101101
000000000001
000000000101
000000000011
000001011111
100000000001
101010101011
;
Run;
data want1;
set have;
y=prxchange('s/0+/0/',-1,x);
z=prxchange('s/1+/1/',-1,y);
Indicator1=ifn(find(z,'101'),1,0);
drop y z;
run;
data want2;
set have;
Indicator2 = mod(X,2) /* not ends with 0 */ * (countw(X,"0","B") > 1) /* has at least 2 sequences of 1s (counting from right) */;
run;
proc sql;
create table compare_results(Where=(Indicator1 ne Indicator2)) as
select a.*,b.Indicator2
from want1 as a
left join want2 as b
on a.X=b.X
;
quit;
You didn't read what I wrote in my answer about "if zeros at the end are not important"...
Actually cannot have zero at end because the check is for customers that exist today (value 1)
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.