<?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 do I calculate a variable based on another variable value arranged in multiple rows in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319096#M70006</link>
    <description>&lt;P&gt;Thank you Jag, I am a couple of issues with this one- I would need all the observations in the final dataset, for some reason I am geting fewer obs. Also somewhere the logic is not working.&lt;/P&gt;&lt;P&gt;Nevertheless, I appreciate your help!&lt;/P&gt;</description>
    <pubDate>Thu, 15 Dec 2016 02:43:36 GMT</pubDate>
    <dc:creator>ahhh</dc:creator>
    <dc:date>2016-12-15T02:43:36Z</dc:date>
    <item>
      <title>How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319081#M69995</link>
      <description>&lt;P&gt;I need urgent help with this situation. I have a dataset that looks like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID &amp;nbsp; VISIT &amp;nbsp; &amp;nbsp; &amp;nbsp; T0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; RESULT&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; V1 &amp;nbsp; &amp;nbsp; &amp;nbsp;07SEP2005 &amp;nbsp; &amp;nbsp;negative&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; v2 &amp;nbsp; &amp;nbsp; &amp;nbsp;06oct2005 &amp;nbsp; &amp;nbsp; &amp;nbsp; negative&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; v3 &amp;nbsp; &amp;nbsp; &amp;nbsp;01apr2006 &amp;nbsp; &amp;nbsp; &amp;nbsp;positive&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; v4 &amp;nbsp; &amp;nbsp; &amp;nbsp;03sep2006 &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;BR /&gt;1 &amp;nbsp; &amp;nbsp; v5 &amp;nbsp; &amp;nbsp; &amp;nbsp;08sep2006 &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; v1 &amp;nbsp; &amp;nbsp; &amp;nbsp;07sep2005 &amp;nbsp; &amp;nbsp; &amp;nbsp;negative&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; v2 &amp;nbsp; &amp;nbsp; &amp;nbsp;02oct2005 &amp;nbsp; &amp;nbsp; &amp;nbsp; positive&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; v3 &amp;nbsp; &amp;nbsp; &amp;nbsp;05nov2005 &amp;nbsp; &amp;nbsp; &amp;nbsp;positive&lt;BR /&gt;2 &amp;nbsp; &amp;nbsp; v4 &amp;nbsp; &amp;nbsp; 10nov2006 &amp;nbsp; &amp;nbsp; &amp;nbsp; negative&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp; v1 &amp;nbsp; &amp;nbsp; &amp;nbsp;07SEP2005 &amp;nbsp; &amp;nbsp; negative&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp; v2 &amp;nbsp; &amp;nbsp; &amp;nbsp;03OCT2005 &amp;nbsp; &amp;nbsp;positive&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp; v3 &amp;nbsp; &amp;nbsp; &amp;nbsp;09nov2005&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp; v4 &amp;nbsp; &amp;nbsp; 10dec2005 &amp;nbsp; &amp;nbsp; &amp;nbsp; negative&lt;BR /&gt;3 &amp;nbsp; &amp;nbsp; v5 &amp;nbsp; &amp;nbsp; &amp;nbsp;03mar2006 &amp;nbsp; &amp;nbsp; positive&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Number of visits may vary for each id and sometimes 'results' might be missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;For each unique ID:&lt;/P&gt;&lt;P&gt;1.) When there is 'positive' test result, we need to look for next consecutive 'negative' result and- we need to calculate the difference between the dates of the first positive result and the date of the first negative result after the first positive result and a new variable 'changed_result' is set to 1&lt;/P&gt;&lt;P&gt;2.) if there are no positive result at all for an id-, no diff in dates is calculated and '&lt;SPAN&gt;changed_result'&lt;/SPAN&gt;&amp;nbsp;=0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope I can get some answers soon. I use BASE SAS V9&lt;/P&gt;&lt;P&gt;Thank you in advance for your help.&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 00:41:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319081#M69995</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-15T00:41:48Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319084#M69998</link>
      <description>&lt;P&gt;Please post your expected output, both values and file.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 01:23:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319084#M69998</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-12-15T01:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319087#M70001</link>
      <description>&lt;P&gt;Here is my dataset and the output I need:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID VISIT T0 RESULT&lt;BR /&gt;1 V1 07SEP2005 negative&lt;BR /&gt;1 v2 06oct2005 negative&lt;BR /&gt;1 v3 01apr2006 positive&lt;BR /&gt;1 v4 03sep2006 negative&lt;BR /&gt;1 v5 08sep2006 negative&lt;BR /&gt;2 v1 07sep2005 negative&lt;BR /&gt;2 v2 02oct2005 positive&lt;BR /&gt;2 v3 05nov2005 positive&lt;BR /&gt;2 v4 10nov2006 negative&lt;BR /&gt;3 v1 07SEP2005 negative&lt;BR /&gt;3 v2 03OCT2005 positive&lt;BR /&gt;3 v3 09nov2005&lt;BR /&gt;3 v4 10dec2005 negative&lt;BR /&gt;3 v5 03mar2006 positive&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;expected output:&lt;/P&gt;&lt;P&gt;ID VISIT T0 RESULT diff changed_result&lt;BR /&gt;1 V1 07SEP2005 negative . 0&lt;BR /&gt;1 v2 06oct2005 negative . 0&lt;BR /&gt;1 v3 01apr2006 positive . 0&lt;BR /&gt;1 v4 03sep2006 negative 155 1&lt;BR /&gt;1 v5 08sep2006 negative . 0&lt;BR /&gt;2 v1 07sep2005 negative . 0&lt;BR /&gt;2 v2 02oct2005 positive . 0&lt;BR /&gt;2 v3 05nov2005 positive . 0&lt;BR /&gt;2 v4 10nov2006 negative 404 1&lt;BR /&gt;3 v1 07SEP2005 negative . 0&lt;BR /&gt;3 v2 03OCT2005 positive . 0&lt;BR /&gt;3 v3 09nov2005 . 0&lt;BR /&gt;3 v4 10dec2005 negative 68 1&lt;BR /&gt;3 v5 03mar2006 positive . 0&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The date value for first positive needs to be stored and compared to the one for first negative after the first positive.&lt;/P&gt;&lt;P&gt;For ID1 :&amp;nbsp;&lt;SPAN&gt;01apr2006 -&amp;nbsp;03sep2006;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for id2 :&amp;nbsp;02oct2005 -&amp;nbsp;10nov2006;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;for id3:&amp;nbsp;03OCT2005 -&amp;nbsp;10dec2005&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thank you&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 01:41:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319087#M70001</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-15T01:41:13Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319088#M70002</link>
      <description>&lt;P&gt;Hope you are expecting an output as below. if yes then follow this code&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
do until(first.id) ;
set have(where=(result='positive'));
by id visit;
posdate=t0;
end;
do until(last.id) ;
set have;
by id visit;
if lag(result) in ('positive',' ') and result='negative' and t0&amp;gt;posdate then changed_result=1; 
else changed_result=0; &lt;BR /&gt;if result='negative' and t0&amp;gt;posdate then diff=t0-posdate;
output;
end;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;BR /&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/13258i348E98FB8B2FE743/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;</description>
      <pubDate>Thu, 15 Dec 2016 02:07:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319088#M70002</guid>
      <dc:creator>Jagadishkatam</dc:creator>
      <dc:date>2016-12-15T02:07:55Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319089#M70003</link>
      <description>&lt;P&gt;Try next code - changed according to your answer:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;set have;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; by id;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;retain &amp;nbsp;phase pos_d neg_d ;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;STRONG&gt;changed_result =0; &amp;nbsp;/* initial value */&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if first.id then phase = 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if phase = 0 and result = 'positive'&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then do; &amp;nbsp;pos_d = t0; phase=1; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if phase = 1 and result = 'negative'&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;then do; neg_d = t0; phase =2; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if last.id the do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if phase = 2 then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;dif_days = neg_d - pos_d;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;changed_result = 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; else do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; dif_days = .;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; changed_result = 0;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;***output;&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;keep ID dif_days changed_result;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 01:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319089#M70003</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-12-15T01:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319094#M70005</link>
      <description>&lt;P&gt;This code works great, Thanks a ton SHMUEL &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 02:41:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319094#M70005</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-15T02:41:25Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319096#M70006</link>
      <description>&lt;P&gt;Thank you Jag, I am a couple of issues with this one- I would need all the observations in the final dataset, for some reason I am geting fewer obs. Also somewhere the logic is not working.&lt;/P&gt;&lt;P&gt;Nevertheless, I appreciate your help!&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 02:43:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319096#M70006</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-15T02:43:36Z</dc:date>
    </item>
    <item>
      <title>Re: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319152#M70013</link>
      <description>&lt;P&gt;You don't really need the double-dow here (i.e. two "do until (last.id)" groups), because there is no need to go back and re-read records.&amp;nbsp; All one should be doing is to maintain the current status, i.e. whether a first positive record has been encountered.&amp;nbsp; If so, then a secondary status is checked, namely whether a subsequent negative has been encountered:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Note the retained variables FIRST_POS in the program below can have three values:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;a missing value, set at the first.id records, which is continued until ...&lt;/LI&gt;
&lt;LI&gt;a T0 value corresponding to the first "positive" record, (&lt;EM&gt;if result="positive" and first_pos=.&lt;/EM&gt;)&amp;nbsp;which is continued until ...&lt;/LI&gt;
&lt;LI&gt;the next "negative" record, (&lt;EM&gt;if result='negative' and&amp;nbsp;not(missing(first_pos)&lt;/EM&gt; )&amp;nbsp;when DIFF is calculated, and first_pos is set to .Z.&lt;BR /&gt;&lt;BR /&gt;It's set to .Z instead of ., so that a subsequent "positive" won't inadvertantly restart the process.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want (drop=first_pos);
  set have;
  by id;

  retain first_pos;  /* T0 date of first positive rec */

  if result='positive' and first_pos=. then first_pos=t0;

  if result='negative' and not(missing(first_pos)) then do;
    changed_result=1;
    diff=t0-first_pos;
    first_pos=.Z;
  end;
  else changed_result=0;

  if last.id then first_pos=.;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 15 Dec 2016 04:57:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319152#M70013</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2016-12-15T04:57:59Z</dc:date>
    </item>
    <item>
      <title>UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319422#M70149</link>
      <description>&lt;P&gt;There is an update in the algorithm, please help:&lt;/P&gt;&lt;P&gt;CALCULATING THE TIME BETWEEN THE FIRST POSITIVE TO HOW EVER LONG THEY REMAIN POSITIVE (BEFORE HAVING A NEGATIVE OR UNKNOWN/MISSING RESULT)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID VISIT T0 RESULT&lt;BR /&gt;1 V1 07SEP2005 negative&lt;BR /&gt;1 v2 06oct2005 negative&lt;BR /&gt;1 v3 01apr2006 positive&lt;BR /&gt;1 v4 03sep2006 negative&lt;BR /&gt;1 v5 08sep2006 negative&lt;BR /&gt;2 v1 07sep2005 negative&lt;BR /&gt;2 v2 02oct2005 positive&lt;BR /&gt;2 v3 05nov2005 positive&lt;BR /&gt;2 v4 10nov2006 negative&lt;BR /&gt;3 v1 07SEP2005 negative&lt;BR /&gt;3 v2 03OCT2005 positive&lt;BR /&gt;3 v3 09nov2005&lt;BR /&gt;3 v4 10dec2005 negative&lt;BR /&gt;3 v5 03mar2006 positive&lt;BR /&gt;4 v1 07SEP2005 negative&lt;BR /&gt;4 v2 10SEP2005 negative&lt;BR /&gt;4 v3 07OCT2005 negative&lt;BR /&gt;4 v4 17nOV2005 negative&lt;BR /&gt;4 v5 27JAN2006 negative&lt;BR /&gt;5 v1 03mar2007 positive&lt;BR /&gt;5 v2 30mar2007 positive&lt;BR /&gt;5 v3 06jun2007 positive&lt;BR /&gt;5 v4 13mar2008 positive&lt;BR /&gt;6 v1 03feb2007 positive&lt;BR /&gt;6 v2 30jun2007 UNKNOWN&lt;BR /&gt;6 v3 06nov2007&lt;BR /&gt;6 v4 03mar2008 positive&lt;BR /&gt;7 v1 01jan2007 positive&lt;BR /&gt;7 v2 30jun2007 positive&lt;BR /&gt;7 v3 08nov2007&lt;BR /&gt;7 v4 01feb2008 positive&lt;BR /&gt;8 v1 07aug2005 negative&lt;BR /&gt;8 v2 07oct2005 negative&lt;BR /&gt;8 v3 15nov2005 positive&lt;BR /&gt;8 v4 01jan2006&lt;BR /&gt;9 v1 07jan2005 negative&lt;BR /&gt;9 v2 10mar2005 positive&lt;BR /&gt;9 v3 27OCT2005 UNKNOWN&lt;BR /&gt;9 v4 07dec2005 negative&lt;BR /&gt;9 v5 20JAN2006 negative&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;diff=t1-t2&lt;BR /&gt;BASICALLY WE FOLLOW FIRST POSITIVE RESULT UNTIL 1.)THERE IS A NEGATIVE RESULT OR 2.) TILL THE LAST IF THEY NEVER BECOME NEGATIVES OR 3.) THE NEXT RESULT IS MISSING OR UNKNOWN&lt;BR /&gt;&lt;BR /&gt;1.) if an id has all negative results (and no positives) it has to be flagged (flag=1)(example: Id 4),&lt;BR /&gt;2.) if an id has all positive results (and no negatives) it has to be flagged (flag=2)(example: Id 5),&lt;BR /&gt;and diff= date of first positive minus date of last positive and changed_result=0&lt;BR /&gt;3.) if the results are like this-- 1- POSITIVE&lt;BR /&gt;2- POSITIVE&lt;BR /&gt;3- POSITIVE&lt;BR /&gt;4- (MISSING VALUE)&lt;BR /&gt;5- POSITIVE&lt;BR /&gt;then diff= dates of first positive and 3 rd positive need to be subtracted and changed_result=0&lt;BR /&gt;(similar situation when results are&lt;BR /&gt;1- POSITIVE&lt;BR /&gt;2- POSITIVE&lt;BR /&gt;3- (MISSING VALUE)&lt;BR /&gt;4- POSITIVE&lt;BR /&gt;then diff= dates of first positive and 2 rd positive need to be subtracted and changed_result=0&lt;BR /&gt;In other words, whenever resulTS are in (positive, missing) then diff=dates of first positive and the positive result right before the missing value&lt;BR /&gt;need to be subtracted and changed_result=0&lt;BR /&gt;4.) if the result are like this-- 1-negative&lt;BR /&gt;2-negative&lt;BR /&gt;3-positive&lt;BR /&gt;4-missing&lt;BR /&gt;then diff= missing value (cuz we have nothing to compare it to) and changed_result=0&lt;BR /&gt;In other words, if the second element while calculating diff is a missing value, then diff=missing and changed_result=0&lt;BR /&gt;5.)'unknown' values in result var should be treated same as missing values&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Number of visits may vary for each id and sometimes 'results' might be missing/unknown (treat both the same way).&lt;BR /&gt;For each unique ID:&lt;BR /&gt;6.) When there is 'positive' test result, we need to look for next consecutive 'negative' result and- we need to calculate the difference between the dates of the first positive result and the date of the first negative result after the first positive result and a new variable 'changed_result' is set to 1&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;In the output only one row should be there for each id:&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;expected output:&lt;/P&gt;&lt;P&gt;ID diff changed_result t1 t2 FLAG&lt;BR /&gt;1 155 1 01apr2006 03sep2006&lt;BR /&gt;2 404 1 02oct2005 10nov2006&lt;BR /&gt;3 . 0 . . (CANT' BE CALCULATED AS THERE IS A MISSING RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;4 . 0 . . 1 (ALL NEGATIVE RESULTS)&lt;BR /&gt;5 376 0 03mar2007 13mar2008 2 (ALL POSITIVE RESULTS)&lt;BR /&gt;6 . 0 . . (CANT' BE CALCULATED AS THERE IS A unknown RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;7 180 0 01jan2007 30jun2007 (this is how long were definitely positive)&lt;BR /&gt;8 . 0 . . (CANT' BE CALCULATED AS THERE IS A MISSING RESULT AFTER FIRST POSITIVE)&lt;BR /&gt;9 . 0 . . (CANT' BE CALCULATED AS THERE IS A unknown RESULT AFTER FIRST POSITIVE)&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I hope I can get some answers soon. I use BASE SAS V9&lt;BR /&gt;Thank you so much in advance for your help.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 01:11:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319422#M70149</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T01:11:08Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319479#M70179</link>
      <description>&lt;P&gt;Attached code contains 3 steps:&lt;/P&gt;
&lt;P&gt;1) reading input into dataset TEST&lt;/P&gt;
&lt;P&gt;2) the old algoritm - you may ignore it, if not need&lt;/P&gt;
&lt;P&gt;3) the new algorithm - producing the expected results from TEST&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 09:15:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319479#M70179</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-12-16T09:15:40Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319562#M70225</link>
      <description>&lt;P&gt;Thank you Shmuel. The code works but for id 7,&lt;/P&gt;&lt;P&gt;7 v1 01jan2007 positive&lt;BR /&gt;7 v2 30jun2007 positive&lt;BR /&gt;7 v3 08nov2007&lt;BR /&gt;7 v4 01feb2008 positive&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the t1,t2 values are incorrect. The diff value is calculated correctly.&lt;/P&gt;&lt;P&gt;output for your code for id7:&amp;nbsp;&lt;/P&gt;&lt;P&gt;id diff changed_result &amp;nbsp; &amp;nbsp; &amp;nbsp;t1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; flag&lt;/P&gt;&lt;P&gt;7 &amp;nbsp;180 &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;SPAN&gt;01feb2008 &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;30jun2007 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;when it should be :&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;id diff changed_result &amp;nbsp; &amp;nbsp; &amp;nbsp;t1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;t2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; flag&lt;/P&gt;&lt;P&gt;7 &amp;nbsp;180 &amp;nbsp;0 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN&gt;01jan2007&lt;/SPAN&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; 30jun2007 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; .&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 15:41:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319562#M70225</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T15:41:22Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319639#M70259</link>
      <description>&lt;P&gt;I have fixed the error with ID=7. Please check again.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data alg2;&lt;BR /&gt; format id diff changed_result t1 t2 flag;&lt;BR /&gt; keep id diff changed_result t1 t2 flag;&lt;BR /&gt; set test;&lt;BR /&gt; by id;&lt;BR /&gt; retain type ; /* 1=start negative, 2=start positive, 0=changed */&lt;BR /&gt; retain pos1d; /* 1st positive date */&lt;BR /&gt; retain flag; /* 1=all negative, 2=all positive, .=changed */&lt;BR /&gt; retain t2; /* date used to calc diff */&lt;BR /&gt; retain diff changed_result 0;&lt;BR /&gt; result = lowcase(result);&lt;BR /&gt; &lt;BR /&gt; if first.id then do; &lt;BR /&gt; diff=.;&lt;BR /&gt; if result = 'positive' then do; type=2; pos1d = t0; flag=2; end; else&lt;BR /&gt; if result = 'negative' then do; type=1; flag=1; end; &lt;BR /&gt; else do; type = 0; flag=.; end;&lt;BR /&gt; &lt;BR /&gt; end; else &lt;BR /&gt; &lt;BR /&gt; if result = 'positive' then do;&lt;BR /&gt; if type ne 2 /* 1st positive which is not 1st in ID */&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;and diff=. &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;/* &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; line added */&lt;/STRONG&gt;&lt;BR /&gt; then do; type=2; pos1d = t0; flag=.; end;&lt;BR /&gt; else if flag=2 then do;&lt;BR /&gt; diff=t0-pos1d; t2=t0;&lt;BR /&gt; end;&lt;BR /&gt; /* if type=2 no change need */&lt;BR /&gt; end; else&lt;BR /&gt; if result = 'negative' then do;&lt;BR /&gt; if type =2 and diff=. /* 1st negative following positive(s) */&lt;BR /&gt; then do; diff = t0 - pos1d; t2=t0; flag=.; changed_result=1; end; &lt;BR /&gt; /* if type is 1 or 0 no change need */&lt;BR /&gt; end; &lt;BR /&gt; else do;&lt;BR /&gt; if type =2 /* missing following positive(s) */&lt;BR /&gt; then do; &lt;STRONG&gt;***pos1d=.;&lt;/STRONG&gt;&amp;nbsp;t2=.; changed_result=0; flag=.; type=0; end; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;/* &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; line changed */&lt;/STRONG&gt;&lt;BR /&gt; end;&lt;BR /&gt; &lt;BR /&gt; if last.id then do;&lt;BR /&gt; if flag=1 then do; diff=.; t1=.; t2=.; output; end; else&lt;BR /&gt; if flag=2 then do; diff=t0 - pos1d; t1=pos1d; t2=t0; output; end; &lt;BR /&gt; else do; &lt;BR /&gt; if diff &amp;gt; 0 &lt;BR /&gt; then do; t1=pos1d; /* changed_result diff and t2 retained */ output; end;&lt;BR /&gt; else do; diff=.; t1=.; t2=.; output; end;&lt;BR /&gt; end;&lt;BR /&gt; end;&lt;BR /&gt; format t1 t2 date9.;&lt;BR /&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;STRONG&gt;Question:&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;what would you like to do in case of two periods with break in ID ?&lt;/P&gt;
&lt;P&gt;assume ID=7 has one more line:&lt;/P&gt;
&lt;P&gt;&amp;nbsp; &amp;nbsp;7 &amp;nbsp;v5 &amp;nbsp;20DEC2009 &lt;STRONG&gt;positive &amp;nbsp;&lt;/STRONG&gt; &lt;U&gt;&amp;lt;&amp;lt;&amp;lt; or &lt;STRONG&gt;negative&lt;/STRONG&gt; &amp;gt;&amp;gt;&amp;gt; those are two different cases to consider &amp;gt;&amp;gt;&amp;gt;&amp;gt;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;Would you prefer the first period? the last one? or the longest ?&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 20:37:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319639#M70259</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-12-16T20:37:46Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319654#M70261</link>
      <description>&lt;P&gt;Answer:&lt;/P&gt;&lt;P&gt;Step1: Need to see when they first become positive and note the date as t1&lt;/P&gt;&lt;P&gt;Step2: Follow the id until we encounter a negative or missing value or unknown , we do not care what ever happens after that. Even if there are multiple positive results or negative results after 1-positive 2-missing, we don't care for them as we encountered a missing value. We are calculating how long a person stays positive before encountering a missing/unknown/negative value.&lt;/P&gt;&lt;P&gt;Step3: if the person had a missing/unknown after first positive, we censor them there and diff=1st positive date minus date(missing/unknown) but changed_result=0;&lt;/P&gt;&lt;P&gt;Step4:&amp;nbsp;&lt;SPAN&gt;if the person had a negative&amp;nbsp;after first positive, we censor them there and diff=1st positive date minus date(first negative after 1st positive) but changed_result=1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;So, in the example you gave for ID 7, we don't care about v5 as after first positive we encountered a misisng at v3, so diff=01jan2007-08nov2007=311&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Your latest code working but I still need the t2 value to be printed instead of missing value there.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Thanks a lot!&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:02:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319654#M70261</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T21:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319657#M70262</link>
      <description>So to put it succinctly- the FIRST PERIOD&lt;BR /&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:03:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319657#M70262</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T21:03:12Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319660#M70264</link>
      <description>I am sorry- the diff=01jan2007-30jun2007 =180</description>
      <pubDate>Fri, 16 Dec 2016 21:09:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319660#M70264</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T21:09:29Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319667#M70267</link>
      <description>&lt;P&gt;Correction&amp;nbsp;on Step3:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Step3: if the person had a missing/unknown after first positive, we censor them there and diff=. &amp;nbsp;but changed_result=0;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;Example ID9&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:21:52 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319667#M70267</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T21:21:52Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319672#M70271</link>
      <description>&lt;P&gt;So all I want is t2 value to be printed for id7 , Right now it is missing value.&lt;/P&gt;&lt;P&gt;Thanks so much in advance Shmuel!&lt;/P&gt;</description>
      <pubDate>Fri, 16 Dec 2016 21:32:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319672#M70271</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-16T21:32:18Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319696#M70285</link>
      <description>&lt;P&gt;Sory, it was a typo in my last post. &amp;nbsp;See line changed:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;if result = 'negative' then do;&lt;BR /&gt; if type =2 and diff=. /* 1st negative following positive(s) */&lt;BR /&gt; then do; diff = t0 - pos1d; t2=t0; flag=.; changed_result=1; end; &lt;BR /&gt; /* if type is 1 or 0 no change need */&lt;BR /&gt; end; &lt;BR /&gt; else do;&lt;BR /&gt; if type =2 /* missing following positive(s) */&lt;BR /&gt; then do; ***pos1d=.; &lt;STRONG&gt;*t2=.;&lt;/STRONG&gt; changed_result=0; flag=.; type=0; end; &amp;nbsp; &lt;STRONG&gt;/* &amp;lt;&amp;lt;&amp;lt;&amp;lt; line changed */&lt;/STRONG&gt; &lt;BR /&gt; end;&lt;/P&gt;</description>
      <pubDate>Sat, 17 Dec 2016 02:02:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319696#M70285</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-12-17T02:02:30Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319836#M70353</link>
      <description>Thank you SHMUEL &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;BR /&gt;</description>
      <pubDate>Mon, 19 Dec 2016 04:07:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/319836#M70353</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-19T04:07:50Z</dc:date>
    </item>
    <item>
      <title>Re: UPDATE: How do I calculate a variable based on another variable value arranged in multiple rows</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/320792#M70704</link>
      <description>&lt;P&gt;Code is not working in this scenario (Please ADVISE)&lt;/P&gt;&lt;P&gt;10 v1 12SEP2005 positive&lt;BR /&gt;10 v2 27SEP2005 positive&lt;BR /&gt;10 v3 16MAR2006 negative&lt;BR /&gt;10 v4 13SEP2006 negative&lt;BR /&gt;10 v5 15FEB2007 negative&lt;BR /&gt;10 v6 06SEP2007 positive&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;WHAT I WANT IN OUTPUT :&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ID DIFF CHANGED_RESULT t1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; t2 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; flag&lt;/P&gt;&lt;P&gt;10 185 &amp;nbsp; 1 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;SPAN&gt;12SEP2005&amp;nbsp;&lt;/SPAN&gt;&amp;nbsp; &amp;nbsp;&lt;SPAN&gt;16MAR2006 &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;date of First positive minus date of first negative (we don't care about what the results are after this)&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;The code I am using:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data alg2;&lt;BR /&gt;format id diff changed_result t1 t2 flag;&lt;BR /&gt;keep id diff changed_result t1 t2 flag;&lt;BR /&gt;set test;&lt;BR /&gt;by id;&lt;BR /&gt;retain type ; /* 1=start negative, 2=start positive, 0=changed */&lt;BR /&gt;retain pos1d; /* 1st positive date */&lt;BR /&gt;retain flag; /* 1=all negative, 2=all positive, .=changed */&lt;BR /&gt;retain t2; /* date used to calc diff */&lt;BR /&gt;retain diff changed_result 0;&lt;BR /&gt;result = lowcase(result);&lt;/P&gt;&lt;P&gt;if first.id then do;&lt;BR /&gt;diff=.;&lt;BR /&gt;if result = 'positive' then do; type=2; pos1d = t0; flag=2; end; else&lt;BR /&gt;if result = 'negative' then do; type=1; flag=1; end;&lt;BR /&gt;else do; type = 0; flag=.; end;&lt;/P&gt;&lt;P&gt;end; else&lt;/P&gt;&lt;P&gt;if result = 'positive' then do;&lt;BR /&gt;if type ne 2 /* 1st positive which is not 1st in ID */&lt;BR /&gt;and diff=. /* &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; line added */&lt;BR /&gt;then do; type=2; pos1d = t0; flag=.; end;&lt;BR /&gt;else if flag=2 then do;&lt;BR /&gt;diff=t0-pos1d; t2=t0;&lt;BR /&gt;end;&lt;BR /&gt;/* if type=2 no change need */&lt;BR /&gt;end; else&lt;BR /&gt;if result = 'negative' then do;&lt;BR /&gt;if type =2 and diff=. /* 1st negative following positive(s) */&lt;BR /&gt;then do; diff = t0 - pos1d; t2=t0; flag=.; changed_result=1; end;&lt;BR /&gt;/* if type is 1 or 0 no change need */&lt;BR /&gt;end;&lt;BR /&gt;else do;&lt;BR /&gt;if type =2 /* missing following positive(s) */&lt;BR /&gt;then do; ***pos1d=.; *t2=.; changed_result=0; flag=.; type=0; end; /* &amp;lt;&amp;lt;&amp;lt;&amp;lt;&amp;lt; line changed */&lt;BR /&gt;end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This is the code I am running:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 22 Dec 2016 18:30:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-do-I-calculate-a-variable-based-on-another-variable-value/m-p/320792#M70704</guid>
      <dc:creator>ahhh</dc:creator>
      <dc:date>2016-12-22T18:30:56Z</dc:date>
    </item>
  </channel>
</rss>

