<?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 compare values in 2 rows within a by group in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650041#M22330</link>
    <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;Thank you so much for the input. I will test out this code next week. This dataset has 2 items because I am examining a substitution from an old model to a new model and identifying the specifications that change between models. The items are grouped by similar type, for example, breads, cars, sewing machines, etc.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 22 May 2020 22:25:01 GMT</pubDate>
    <dc:creator>benjamin_2018</dc:creator>
    <dc:date>2020-05-22T22:25:01Z</dc:date>
    <item>
      <title>How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650012#M22323</link>
      <description>&lt;P&gt;Hello. I have a dataset of specifications for certain items, and each item has an old model and a new model. The data looks something like this.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data items;
input Item $ SpecA $ SpecB $ SpecC $ ;
datalines;
ABC A3 B2 C5
ABC A3 B3 .
DEF .  B2 C4
DEF A1 .  C9
GHI A4 B9 C6
GHI A1 B9 .
;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I want to, for each Item in the first column, compare SpecA, SpecB, and SpecC.&lt;/P&gt;&lt;P&gt;I want the output to have the following:&lt;/P&gt;&lt;P&gt;1) If there is a change from missing to not missing, or a change in value, then return the value of the 2nd row in the by group (the new model).&lt;/P&gt;&lt;P&gt;2) If there is a change from not missing to missing, then return the value from the 1st row in the by group (the old model).&lt;/P&gt;&lt;P&gt;3) If there is no change, then return a blank value.&amp;nbsp;&lt;/P&gt;&lt;P&gt;So here is what the output for the above would look like:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Item&amp;nbsp; SpecA&amp;nbsp; SpecB&amp;nbsp; SpecC&lt;/P&gt;&lt;P&gt;ABC&amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; B3 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp; C5&lt;/P&gt;&lt;P&gt;DEF&amp;nbsp; A1 &amp;nbsp; &amp;nbsp; &amp;nbsp; B2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C9&lt;/P&gt;&lt;P&gt;GHI &amp;nbsp; A1 &amp;nbsp; &amp;nbsp;&amp;nbsp; . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; C6&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for any suggestions on the best approach. I have tried using a array but I'm not too experienced with that feature. I typically use SAS Enterprise Guide 7.15 and SAS 9.4&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 20:36:38 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650012#M22323</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2020-05-22T20:36:38Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650018#M22325</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/238027"&gt;@benjamin_2018&lt;/a&gt;&amp;nbsp; I'm curious to know why does the sample always have &lt;EM&gt;&lt;STRONG&gt;sets of 2 items.&amp;nbsp;&lt;/STRONG&gt;&lt;/EM&gt;Is that really a good representative of your real?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Anyways, for what it's worth--&amp;gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data items;
input Item $ SpecA $ SpecB $ SpecC $ ;
datalines;
ABC A3 B2 C5
ABC A3 B3 .
DEF .  B2 C4
DEF A1 .  C9
GHI A4 B9 C6
GHI A1 B9 .
;
run;

data want;
 set items;
 by item;
 array s  SpecA  SpecB  SpecC;
 array l(3) $ _temporary_;
 do over s;
  if first.item then l(_i_)=s;
  else do;
   if l(_i_)&amp;gt;' ' and s=' ' then s=l(_i_);
   else if l(_i_)&amp;gt;' ' and s=l(_i_) then s=' ';
  end;
 end;
 if last.item;
run; &lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 22 May 2020 21:14:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650018#M22325</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-22T21:14:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650028#M22329</link>
      <description>&lt;P&gt;This would probably benefit from use a conditional SET with POINT=&amp;nbsp; and a bunch of renames:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data items;
input Item $ SpecA $ SpecB $ SpecC $ ;
datalines;
ABC A3 B2 C5
ABC A3 B3 .
DEF .  B2 C4
DEF A1 .  C9
GHI A4 B9 C6
GHI A1 B9 .
run;


data want (drop=frst_:);
  set items;
  by item;
  if first.item then set items (rename=(speca=frst_speca specb=frst_specb specc=frst_specc)) point=_n_; 
  array spc  spec:;
  array fsp  frst_spec: ;
  if last.item;
  do over spc ;
    if spc=fsp then spc=' '; else
    if missing(spc) then spc=fsp; 
  end;
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>Fri, 22 May 2020 21:21:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650028#M22329</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-05-22T21:21:00Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650041#M22330</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/138205"&gt;@novinosrin&lt;/a&gt;Thank you so much for the input. I will test out this code next week. This dataset has 2 items because I am examining a substitution from an old model to a new model and identifying the specifications that change between models. The items are grouped by similar type, for example, breads, cars, sewing machines, etc.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 22:25:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650041#M22330</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2020-05-22T22:25:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650042#M22331</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/31461"&gt;@mkeintz&lt;/a&gt;Ok, thank you! I've not used the point= option on the set statement so I will need to look at that. At least I was on the right path by using 2 arrays, a first. and last. automatic variable with the by group, and a do loop. I will test this option out when I'm back at work next week. Thank you very much! Ben&lt;/P&gt;</description>
      <pubDate>Fri, 22 May 2020 22:29:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650042#M22331</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2020-05-22T22:29:48Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650060#M22333</link>
      <description>&lt;P&gt;You can also use a self-merge with offset as below - no extra SET statement needed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data items;
input Item $ SpecA $ SpecB $ SpecC $ ;
datalines;
ABC A3 B2 C5
ABC A3 B3 .
DEF .  B2 C4
DEF A1 .  C9
GHI A4 B9 C6
GHI A1 B9 .
run;
data want (drop=frst_:);
  merge items (rename=(item=frst_item speca=frst_speca specb=frst_specb specc=frst_specc))
        items (firstobs=2);
  if item=frst_item;
  array spc  spec:;
  array fsp  frst_spec: ;
  do over spc ;
    if spc=fsp then spc=' '; else
    if missing(spc) then spc=fsp; 
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But note you can NOT use a BY statement with this merge.&amp;nbsp; Because if you do, the initial offset of the two streams of data from HAVE will be lost when the second BY-group is encountered.&amp;nbsp; I.e. the first output obs would have data from incoming obs1 and obs2, but the second output obs would pause the second data stream (as it had already reached the end of the by group.&lt;/P&gt;</description>
      <pubDate>Sat, 23 May 2020 01:23:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650060#M22333</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2020-05-23T01:23:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650699#M22352</link>
      <description>&lt;P&gt;I got this to work! Thank you. I just added a length to the elements in the temporary array because in my real dataset the lengths for all character variables are 64.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 12:36:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650699#M22352</guid>
      <dc:creator>benjamin_2018</dc:creator>
      <dc:date>2020-05-26T12:36:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to compare values in 2 rows within a by group</title>
      <link>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650708#M22353</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/238027"&gt;@benjamin_2018&lt;/a&gt;&amp;nbsp; I'm glad. Yes Indeed, I did assume you would be able to adjust the length. Good &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; Have a good day and stay safe&lt;/P&gt;</description>
      <pubDate>Tue, 26 May 2020 12:48:02 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/How-to-compare-values-in-2-rows-within-a-by-group/m-p/650708#M22353</guid>
      <dc:creator>novinosrin</dc:creator>
      <dc:date>2020-05-26T12:48:02Z</dc:date>
    </item>
  </channel>
</rss>

