<?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: comparing 2rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773274#M245625</link>
    <description>&lt;P&gt;You can define the value of &lt;FONT face="courier new,courier"&gt;var_new&lt;/FONT&gt; in this case as appropriate for your application, e.g., use the value of &lt;FONT face="courier new,courier"&gt;var3name&lt;/FONT&gt; (as my suggested code does), leave it missing, set it to a constant value (e.g., "unique") or to a value indicating which of the three variables &lt;FONT face="courier new,courier"&gt;var1&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;var2&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt; differ, etc. The code follows the specifications.&lt;/P&gt;</description>
    <pubDate>Sun, 10 Oct 2021 16:38:48 GMT</pubDate>
    <dc:creator>FreelanceReinh</dc:creator>
    <dc:date>2021-10-10T16:38:48Z</dc:date>
    <item>
      <title>comparing 2rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773269#M245621</link>
      <description>&lt;P&gt;Dear all,&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question concerning how to compare two rows in SAS.&lt;/P&gt;
&lt;P&gt;I have the following dataset. I wish to compare&amp;nbsp; row1 with row2 and if var1, var2, var3 in row1 is the same as the corresponding variables in row2 then the value of the first var3name should be outputted in a new variable called var_new for all corresponding rows&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data sample_data;
input var1 var2 var3 var3name $;
datalines; 
	01 0 01 ABN 
	01 0 01 ABS
	01 0 01 ABE
	01 0 02 CGM
	01 0 02 CGN
	01 0 02 CGM
	01 0 02 CGR
	02 1 03 DFJ
	02 1 03 DFK
	02 1 03 DFL
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The output should look like this below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
input var1 $ var2 $ var3 $ var3name $ var_new $;
datalines; 
	01 0 01 ABN ABN
	01 0 01 ABS ABN
	01 0 01 ABE ABN
	01 0 02 CGM CGM
	01 0 02 CGN CGM
	01 0 02 CGM CGM
	02 1 03 DFJ DFJ
	02 1 03 DFK DFJ
	02 1 03 DFL DFJ
;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I will appreciate any help&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 15:23:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773269#M245621</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2021-10-10T15:23:05Z</dc:date>
    </item>
    <item>
      <title>Re: comparing 2rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773270#M245622</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/168930"&gt;@Anita_n&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Use BY-group processing and retain variable &lt;FONT face="courier new,courier"&gt;var_new&lt;/FONT&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set sample_data;
by var1-var3;
if first.var3 then var_new=var3name;
retain var_new;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You haven't specified the value of &lt;FONT face="courier new,courier"&gt;var_new&lt;/FONT&gt; in the case that, e.g., the first and second row &lt;EM&gt;differ&lt;/EM&gt; in &lt;FONT face="courier new,courier"&gt;var1&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;var2&lt;/FONT&gt; or &lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt;. My suggested code treats the first row in this case as a group comprised of only one observation, i.e., the value of&amp;nbsp;&lt;FONT face="courier new,courier"&gt;var3name&lt;/FONT&gt; is copied to &lt;FONT face="courier new,courier"&gt;var_new&lt;/FONT&gt;, as always in the first observation of a group.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;(Note that your WANT and SAMPLE_DATA are not exactly consistent.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Edit:&lt;/P&gt;
&lt;P&gt;If your real variable names do not have a common prefix followed by "1", "2", "3," write them out in the BY statement, e.g.,&lt;/P&gt;
&lt;PRE&gt;by red green blue;&lt;/PRE&gt;
&lt;P&gt;If your real data are not sorted by &lt;FONT face="courier new,courier"&gt;var1&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;var2&amp;nbsp;&lt;/FONT&gt;&lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt;, but you still want to compare only consecutive observations, add the NOTSORTED option to the BY statement:&lt;/P&gt;
&lt;PRE&gt;by var1-var3 notsorted;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 16:17:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773270#M245622</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-10T16:17:45Z</dc:date>
    </item>
    <item>
      <title>Re: comparing 2rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773272#M245624</link>
      <description>&lt;P&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733"&gt;@FreelanceReinh&lt;/a&gt;&amp;nbsp; thanks for the quick reply. Yes,&amp;nbsp; I will forgot to specify the case where row1 and row2 differs in var1, var2, and var3, could you suggest how I should go about such cases please. Thanks a lot&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 16:22:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773272#M245624</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2021-10-10T16:22:34Z</dc:date>
    </item>
    <item>
      <title>Re: comparing 2rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773274#M245625</link>
      <description>&lt;P&gt;You can define the value of &lt;FONT face="courier new,courier"&gt;var_new&lt;/FONT&gt; in this case as appropriate for your application, e.g., use the value of &lt;FONT face="courier new,courier"&gt;var3name&lt;/FONT&gt; (as my suggested code does), leave it missing, set it to a constant value (e.g., "unique") or to a value indicating which of the three variables &lt;FONT face="courier new,courier"&gt;var1&lt;/FONT&gt;, &lt;FONT face="courier new,courier"&gt;var2&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt; differ, etc. The code follows the specifications.&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 16:38:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773274#M245625</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2021-10-10T16:38:48Z</dc:date>
    </item>
    <item>
      <title>Re: comparing 2rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773276#M245626</link>
      <description>&lt;P&gt;thanks, your code worked&lt;/P&gt;</description>
      <pubDate>Sun, 10 Oct 2021 16:41:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/comparing-2rows/m-p/773276#M245626</guid>
      <dc:creator>Anita_n</dc:creator>
      <dc:date>2021-10-10T16:41:28Z</dc:date>
    </item>
  </channel>
</rss>

