<?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: Help understanding SAS logic (order of operations on a combined sas data merge and if) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941953#M369466</link>
    <description>&lt;P&gt;Can you explain what you are trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first two steps create datasets. (although they both have an extra RUN statement after them that will do nothing.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first MERGE is combining the observations by VAR1.&amp;nbsp; Since DF2 ("df" is that short for "dataframe"?&amp;nbsp; Note that in SAS these are called datasets.) only has one observation per value of VAR1 they are read into the merge step only once.&amp;nbsp; If you change the value of VAR3 in the data step then there is no way for the value to be changed back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run that step checking for VAR2=5 instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
    input var1 $ var2;
datalines;
A 4
A 5
A 6
B 4
B 5
B 6
;

data ds2;
    input var1 $ var3;
datalines;
A 10
B 20
;

data ds3;
  merge ds1 ds2;
  by var1;
  if var2 = 5 then var3 = 30;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    var1    var2    var3

 1      A        4      10
 2      A        5      30
 3      A        6      30
 4      B        4      20
 5      B        5      30
 6      B        6      30
&lt;/PRE&gt;
&lt;P&gt;If you want to remember the old value of VAR3 then make a new variable instead.&amp;nbsp; You can always add a RENAME statement so the names used when writing the DS3 will be different.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds3;
  merge ds1 ds2;
  by var1;
  if var2 = 5 then newvar3 = 30;
  else newvar3=var3;
  rename var3=oldvar3 newvar3=var3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    var1    var2    oldvar3    var3

 1      A        4        10       10
 2      A        5        10       30
 3      A        6        10       10
 4      B        4        20       20
 5      B        5        20       30
 6      B        6        20       20
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 30 Aug 2024 13:24:40 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2024-08-30T13:24:40Z</dc:date>
    <item>
      <title>Help understanding SAS logic (order of operations on a combined sas data merge and if)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941935#M369461</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone explain why SAS is doing what it is doing here:&lt;/P&gt;&lt;PRE&gt;data df1;
    input var1 $ var2;
    datalines;
A 4
A 5
B 4
B 5
;
run;

data df2;
    input var1 $ var3;
    datalines;
A 10
B 20
;
run;

data df3;
merge df1 df2;
by var1;
if var2 = 4 then var3 = 30;
run;&lt;/PRE&gt;&lt;P&gt;To get what I want I need to split the merge and if statements:&lt;/P&gt;&lt;PRE&gt;data df4;
merge df1 df2;
by var1;
run;
data df4;
set df4;
if var2 = 4 then var3 = 30;
run;&lt;/PRE&gt;&lt;P&gt;I'm just not getting how SAS is operating when calculating df3.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 12:21:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941935#M369461</guid>
      <dc:creator>python_user</dc:creator>
      <dc:date>2024-08-30T12:21:39Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding SAS logic (order of operations on a combined sas data merge and if)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941953#M369466</link>
      <description>&lt;P&gt;Can you explain what you are trying to do?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first two steps create datasets. (although they both have an extra RUN statement after them that will do nothing.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your first MERGE is combining the observations by VAR1.&amp;nbsp; Since DF2 ("df" is that short for "dataframe"?&amp;nbsp; Note that in SAS these are called datasets.) only has one observation per value of VAR1 they are read into the merge step only once.&amp;nbsp; If you change the value of VAR3 in the data step then there is no way for the value to be changed back.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Run that step checking for VAR2=5 instead.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds1;
    input var1 $ var2;
datalines;
A 4
A 5
A 6
B 4
B 5
B 6
;

data ds2;
    input var1 $ var3;
datalines;
A 10
B 20
;

data ds3;
  merge ds1 ds2;
  by var1;
  if var2 = 5 then var3 = 30;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    var1    var2    var3

 1      A        4      10
 2      A        5      30
 3      A        6      30
 4      B        4      20
 5      B        5      30
 6      B        6      30
&lt;/PRE&gt;
&lt;P&gt;If you want to remember the old value of VAR3 then make a new variable instead.&amp;nbsp; You can always add a RENAME statement so the names used when writing the DS3 will be different.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ds3;
  merge ds1 ds2;
  by var1;
  if var2 = 5 then newvar3 = 30;
  else newvar3=var3;
  rename var3=oldvar3 newvar3=var3;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Result&lt;/P&gt;
&lt;PRE&gt;OBS    var1    var2    oldvar3    var3

 1      A        4        10       10
 2      A        5        10       30
 3      A        6        10       10
 4      B        4        20       20
 5      B        5        20       30
 6      B        6        20       20
&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 13:24:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941953#M369466</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2024-08-30T13:24:40Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding SAS logic (order of operations on a combined sas data merge and if)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941960#M369468</link>
      <description>&lt;P&gt;Sorry about the code quality, I rarely create datasets from hard coded inputs and I've just been conditioned that a "data" step needs a "run". And yes, I guess my tables should be named dtx &lt;span class="lia-unicode-emoji" title=":face_with_tongue:"&gt;😛&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I wanted to merge datasets, then have the if statement apply to the post-merged datasets. SAS (as you point out) does not complete the merge until the 'run' line, and over writes the merged values affected by if for all subsequent rows until the merge value changes.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But thank you for the example &amp;amp; explanation. I get how SAS got the result it got.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks again.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 13:38:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941960#M369468</guid>
      <dc:creator>python_user</dc:creator>
      <dc:date>2024-08-30T13:38:04Z</dc:date>
    </item>
    <item>
      <title>Re: Help understanding SAS logic (order of operations on a combined sas data merge and if)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941973#M369472</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/328313"&gt;@python_user&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;SAS (as you point out) does not complete the merge until the 'run' line, and over writes the merged values affected by if for all subsequent rows until the merge value changes.&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Actually, the "overwriting" takes place only in the observation where the conditional assignment statement is executed. In subsequent observations of the same BY group the value is just &lt;EM&gt;retained&lt;/EM&gt; (i.e. repeated). Overwriting is also what makes your final DATA step (using the SET statement) work.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The key fact is that&lt;/P&gt;
&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;SPAN&gt;"The variables read using the MERGE statement are retained in the PDV." (&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lestmtsref/n1i8w2bwu1fn5kn1gpxj18xttbb0.htm" target="_blank" rel="noopener"&gt;documentation of the MERGE statement&lt;/A&gt;)&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;
&lt;P&gt;In the DATA step creating dataset DF3 the MERGE statement reads &lt;FONT face="courier new,courier"&gt;var3=10&lt;/FONT&gt; from dataset DF2 while processing the first observation of the BY group &lt;FONT face="courier new,courier"&gt;var1='A'&lt;/FONT&gt;. Dataset DF3, having only one observation with&amp;nbsp;&lt;FONT face="courier new,courier"&gt;var1='A'&lt;/FONT&gt;, does not contribute a new value of &lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt; for the second observation of that BY group. Due to the implied RETAIN, the value of &lt;FONT face="courier new,courier"&gt;var3&lt;/FONT&gt; from the previous observation is repeated. While this is the desired behavior in the absence of the IF-THEN statement, the repetition of value &lt;FONT face="courier new,courier"&gt;30&lt;/FONT&gt;&amp;nbsp;introduced by the conditional assignment statement seems to contradict the IF condition. But the issue is that the original value &lt;FONT face="courier new,courier"&gt;10&lt;/FONT&gt; has been overwritten by the assignment statement &lt;FONT face="courier new,courier"&gt;var3=30&lt;/FONT&gt; and hence is no longer available. (Analogous behavior in the second BY group.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This is even similar in the DATA step updating dataset DF4. The SET statement also implies a RETAIN for &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;, &lt;EM&gt;but&lt;/EM&gt; this time the retained value&amp;nbsp;&lt;FONT face="courier new,courier"&gt;var3=30&lt;/FONT&gt;&amp;nbsp;from the first observation with &lt;FONT face="courier new,courier"&gt;var1='A'&lt;/FONT&gt;&amp;nbsp;is &lt;EM&gt;overwritten&lt;/EM&gt; by the value &lt;FONT face="courier new,courier"&gt;var3=10&lt;/FONT&gt; &lt;EM&gt;read&lt;/EM&gt; from the second observation with &lt;FONT face="courier new,courier"&gt;var1='A'&lt;/FONT&gt;&amp;nbsp;in dataset DF4.&lt;/P&gt;</description>
      <pubDate>Fri, 30 Aug 2024 13:58:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-understanding-SAS-logic-order-of-operations-on-a-combined/m-p/941973#M369472</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2024-08-30T13:58:29Z</dc:date>
    </item>
  </channel>
</rss>

