<?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 select value closest to X? in SAS Data Management</title>
    <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307549#M8791</link>
    <description>&lt;P&gt;Here is a simple way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closenessRanks;
input age rank;
datalines;
85 0
84 1
86 2
83 3
87 4
88 5
89 6
90 7
91 8
92 9
93 10
94 11
95 12
96 13
97 14
98 15
99 16
;

data have;
input id visit age score;
datalines;
1 1 80 30      
1 2 85 28
1 3 86 25
2 1 84 25 
2 2 87 24
3 1 90 28
3 2 95 28
;

proc sql;
create table want as
select a.*,
    case when rank=min(rank) then score else . end as newScore
from 
    have as a left join 
    closenessRanks as b on a.age=b.age
group by id
order by id, visit;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Thu, 27 Oct 2016 02:58:27 GMT</pubDate>
    <dc:creator>PGStats</dc:creator>
    <dc:date>2016-10-27T02:58:27Z</dc:date>
    <item>
      <title>How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307514#M8790</link>
      <description>&lt;P&gt;Hi,&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am working with a dataset that looks like this. Each participant was scored multiple times and age at exam was recorded.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;id visit age&amp;nbsp;score&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 1 80 30 &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 2 85 28&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 3 86 25&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2 1 84 25&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2 2 87 24&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3 1 90 28&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3 2 95 28&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I want to&amp;nbsp;create a new variable that reports the score (score_new) closest to age 85.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Particularly, I want the score at age 85. If there is no value for this, then I want the score at an age +/- 2 years, using&amp;nbsp;the score closest to age 85. If there is no value for this either, then I want the&amp;nbsp;score recorded after age 85, again using the score closest to 85. If there is a tie (e.g., a score at 84 and 86), then I want to keep the younger score. Remaining can be missing.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;So, I would like to end with a final data set like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;&lt;SPAN&gt;id visit age score score_new&lt;/SPAN&gt;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 1 80 30 . &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 2 85 28 28&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;1 3 86 25 .&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2 1 84 25 25&amp;nbsp;&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;2 2 86 24 .&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3 1 90 28 28&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&lt;FONT face="courier new,courier"&gt;3 2 95 28 .&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any help would be appreciated.&amp;nbsp;Happy to explain further. I am new to arrays, so replies with detail&amp;nbsp;would be&amp;nbsp;best.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Looking forward to your feedback and solving this!&lt;/P&gt;&lt;P&gt;Thanks.&lt;/P&gt;</description>
      <pubDate>Wed, 26 Oct 2016 22:21:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307514#M8790</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2016-10-26T22:21:23Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307549#M8791</link>
      <description>&lt;P&gt;Here is a simple way:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closenessRanks;
input age rank;
datalines;
85 0
84 1
86 2
83 3
87 4
88 5
89 6
90 7
91 8
92 9
93 10
94 11
95 12
96 13
97 14
98 15
99 16
;

data have;
input id visit age score;
datalines;
1 1 80 30      
1 2 85 28
1 3 86 25
2 1 84 25 
2 2 87 24
3 1 90 28
3 2 95 28
;

proc sql;
create table want as
select a.*,
    case when rank=min(rank) then score else . end as newScore
from 
    have as a left join 
    closenessRanks as b on a.age=b.age
group by id
order by id, visit;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Oct 2016 02:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307549#M8791</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-27T02:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307551#M8792</link>
      <description>&lt;P&gt;Maybe I understood what you are talking about.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data have;
input id visit age score;
cards;
1 1 80 30      
1 2 85 28
1 3 86 25
2 1 84 25 
2 2 87 24
3 1 90 28
3 2 95 28
;
run;
proc sql;
create table temp1 as
 select *
  from have
   group by id
    having sum(age=85)=0 and age in (83 84 86 87)
     order by id,age;
create table temp2 as
 select *
  from have
   group by id
    having sum(age in (83 84 85 86 87))=0 and age gt 87
     order by id,age;
quit;
data key1;
 set temp1;
 by id;
 if first.id;
run;
data key2;
 set temp2;
 by id;
 if first.id;
run;
data key;
 set have(where=(age=85)) key1 key2;
 keep id age score;
 rename score=new_score;
run;

data want;
 merge have key;
 by id age;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Oct 2016 03:10:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307551#M8792</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2016-10-27T03:10:07Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307556#M8793</link>
      <description>&lt;P&gt;Thank you so much! This works, but there's one small&amp;nbsp;issue to fix. For individuals where none of the scores match one of the ranked ages, the original score is being used as&amp;nbsp;newscore. Instead, I would like these to be missing. How can I change this? Thank you.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 04:22:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307556#M8793</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2016-10-27T04:22:03Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307557#M8794</link>
      <description>&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 04:12:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307557#M8794</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2016-10-27T04:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307559#M8795</link>
      <description>&lt;P&gt;Ok. Try&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc sql;
create table want as
select a.*,
    case when coalesce(rank, -1)=min(rank) then score else . end as newScore
from 
    have as a left join 
    closenessRanks as b on a.age=b.age
group by id
order by id, visit;
select * from want;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;That should fix it.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Oct 2016 04:35:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307559#M8795</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-10-27T04:35:22Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307560#M8796</link>
      <description>Yes, it does! Thank you.</description>
      <pubDate>Thu, 27 Oct 2016 04:39:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/307560#M8796</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2016-10-27T04:39:50Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/313215#M8965</link>
      <description>&lt;P&gt;Hi PG,&amp;nbsp;&lt;/P&gt;&lt;P&gt;I found one case when this doesn't work -- when the first visit is missing, and there are values for subsequent visits. Code ends up not pulling any value for new score, when it should. Can't figure out how to change this. Would appreciate a solution for this. Thank you in advance!&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 21 Nov 2016 19:27:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/313215#M8965</guid>
      <dc:creator>pamplemouse22</dc:creator>
      <dc:date>2016-11-21T19:27:34Z</dc:date>
    </item>
    <item>
      <title>Re: How do I select value closest to X?</title>
      <link>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/313236#M8966</link>
      <description>&lt;P&gt;The problem could be with the way you read your data. Compare the results with and without the &lt;STRONG&gt;infile&lt;/STRONG&gt; statement in&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data closenessRanks;
input age rank;
datalines;
85 0
84 1
86 2
83 3
87 4
88 5
89 6
90 7
91 8
92 9
93 10
94 11
95 12
96 13
97 14
98 15
99 16
;

data have;
infile datalines missover;
input id visit age score;
datalines;
1 1 80 30      
1 2 85 28
1 3 86 25
2 1 84 25 
2 2 87 24
3 1 90 28
3 2 95 28
4 1 79 88
4 2 80 99
5 1 80 
5 2 85 28
5 3 86 25
;

proc sql;
create table want as
select a.*,
    case when coalesce(rank, -1)=min(rank) then score else . end as newScore
from 
    have as a left join 
    closenessRanks as b on a.age=b.age
group by id
order by id, visit;
select * from want;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 21 Nov 2016 20:20:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Data-Management/How-do-I-select-value-closest-to-X/m-p/313236#M8966</guid>
      <dc:creator>PGStats</dc:creator>
      <dc:date>2016-11-21T20:20:05Z</dc:date>
    </item>
  </channel>
</rss>

