<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to check for an array of columns and search for specific pattern of events in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887227#M350542</link>
    <description>&lt;P&gt;To extend your current idea is easy.&amp;nbsp; You can take advantage of the power of the data step DO loop to make it even simpler.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg=0;
  do i=1 to dim(colname) until (colname[i] ='1');
  end;
  do i=i+1 to dim(colname) until (flg);
    flg=colname[i] ='0';
  end;
  if flg;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can eliminate the first loop by using the WHICHC() function to find the first '1'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg=0;
  i=whichc('1', of colname[*]);;
  if i then do i=i+1 to dim(colname) until (flg);
    flg=colname[i] ='0';
  end;
  if flg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you don't want to include cases where there is a '0' before the first '1' then you could eliminate the do loops completely by just testing if the first '0' is after the first '1'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg= 0 &amp;lt; whichc('1',of colname[*]) &amp;lt; whichc('0',of colname[*]);
  if flg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 01 Aug 2023 15:57:39 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2023-08-01T15:57:39Z</dc:date>
    <item>
      <title>How to check for an array of columns and search for specific pattern of events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887220#M350538</link>
      <description>&lt;P&gt;Hello folks, I think I've figured partially out about this issue at hand and it should be straightforward.&lt;/P&gt;
&lt;P&gt;Basically, I want to search for values in an array of 21 columns and make sure I capture rows when event A occurs BEFORE event B. In my case, status 1 occurs before status 0, but not necessarily in adjacent columns. Below is what my data looks like:&lt;/P&gt;
&lt;P&gt;each row is a unique ID, and I have column names from st1 till st21:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kevsma_0-1690850417892.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86318iDAA7FEC1D7262120/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kevsma_0-1690850417892.png" alt="kevsma_0-1690850417892.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I want to identify/signal the rows when value 1 occurs BEFORE value 0 per row and there could be other values in between the two values I want, but as long as 1 is before 0, I want to flag the row. Below is my code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
DATA TEST; SET KEEP4;
	ARRAY COLNAME{21} ST1-ST21;
	FLG=0;
	DO i=1 TO DIM(COLNAME);
		IF COLNAME{i} ='1' THEN DO;
			IF COLNAME{i+1}='0' 
				THEN DO;
			   FLG=1;
			END;
		END;
	END;
	DROP i;
RUN;

DATA TEST1; SET TEST;
	WHERE FLG=1; RUN;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But the code above only selects the rows when 1 occurs RIGHT before 0 like below:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="kevsma_1-1690850609525.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/86319i8DD485E175A2AD71/image-size/medium?v=v2&amp;amp;px=400" role="button" title="kevsma_1-1690850609525.png" alt="kevsma_1-1690850609525.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;There will be situations when 1 occurs, followed by a bunch of other values before 0 appears, how can I capture those?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Many thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 00:44:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887220#M350538</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-08-01T00:44:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to check for an array of columns and search for specific pattern of events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887224#M350540</link>
      <description>&lt;P&gt;It's a lot easier without arrays. Just create a string from the columns and search for a '1' followed by a '0':&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  input st1 $ st2 $ st3 $ st4 $;
  String = cats(of st1 - st4);
  Flag = (findc(string,'1') and findc(string,'0', ,findc(string,'1'))); 
datalines;
0 1 5 0
0 1 0 5
0 1 0 5
5 1 0 6
1 0 5 D
1 0 1 5
1 2 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 02:04:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887224#M350540</guid>
      <dc:creator>SASKiwi</dc:creator>
      <dc:date>2023-08-01T02:04:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to check for an array of columns and search for specific pattern of events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887225#M350541</link>
      <description>&lt;P&gt;If the only condition is that there needs to be a sequence where 1 appears prior to 0 then below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data Have;
  input st1 $ st2 $ st3 $ st4 $;
  flag = find(compress(cats(of st:),'01','k'),'10')&amp;gt;0;
datalines;
0 1 5 0
0 1 0 5
0 1 0 5
5 1 0 6
1 0 5 D
1 1 5 0
1 2 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;With an array something like below should work.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data demo;
  input st1 $ st2 $ st3 $ st4 $;
  array _st {*} st:;
  do _i=1 to dim(_st);
    if _st[_i]='1' then flag=0;
    else if flag=0 and _st[_i]='0' then
      do;
        flag=1;
        leave;
      end;
  end;
  if missing(flag) then flag=0;
  drop _:;
datalines;
0 1 5 0
0 1 0 5
0 1 0 5
5 1 0 6
1 0 5 D
1 1 5 0
1 2 1 1
;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 01 Aug 2023 02:27:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887225#M350541</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2023-08-01T02:27:56Z</dc:date>
    </item>
    <item>
      <title>Re: How to check for an array of columns and search for specific pattern of events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887227#M350542</link>
      <description>&lt;P&gt;To extend your current idea is easy.&amp;nbsp; You can take advantage of the power of the data step DO loop to make it even simpler.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg=0;
  do i=1 to dim(colname) until (colname[i] ='1');
  end;
  do i=i+1 to dim(colname) until (flg);
    flg=colname[i] ='0';
  end;
  if flg;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Or you can eliminate the first loop by using the WHICHC() function to find the first '1'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg=0;
  i=whichc('1', of colname[*]);;
  if i then do i=i+1 to dim(colname) until (flg);
    flg=colname[i] ='0';
  end;
  if flg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;And if you don't want to include cases where there is a '0' before the first '1' then you could eliminate the do loops completely by just testing if the first '0' is after the first '1'.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test1;
  set have;
  array colname st1-st21; 
  flg= 0 &amp;lt; whichc('1',of colname[*]) &amp;lt; whichc('0',of colname[*]);
  if flg;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 15:57:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887227#M350542</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2023-08-01T15:57:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to check for an array of columns and search for specific pattern of events</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887340#M350582</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt;&amp;nbsp;! this is exactly what I need! Much appreciated!&lt;/P&gt;</description>
      <pubDate>Tue, 01 Aug 2023 15:42:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-check-for-an-array-of-columns-and-search-for-specific/m-p/887340#M350582</guid>
      <dc:creator>kevsma</dc:creator>
      <dc:date>2023-08-01T15:42:25Z</dc:date>
    </item>
  </channel>
</rss>

