<?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 choose a value closest to a specific value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494449#M130291</link>
    <description>&lt;P&gt;I need a bit more information than this.. Does this mean you want 7 different data sets? One for each value of&amp;nbsp;&lt;SPAN&gt;12, 26, 52, 104, 156, 208 and 260?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 11 Sep 2018 11:43:31 GMT</pubDate>
    <dc:creator>PeterClemmensen</dc:creator>
    <dc:date>2018-09-11T11:43:31Z</dc:date>
    <item>
      <title>how to choose a value closest to a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494442#M130287</link>
      <description>&lt;P&gt;I am not that advanced in SAS programming and have currently run into some problems regarding how to call a specific value closest to a given value. The problem consits of two "subproblems"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;We are looking at&amp;nbsp;outcomes after a time period of 12, 26, 52, 104, 156, 208 and 260 weeks, we have acceptet an interval of +/-&amp;nbsp;4&amp;nbsp;weeks&amp;nbsp;for the time points as the visit date is not always the exact date corresponding to the time period. This give rise to the first problem that some patients might&amp;nbsp;multiple visits in the time frame.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here we want the datapoint closest to the time point. E.g. in the example provided for ID = 1, visit 2 (8 weeks), visit 3 (12 weeks), and visit 4 (14 weeks) all satisfy the condition of 12 weeks +/- 4 weeks. The one we would like to be our 12 weeks visit should be visit 3.&amp;nbsp;&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D&gt;7&lt;/TD&gt;&lt;TD&gt;31&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;1&lt;/TD&gt;&lt;TD&gt;0&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;12&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;10&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;TD&gt;.&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;14&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;TD&gt;5&lt;/TD&gt;&lt;/TR&gt;&lt;TR&gt;&lt;TD&gt;2&lt;/TD&gt;&lt;TD&gt;4&lt;/TD&gt;&lt;TD&gt;26&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;TD&gt;3&lt;/TD&gt;&lt;/TR&gt;&lt;/TBODY&gt;&lt;/TABLE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Now the second problem is that we are looking at two different outcomes: score1 and score2. For some visits score1 and score2 is not recorded, in this scenario, if multiple visits are in the time period, we want to call the visit which have both scores recorded. and if only one then score1&amp;gt;score2. E.g. in &lt;SPAN&gt;the example provided for ID = 2 we want visit 3 to be the one called.&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I can code this with lead and lag functions (atleast the first part), however this is very time consuming and syntax heavy. Is there a smartere way to accomplish this?&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 11:10:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494442#M130287</guid>
      <dc:creator>portneren</dc:creator>
      <dc:date>2018-09-11T11:10:40Z</dc:date>
    </item>
    <item>
      <title>Re: how to choose a value closest to a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494449#M130291</link>
      <description>&lt;P&gt;I need a bit more information than this.. Does this mean you want 7 different data sets? One for each value of&amp;nbsp;&lt;SPAN&gt;12, 26, 52, 104, 156, 208 and 260?&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 11:43:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494449#M130291</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2018-09-11T11:43:31Z</dc:date>
    </item>
    <item>
      <title>Re: how to choose a value closest to a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494452#M130294</link>
      <description>&lt;P&gt;I would start by finding the records in the correct intervals and giving them a rank by scores available and a calculated time difference:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data ordered;
  set have;
  do scheduled=12, 26, 52, 104, 156, 208,260;
    if scheduled-4&amp;lt;=Duration_weeks&amp;lt;=scheduled+4 then do;
      rank=missing(score1)*2+missing(score2); /* Score1 has higher priority, high rank is bad */
      diff=abs(scheduled-Duration_weeks);
      output;
      end;
    end;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Then, it is very simple:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sort;
  by id scheduled rank diff;
run;

data want;
  set ordered;
  by id scheduled rank diff;
  if first.scheduled;
run;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;If I read your specification correctly, you want to prioritize RANK over DIFF. If not, switch the two in the BY statements.&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 11:53:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494452#M130294</guid>
      <dc:creator>s_lassen</dc:creator>
      <dc:date>2018-09-11T11:53:24Z</dc:date>
    </item>
    <item>
      <title>Re: how to choose a value closest to a specific value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494470#M130310</link>
      <description>&lt;P&gt;Thank you, this is exactly what I needed - it works perfect.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 11 Sep 2018 12:50:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/how-to-choose-a-value-closest-to-a-specific-value/m-p/494470#M130310</guid>
      <dc:creator>portneren</dc:creator>
      <dc:date>2018-09-11T12:50:50Z</dc:date>
    </item>
  </channel>
</rss>

