<?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 create a new variable by comparing two obs in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456264#M115552</link>
    <description>&lt;P&gt;Every call of the lag() function retrieves a value from the FIFO chain and feeds a new value into it, so you must call lag() only once per data step iteration for a given variable.&lt;/P&gt;
&lt;P&gt;And&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if test and&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means that test is converted to numeric and used as a boolean value, which is not what you intended IMO.&lt;/P&gt;
&lt;P&gt;Maxim 2: Read the log! It will alert you to such unwanted effects.&lt;/P&gt;</description>
    <pubDate>Sun, 22 Apr 2018 05:47:37 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2018-04-22T05:47:37Z</dc:date>
    <item>
      <title>how to create a new variable by comparing two obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456241#M115540</link>
      <description>&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need help in my code in data step three.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First I sorted data and ran a data step to get last record for 'id test date'.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then I need find if 'test' in lab1 and lab2 and at least one obs where value= 'neg' then want='neg';&lt;/P&gt;
&lt;P&gt;else if 'test' in lab1 and lab2 and at least one obs has the vaiable 'value'&amp;nbsp; other than neg or blank then want='pos';&lt;/P&gt;
&lt;P&gt;if 'test' in lab1 and lab2 and both values of variable 'value' are blank then want='un';&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;output needed;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;id test value date           want&lt;BR /&gt;a lab1 neg 2015-10-10         neg
a lab2 neg 2015-10-10         neg
b lab1 neg 2015-10-01         neg
b lab2     2015-10-01         neg   
c lab1 pos 2015-10-01         pos
c lab2     2015-10-01         pos
d lab1 eqi 2015-10-01         pos
d lab2     2015-10-01         pos
e lab1     2015-10-01         un
e lab2     2015-10-01         un&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;input&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
input id $1 test $3-6 value $8-10 date$11-21;
datalines;
a lab1 neg 2015-10-01
a lab2 neg 2015-10-01
a lab1 neg 2015-10-10
a lab2 neg 2015-10-10
b lab1 neg 2015-10-01
b lab2     2015-10-01       
c lab1 pos 2015-10-01
c lab2     2015-10-01
d lab1 eqi 2015-10-01
d lab2     2015-10-01
e lab1     2015-10-01
e lab2     2015-10-01
;
proc sort data=one;
by id test date;
run;

data two;
set one;
by id test date;
if last.test;
run;
proc sort data=two;
by id date test;
run;

data three;
set two;
if test and lag(test) in ('lab1' 'lab2') and value and lag(value) in (('neg' 'neg') or ('neg' '')) then want='neg';
else if test and lag(test) in ('lab1' 'lab2') and value and lag(value) not in (('neg' 'neg') or ('' '')) then want='pos';
else if test and lag(test) in ('lab1' 'lab2') and value and lag(value) not in (('' '')) then want='un';
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Apr 2018 00:04:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456241#M115540</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-04-22T00:04:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to create a new variable by comparing two obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456258#M115550</link>
      <description>&lt;P&gt;It is not recommended to use lag function in a conditional statement.&lt;/P&gt;
&lt;P&gt;Try next code (the 3rd step only):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data three;
 set two;
  by id;
     retain lag_test lag_value;
     lag_test = lag(test);
     lag_value = lag(value);

     if test ne lag_test then do; 
        if (value='neg' or lag_value='neg') then want='neg'; else
        if (value=' ' and lag_value=' ')  then want='un'; else want='pos';
     end;
     drop lag_test lag_value;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Apr 2018 03:59:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456258#M115550</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2018-04-22T03:59:12Z</dc:date>
    </item>
    <item>
      <title>Re: how to create a new variable by comparing two obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456259#M115551</link>
      <description>&lt;P&gt;Not sure if I follow your rules, but the following achieves your desired result for your example:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data one;
  informat date yymmdd10.;
  input id $ test $ value $ date;
  format date date9.;
  datalines;
a lab1 neg 2015-10-01
a lab2 neg 2015-10-01
a lab1 neg 2015-10-10
a lab2 neg 2015-10-10
b lab1 neg 2015-10-01
b lab2 .   2015-10-01       
c lab1 pos 2015-10-01
c lab2 .   2015-10-01
d lab1 eqi 2015-10-01
d lab2 .   2015-10-01
e lab1 .   2015-10-01
e lab2 .   2015-10-01
;

data two;
  set one (where=(test in ('lab1','lab2')));
  if value eq 'eqi' then wanta='pos';
  else if missing(value) then wanta='un';
  else wanta=value;
run;

proc sort data=two;
  by id descending test wanta descending date;
run;

proc sort data=two nodupkey;
  by id descending test wanta;
run;

data want;
  do until (last.id);
    set two;
    by id;
    if wanta eq 'pos' then pos=1;
    else if wanta eq 'neg' then neg=1;
    else if wanta eq 'un' then un=1;
  end;
  do until (last.id);
    set one;
    by id;
    if neg then do;
      if not pos and not un then do;
        if first.id then output;
      end;
      else do;
        wanta='neg';
        output;
      end;
    end;
    else if pos then do;
      wanta='pos';
      output;
    end;
    else if not pos and not neg then output;
  end;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Art, CEO, AnalystFinder.com&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 22 Apr 2018 04:24:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456259#M115551</guid>
      <dc:creator>art297</dc:creator>
      <dc:date>2018-04-22T04:24:04Z</dc:date>
    </item>
    <item>
      <title>Re: how to create a new variable by comparing two obs</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456264#M115552</link>
      <description>&lt;P&gt;Every call of the lag() function retrieves a value from the FIFO chain and feeds a new value into it, so you must call lag() only once per data step iteration for a given variable.&lt;/P&gt;
&lt;P&gt;And&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if test and&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;means that test is converted to numeric and used as a boolean value, which is not what you intended IMO.&lt;/P&gt;
&lt;P&gt;Maxim 2: Read the log! It will alert you to such unwanted effects.&lt;/P&gt;</description>
      <pubDate>Sun, 22 Apr 2018 05:47:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-create-a-new-variable-by-comparing-two-obs/m-p/456264#M115552</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2018-04-22T05:47:37Z</dc:date>
    </item>
  </channel>
</rss>

