<?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: compare values in different rows in different variables. in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35006#M8564</link>
    <description>"...I would like to compare the value on Price A to the last values that shows before "." on Price B and Price C...."&lt;BR /&gt;
&lt;BR /&gt;
Is that now the value of B and C the last time A was not missing - or is this the last time B and C was not missing? Not clear to me - and however I interprete it the case "...compare obs11 value on Price A to obs9 Price B and obs9 Price C..." doesn't make sense to me. Should obs11 be compared to B and C from obs10?&lt;BR /&gt;
&lt;BR /&gt;
Whatsoever: I believe you will need to use 'retain'. Something like this:&lt;BR /&gt;
&lt;BR /&gt;
data demo;&lt;BR /&gt;
set have;&lt;BR /&gt;
retain Br Cr;&lt;BR /&gt;
if _n_=1 then&lt;BR /&gt;
do;&lt;BR /&gt;
Br=b; Cr=c;&lt;BR /&gt;
end;&lt;BR /&gt;
else &lt;BR /&gt;
do;&lt;BR /&gt;
&lt;BR /&gt;
if a ne '.' then&lt;BR /&gt;
do;&lt;BR /&gt;
...compare a with b and c...&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
...assign the vars b and c to the retained ones as this iteration will be the last time 'a' was not missing during the next iteration when a is not missing.&lt;BR /&gt;
Br=b; Cr=c;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
You still will have to think what to do the first time 'a' is not missing. To what values of b and c do you want to compare?&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
    <pubDate>Tue, 29 Dec 2009 11:11:59 GMT</pubDate>
    <dc:creator>Patrick</dc:creator>
    <dc:date>2009-12-29T11:11:59Z</dc:date>
    <item>
      <title>compare values in different rows in different variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35004#M8562</link>
      <description>Hi, I am trying to compare values in different observations in &lt;BR /&gt;
different variables. &lt;BR /&gt;
Whenever there is value on Price A,I would like to compare the value &lt;BR /&gt;
on Price A to the last values that shows before "." on Price B and &lt;BR /&gt;
Price C. &lt;BR /&gt;
For example, compare obs 4 on Price A to obs3 Price B and obs3 Price C. &lt;BR /&gt;
                    compare obs10 value on Price A to obs9 Price B and obs9 Price C &lt;BR /&gt;
                    compare obs11 value on Price A to obs9 Price B and obs9 Price C &lt;BR /&gt;
Since they are not in same line, I am having hard time comparing them. I &lt;BR /&gt;
used lag function to line up on those values in different variables &lt;BR /&gt;
but it doesn't work in obs 10 and obs 11 or obs 25,26,27 case. &lt;BR /&gt;
&lt;BR /&gt;
EX: &lt;BR /&gt;
if price A ^="." then do; &lt;BR /&gt;
If price A (obs4) &amp;gt;= Price C(obs3) then  Cnt = "+"; &lt;BR /&gt;
If price A (obs4)&amp;lt;= Price B (obs3) then Cnt = "-"; &lt;BR /&gt;
end; &lt;BR /&gt;
&lt;BR /&gt;
DataSet&lt;BR /&gt;
obs     Price A Price B Price C &lt;BR /&gt;
1       .       1115.75 1116 &lt;BR /&gt;
2       .       1115.75 1116 &lt;BR /&gt;
3       .       1115.75 1116 &lt;BR /&gt;
4       1115    .       . &lt;BR /&gt;
5       .       1115.75 1116 &lt;BR /&gt;
6       1116    .       . &lt;BR /&gt;
7       .       1115.75 1116 &lt;BR /&gt;
8       .       1115.75 1116 &lt;BR /&gt;
9       .       1115.75 1116 &lt;BR /&gt;
10      1116.5  .       . &lt;BR /&gt;
11      1116    .       . &lt;BR /&gt;
12      .       1115.75 1116 &lt;BR /&gt;
13      .       1115.75 1116 &lt;BR /&gt;
14      .       1115.75 1116 &lt;BR /&gt;
15      1116    .       . &lt;BR /&gt;
16      .       1115.75 1116 &lt;BR /&gt;
17      .       1115.75 1116 &lt;BR /&gt;
18      .       1115.75 1116 &lt;BR /&gt;
19      .       1115.75 1116 &lt;BR /&gt;
20      1116    .       . &lt;BR /&gt;
21      .       1115.75 1116 &lt;BR /&gt;
22      .       1115.75 1116 &lt;BR /&gt;
23      .       1115.75 1116 &lt;BR /&gt;
24      .       1115.75 1116 &lt;BR /&gt;
25      1116    .       . &lt;BR /&gt;
26      1116.25 .       . &lt;BR /&gt;
27      1116.5  .       . &lt;BR /&gt;
28      .       1115.75 1116.5 &lt;BR /&gt;
29      1116.5  .       .</description>
      <pubDate>Mon, 28 Dec 2009 16:25:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35004#M8562</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2009-12-28T16:25:46Z</dc:date>
    </item>
    <item>
      <title>Re: compare values in different rows in different variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35005#M8563</link>
      <description>Explore using the LAG function and/or capture the prior obs value (using RETAIN to a new-named variable).  Also, you will likely need to explore using  BY GROUP PROCESSING (hint: a search argument for you to use against SAS.COM for doc references), and IF FIRST.&lt;VARNAME&gt;  and/or IF LAST.&lt;VARNAME&gt;  in order to keep track of "related" variable values as you process your SAS files.&lt;BR /&gt;
&lt;BR /&gt;
Scott Barry&lt;BR /&gt;
SBBWorks, Inc.&lt;/VARNAME&gt;&lt;/VARNAME&gt;</description>
      <pubDate>Mon, 28 Dec 2009 17:24:00 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35005#M8563</guid>
      <dc:creator>sbb</dc:creator>
      <dc:date>2009-12-28T17:24:00Z</dc:date>
    </item>
    <item>
      <title>Re: compare values in different rows in different variables.</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35006#M8564</link>
      <description>"...I would like to compare the value on Price A to the last values that shows before "." on Price B and Price C...."&lt;BR /&gt;
&lt;BR /&gt;
Is that now the value of B and C the last time A was not missing - or is this the last time B and C was not missing? Not clear to me - and however I interprete it the case "...compare obs11 value on Price A to obs9 Price B and obs9 Price C..." doesn't make sense to me. Should obs11 be compared to B and C from obs10?&lt;BR /&gt;
&lt;BR /&gt;
Whatsoever: I believe you will need to use 'retain'. Something like this:&lt;BR /&gt;
&lt;BR /&gt;
data demo;&lt;BR /&gt;
set have;&lt;BR /&gt;
retain Br Cr;&lt;BR /&gt;
if _n_=1 then&lt;BR /&gt;
do;&lt;BR /&gt;
Br=b; Cr=c;&lt;BR /&gt;
end;&lt;BR /&gt;
else &lt;BR /&gt;
do;&lt;BR /&gt;
&lt;BR /&gt;
if a ne '.' then&lt;BR /&gt;
do;&lt;BR /&gt;
...compare a with b and c...&lt;BR /&gt;
...&lt;BR /&gt;
&lt;BR /&gt;
...assign the vars b and c to the retained ones as this iteration will be the last time 'a' was not missing during the next iteration when a is not missing.&lt;BR /&gt;
Br=b; Cr=c;&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
You still will have to think what to do the first time 'a' is not missing. To what values of b and c do you want to compare?&lt;BR /&gt;
&lt;BR /&gt;
HTH&lt;BR /&gt;
Patrick</description>
      <pubDate>Tue, 29 Dec 2009 11:11:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/compare-values-in-different-rows-in-different-variables/m-p/35006#M8564</guid>
      <dc:creator>Patrick</dc:creator>
      <dc:date>2009-12-29T11:11:59Z</dc:date>
    </item>
  </channel>
</rss>

