<?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: Calculating best response - data step logic issue in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculating-best-response-data-step-logic-issue/m-p/499125#M132759</link>
    <description>&lt;P&gt;This is a Q&amp;amp;A board, I do not see a question here.&amp;nbsp; Seems like you have a reasonable spec there how are you going with it?&amp;nbsp; Or were you after a contractor to do some work for you?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Wed, 26 Sep 2018 15:03:47 GMT</pubDate>
    <dc:creator>RW9</dc:creator>
    <dc:date>2018-09-26T15:03:47Z</dc:date>
    <item>
      <title>Calculating best response - data step logic issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-best-response-data-step-logic-issue/m-p/499117#M132754</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Dear,&lt;/P&gt;
&lt;P&gt;I have the following sample data from which i need to calculate best response and date of confirmation(DOC). Please help. Thank you&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I calculate best response and DOC by following&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1. If the response is CR or PR at visits, then I calculate number of days between visits(use "date" variable values). The number of days ge 28 than best response is earlier visit response and &lt;SPAN&gt;earlier visit&amp;nbsp;&lt;/SPAN&gt;date as DOC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;eg: for id=1 number of days between visit1 and visit2 = 60(greater than 28) so best response="CR" and DOC=2017-01-01&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I need to calculate between all visits&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; for id=1 number of days between visit1 and visit3 = 90(greater than 28) so best response="CR" and date of confirmation=2017-01-01&lt;BR /&gt; for id=1 number of days between visit2 and visit3 = 30(greater than 28) so best response="CR" and date of confirmation=2017-03-01&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;at the end of datastep for id=1:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id bestresponse DOC&lt;BR /&gt;1 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-01-01&lt;BR /&gt;1 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-01-01&lt;BR /&gt;1 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-03-01&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;for id=2, number of days between visit1 and visit2 = 10(less than 28) so final response="SD" and date of confirmation=blank&lt;BR /&gt;for id=2, number of days between visit1 and visit3 = 40(greater than 28) so final response="CR" and date of confirmation=2017-05-20. So on for next visits&lt;/P&gt;
&lt;P&gt;id finalresponse DOC&lt;BR /&gt;2 SD &lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017-05-20&lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017-05-20 &lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-05-20&lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-05-30&lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 2017-05-30&lt;BR /&gt;2 CR&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;2017-07-01&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;for id=5, after "CR" there is NE , so i need to take difference between "randomdate" and "date"(not between two dates). if (2017-01-01-2016-10-30) greater than 42 than final response=SD, else NE. DOC is blank&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;id final response DOC &lt;BR /&gt;5 SD&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;for id=7, 1.first visit=CR and second visit=NE so i take difference between date and radte.if greater than 42 than final response=SD, else NE. DOC is blank. For this subject i need to ignore second visit and third vist as second visit is NE.&lt;BR /&gt;2. (first visit -third visit)the number of "NE" between two CR is one. if there is one NE then i need to take difference between first CR and second CR. if gt than 28 than CR, else SD&lt;/P&gt;
&lt;P&gt;id finalresponse DOC&lt;BR /&gt;7 SD &lt;BR /&gt;7 CR 2017-01-01&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;for id =8, 1.first visit=CR and second visit=NE so i take difference between date and radte.if greater than 42 than final response=SD, else NE. DOC is blank. For this subject i need to ignore second visit and third vist as second visit and third are NE.&lt;BR /&gt;2. (first visit -third visit)the number of "NE" between two CR response is two. if there is more than one NE then the final reponse= SD. I should not take difference between first and last CR&lt;/P&gt;
&lt;P&gt;id final response DOC&lt;/P&gt;
&lt;P&gt;8 SD&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;for ID=9, 1.1.first visit=PR and second visit=NE so i take difference between date and radte.if greater than 42 than final response=SD, else NE. &lt;BR /&gt;2. here more than one NE between two PR. When the response is PR, i take difference between first PR and last PR. If gt 28 han final reponse=PR. else SD&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;id finalresponse DOC&lt;BR /&gt;9 SD&lt;/P&gt;
&lt;P&gt;9 PR 2017-01-01&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For id =10&lt;/P&gt;
&lt;P&gt;id finalresponse DOC&lt;BR /&gt;10 PR 2017-01-01 (first and second visit)&lt;BR /&gt;10 PR 2017-01-01 (first and third visit)&lt;BR /&gt;10 PR 2017-01-01 (first and fourth visit)&lt;BR /&gt;10 PR 2017-01-01 (first and fifth visit)&lt;BR /&gt;10 PR 2017-03-01 (second and third visit)&lt;BR /&gt;10 PR 2017-03-01 (second and fourth visit)&lt;BR /&gt;10 PR 2017-03-01 (second and fifth visit)&lt;BR /&gt;10 PR 2017-04-01 (third and fourth visit)&lt;BR /&gt;10 PR 2017-04-01 (third and fifth visit)&lt;BR /&gt;10 CR 2017-05-20 (third and fifth visit)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The intermediate output dataset i calculated is below. The finalresponse values are formated as CR=1, PR=2, SD=3 and PD=5,NE=4. Then I sorted the dataset by&amp;nbsp; id finalresponse doc and took first,date to get output I need. My code working fine but lengthy and not getting the output i need when the new data is added.&lt;/P&gt;
&lt;P&gt;Intermediate dataset&lt;BR /&gt;id fianlresponse DOC&lt;BR /&gt;1 CR 2017-01-01&lt;BR /&gt;1 CR 2017-01-01&lt;BR /&gt;1 CR 2017-03-01&lt;BR /&gt;2 SD &lt;BR /&gt;2 CR 2017-05-20&lt;BR /&gt;2 CR 2017-05-20 &lt;BR /&gt;2 CR 2017-05-20&lt;BR /&gt;2 CR 2017-05-30&lt;BR /&gt;2 CR 2017-05-30&lt;BR /&gt;2 CR 2017-07-01 &lt;BR /&gt;5 SD&lt;BR /&gt;7 SD &lt;BR /&gt;7 CR 2017-01-01&lt;BR /&gt;8 SD &lt;BR /&gt;9 SD &lt;BR /&gt;9 PR 2017-01-01 &lt;BR /&gt;10 PR 2017-01-01 &lt;BR /&gt;10 PR 2017-01-01 &lt;BR /&gt;10 PR 2017-01-01 &lt;BR /&gt;10 PR 2017-01-01 &lt;BR /&gt;10 PR 2017-03-01 &lt;BR /&gt;10 PR 2017-03-01 &lt;BR /&gt;10 PR 2017-03-01 &lt;BR /&gt;10 PR 2017-04-01 &lt;BR /&gt;10 PR 2017-04-01 &lt;BR /&gt;10 CR 2017-05-20&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt; output i needed:&lt;/P&gt;
&lt;P&gt;id final response doc&lt;BR /&gt;1 CR 2017-01-01&lt;BR /&gt;2 CR 2017-05-20&lt;BR /&gt;5 SD&lt;BR /&gt;7 CR 2017-01-01 &lt;BR /&gt;8 SD&lt;BR /&gt;9 PR 2017-01-01&lt;BR /&gt;10 CR 2017-05-20&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc format;
value $aval
    'CR' = 1
    'PR' = 2
    'SD' = 3
    'PD' = 5
    'NE' = 4	
    ;
data one;
input id date$ 4-13 +1 response$ +1 randomdate $18-27;
datalines;
01 2017-01-01 CR 2016-10-30     
01 2017-03-01 CR 2016-10-30
01 2017-04-01 CR 2016-10-30      
02 2017-05-20 CR 2016-10-30
02 2017-05-30 CR 2016-10-30
02 2017-07-01 CR 2016-10-30
02 2017-08-30 CR 2016-10-30                             
05 2017-01-01 CR 2016-10-30
05 2017-01-30 NE 2016-10-30                             
07 2017-01-01 CR 2016-10-30
07 2017-01-30 NE 2016-10-30   
07 2017-02-21 CR 2016-10-30
08 2017-01-01 CR 2016-10-30
08 2017-01-30 NE 2016-10-30    
08 2017-02-30 NE 2016-10-30
08 2017-04-01 CR 2016-10-30
09 2017-01-01 PR 2016-10-30
09 2017-01-30 NE 2016-10-30    
09 2017-02-30 NE 2016-10-30
09 2017-04-01 PR 2016-10-30
10 2017-01-01 PR 2016-10-30     
10 2017-03-01 PR 2016-10-30
10 2017-04-01 PR 2016-10-30      
10 2017-05-20 CR 2016-10-30
10 2017-06-20 CR 2016-10-30
;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 14:47:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-best-response-data-step-logic-issue/m-p/499117#M132754</guid>
      <dc:creator>knveraraju91</dc:creator>
      <dc:date>2018-09-26T14:47:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculating best response - data step logic issue</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculating-best-response-data-step-logic-issue/m-p/499125#M132759</link>
      <description>&lt;P&gt;This is a Q&amp;amp;A board, I do not see a question here.&amp;nbsp; Seems like you have a reasonable spec there how are you going with it?&amp;nbsp; Or were you after a contractor to do some work for you?&amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Sep 2018 15:03:47 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculating-best-response-data-step-logic-issue/m-p/499125#M132759</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2018-09-26T15:03:47Z</dc:date>
    </item>
  </channel>
</rss>

