<?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: select observations if same entries are adjacent within the same variable group in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326482#M72705</link>
    <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Define two separate arrays based on two group of variables. Please try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array v13(*) var1-var3;&lt;BR /&gt;array v46(*) var4-var6;&lt;BR /&gt;flag=0;&lt;BR /&gt;do i=1 to dim(v13)-1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if v13{i}=v13{i+1} then flag+1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if v46{i}=v46{i+1} then flag+1;&lt;BR /&gt;end;&lt;BR /&gt;if flag;&lt;BR /&gt;run;&lt;/P&gt;</description>
    <pubDate>Sat, 21 Jan 2017 04:48:25 GMT</pubDate>
    <dc:creator>stat_sas</dc:creator>
    <dc:date>2017-01-21T04:48:25Z</dc:date>
    <item>
      <title>select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326465#M72690</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;suppsoe I have the following data:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE style="border-collapse: collapse; width: 288pt;" border="0" width="384" cellspacing="0" cellpadding="0"&gt;&lt;COLGROUP&gt;&lt;COL style="width: 48pt;" span="6" width="64" /&gt; &lt;/COLGROUP&gt;
&lt;TBODY&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD width="64" height="20" style="height: 15.0pt; width: 48pt;"&gt;var1&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;var2&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;var3&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;var4&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;var5&lt;/TD&gt;
&lt;TD width="64" style="width: 48pt;"&gt;var6&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR style="height: 15.0pt;"&gt;
&lt;TD height="20" style="height: 15.0pt;"&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;TD&gt;b&lt;/TD&gt;
&lt;TD&gt;a&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;var1 - var 3 are considered one group, and var4 - var6 are considered a second group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;what I would like to do is to select observations where there are identical entries adjacent to each other within the same group.&lt;/P&gt;
&lt;P&gt;So the first row will be selected because the value "a" is present for var1 and var2 (and they are adjacent within the first group), and also the value "b" is present in var5 and var6 (and they are adjacent within the second group).&lt;/P&gt;
&lt;P&gt;Likewise the second row will also be selected because "b" is in var2 and var3. The third row will NOT be selected.&lt;/P&gt;
&lt;P&gt;I guess that the tricky art here is to code for "adjacentness within group"&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks!&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326465#M72690</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-21T03:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326474#M72697</link>
      <description>&lt;P&gt;If this really represents your problem, it's easy enough:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;if var1=var2 or var2=var3 or var4=var5 or var5=var6;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you actually have hundreds of variables instead, the solution would use arrays. &amp;nbsp;But let's not go there unless it's needed.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:42:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326474#M72697</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-21T03:42:10Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326475#M72698</link>
      <description>&lt;P&gt;Another option, define two different arrays and check for adjacent within each array.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:46:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326475#M72698</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-01-21T03:46:14Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326476#M72699</link>
      <description>&lt;P&gt;I'm not sure if I understand the requirements but here is the solution for what I think is being asked:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have;
  infile cards dlm=',';
  informat var1-var6 $1.;
  length test $1;
  input var1-var6;
  array b $ var4-var6;
  call missing(test);
  if var1 eq var2 then test=var1;
  else if var2 eq var3 then test=var2;
  if not missing(test) and
     var1 in b and
     var3 in b;
  cards;
a,a,b,a,b,b
a,b,b,a,b,a
a,b,a,a,b,a
;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 03:50:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326476#M72699</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-01-21T03:50:44Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326482#M72705</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Define two separate arrays based on two group of variables. Please try this.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;BR /&gt;set have;&lt;BR /&gt;array v13(*) var1-var3;&lt;BR /&gt;array v46(*) var4-var6;&lt;BR /&gt;flag=0;&lt;BR /&gt;do i=1 to dim(v13)-1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if v13{i}=v13{i+1} then flag+1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if v46{i}=v46{i+1} then flag+1;&lt;BR /&gt;end;&lt;BR /&gt;if flag;&lt;BR /&gt;run;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:48:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326482#M72705</guid>
      <dc:creator>stat_sas</dc:creator>
      <dc:date>2017-01-21T04:48:25Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326483#M72706</link>
      <description>&lt;P&gt;Hi art297,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;thanks for the code. I ran it and it gave me the first 2 rows. But then to test it further I made a small change to the second observation:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;a,b,a,a,b,b --&amp;gt; I made the b's be adjacent in the second group. When I ran th ecode again I got only the first row and not also the second row, so its as if the second group isn't included in the selection process.&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 04:53:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326483#M72706</guid>
      <dc:creator>ilikesas</dc:creator>
      <dc:date>2017-01-21T04:53:29Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326488#M72711</link>
      <description>&lt;P&gt;Then try it with this modification:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data have (drop=test:);
  infile cards dlm=',';
  informat var1-var6 $1.;
  length test1 test2 $1;
  input var1-var6;
  array a $ var1-var3;
  array b $ var4-var6;
  call missing(test1);
  call missing(test2);
  if var1 eq var2 then test1=var1;
  else if var2 eq var3 then test1=var2;
  else if var4 eq var5 then test2=var4;
  else if var5 eq var6 then test2=var5;
  if not missing(test1) then do;
    if var1 in b and var3 in b then output;
  end;
  else if not missing(test2) then do;
    if var4 in a and var6 in a then output;
  end;
  cards;
a,a,b,a,b,b
a,b,b,a,b,a
a,b,a,a,b,a
a,b,a,a,b,b
;
&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 05:37:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326488#M72711</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2017-01-21T05:37:30Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326496#M72714</link>
      <description>&lt;P&gt;This is getting very close to the solution I pictured for hundreds of variables. &amp;nbsp;There are two changes to consider:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;Must both groups contain the same number of variables?&lt;/LI&gt;
&lt;LI&gt;Should processing be cut short once a match is found?&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;Here would be the result:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;data want;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set have;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;array group1 {*} first list of many variable names;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;array group2 {*} another set containing variables that belong&amp;nbsp;in the second group;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;flag=0;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;do i=1 to dim(group1)-1 until (flag=1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;if group1{i}=group1{i+1} then flag=1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;end;&lt;BR /&gt;if flag=0 then do i=1 to dim(group2)-1 until (flag=1);&lt;BR /&gt;&amp;nbsp; &amp;nbsp;if group2{i}=group2{i+1} then flag=1;&lt;BR /&gt;end;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if flag;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 12:40:53 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326496#M72714</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-01-21T12:40:53Z</dc:date>
    </item>
    <item>
      <title>Re: select observations if same entries are adjacent within the same variable group</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326507#M72720</link>
      <description>&lt;P&gt;If there are multple groups of varying sizes, there is still a way to avoid multiple arrays and do groups.&amp;nbsp; Say you have 100 vars in 6 groups of size 10, 20, 30, 20,10, 10.&amp;nbsp; Placed in an array of 100 vars, they would have "upper bounds" at elements 10, 30, 60, 80, 90, and 100 respectively:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
  set have;
  array var{*}   var1-var100;
  array upbnds{6} _temporary_ (10 30 60 80 90 100);

  U=1;
  do V=1 to dim(var)-1 until (flag=1);
    if V=upbnds{U} then U=U+1;
    else if var{v}=var{v+1} then flag=1;
  end;
  if flag;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 21 Jan 2017 14:54:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-observations-if-same-entries-are-adjacent-within-the-same/m-p/326507#M72720</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-01-21T14:54:31Z</dc:date>
    </item>
  </channel>
</rss>

