<?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: select variable that has the closest value in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303567#M64507</link>
    <description>&lt;P&gt;Thank you! With a few tweaks (added "end" to close the if loop, added "of" within the min function), this code worked to create variablewant.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want ;&lt;BR /&gt;set have ;&lt;/P&gt;&lt;P&gt;array aggx Agg_60 Agg_65 Agg_70 ;&lt;BR /&gt;array disx dis1-dis3;&lt;BR /&gt;array sitx $ Site_60 Site_65 Site_70 ;&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(aggx);&lt;BR /&gt;disx(i) = abs( aggx(i) - 0.25);&lt;BR /&gt;end;&lt;BR /&gt;min_agg = min(of dis: );&lt;BR /&gt;do i=1 to dim(aggx);&lt;BR /&gt;if disx(i) = min_agg then do;&lt;BR /&gt;variablewant = sitx(i);&lt;BR /&gt;call symput('aggSite' , variablewant); /* to be used on next steps */&lt;BR /&gt;leave;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;proc print ; run;&lt;/P&gt;</description>
    <pubDate>Mon, 10 Oct 2016 15:11:39 GMT</pubDate>
    <dc:creator>CP2</dc:creator>
    <dc:date>2016-10-10T15:11:39Z</dc:date>
    <item>
      <title>select variable that has the closest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303210#M64370</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;I created three variables based on how certain observations represent the total and based on percentile criteria. I need to choose which of these variables has the closest value to 25% and then be able to use another variable that is associated with that variable. &amp;nbsp;All observations are the same within each of these variables just because of the way I generated the results into the dataset (vs using macro variables).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The variable names represent the percentile criteria used. I need to choose which of the "AGG_" variables is closest to 25% so that I can use the associated "SITE_" variable which also uses the percentile in the variable name. &amp;nbsp;A picture of the resulting dataset is attached which shows what I am doing. Also, ideally, the percentiles will change so that I would prefer to not have to hardcode the variables in (as in the PROC SQL at the end), so any advice on how to use macro variables would be helpful .&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Small Sample Code:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data have ;&lt;BR /&gt;infile datalines dsd missover ;&lt;BR /&gt;input SiteID totalpt diagpts Totalpts DiagPts diagsite_pct acaSite commSite privSite diagmd_pct ;&lt;BR /&gt;datalines;&lt;BR /&gt;2958,137,3,42,1,0.02189781,,,137,0.023809524&lt;BR /&gt;2965,1366,13,104,1,0.009516837,,,1366,0.009615385&lt;BR /&gt;2999,100,2,43,2,0.02,,,100,0.046511628&lt;BR /&gt;3075,300,6,153,6,0.02,,,300,0.039215686&lt;BR /&gt;3101,429,10,95,3,0.023310023,,,429,0.031578947&lt;BR /&gt;3171,189,1,5,1,0.005291005,,,189,0.2&lt;BR /&gt;3279,37,2,2,1,0.054054054,,37,,0.5&lt;BR /&gt;3285,484,5,88,2,0.010330579,,,484,0.022727273&lt;BR /&gt;3307,2908,31,119,1,0.010660248,,,2908,0.008403361&lt;BR /&gt;3400,491,7,127,5,0.014256619,,,491,0.039370079&lt;BR /&gt;3434,1047,37,7,4,0.035339064,1047,,,0.571428571&lt;BR /&gt;3438,244,7,132,5,0.028688525,,244,,0.037878788&lt;BR /&gt;3450,2486,61,192,3,0.024537409,,,2486,0.015625&lt;BR /&gt;3493,434,9,66,1,0.020737327,,434,,0.015151515&lt;BR /&gt;3496,194,2,29,2,0.010309278,,,194,0.068965517&lt;BR /&gt;3530,77,23,25,9,0.298701299,77,,,0.36&lt;BR /&gt;3537,113,2,1,1,0.017699115,113,,,1&lt;BR /&gt;3547,470,6,42,1,0.012765957,,470,,0.023809524&lt;BR /&gt;3551,140,2,140,2,0.014285714,,140,,0.014285714&lt;BR /&gt;3638,914,83,54,23,0.090809628,914,,,0.425925926&lt;BR /&gt;;&lt;BR /&gt;run;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%let class=25 ; /*percentile for practice class type is set at 25 for now*/&lt;BR /&gt;proc univariate data=have noprint;&lt;BR /&gt;var acasite commsite privsite diagsite_pct diagsite_pct; /*diagsite_pct is duplicated to create the dummy site_ variables*/&lt;BR /&gt;output out=pctOutput pctlpre=aca_ comm_ priv_ diag_ site_ pctlpts=&amp;amp;class., 60 to 75 by 5;&lt;BR /&gt;run;&lt;BR /&gt;&lt;SPAN&gt;proc print; run;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;data have (drop=i);&lt;BR /&gt;if _N_ = 1 then set pctoutput ; /*add the percentiles row generated from proc univariate to lookup table*/&lt;BR /&gt;set have ;&lt;/P&gt;&lt;P&gt;/*identify practices where number of patients with diagnosis is greater than or equal to various percentiles from proc univariate*/&lt;BR /&gt;array diag{*} diag_: ;&lt;BR /&gt;array site{*} site_: ;&lt;/P&gt;&lt;P&gt;do i = 1 to dim(diag) ;&lt;BR /&gt;Site[i]=. ;&lt;BR /&gt;if diagsite_PCT &amp;gt;= diag[i] and ( acaSite &amp;gt;= 0 or commSite &amp;gt;= comm_25 or privSite &amp;gt;= priv_25 ) then Site[i] = 1 ;&lt;BR /&gt;end ;&lt;/P&gt;&lt;P&gt;run;&lt;BR /&gt;proc print; run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/*Determine if percentile representativeness is around 25% of total diagnosis sites */&lt;/P&gt;&lt;P&gt;/*NOTE: I prefer to be able to use macrovariables rather than hardcode just in case we decide to choose different percentiles later*/&lt;BR /&gt;proc sql ;&lt;BR /&gt;create table haveAgg as&lt;BR /&gt;Select SiteID, Site_60, Site_65, Site_70, sum(Site_60)/count(diagsite_pct) as Agg_60,&lt;BR /&gt;sum(Site_65)/count(diagsite_pct) as Agg_65,sum(Site_70)/count(diagsite_pct) as Agg_70&lt;BR /&gt;from have&lt;BR /&gt;Quit ;&lt;BR /&gt;proc print ; run;&lt;/P&gt;&lt;P&gt;/* Choose percentile column where diagnosis representativeness is closest to 25% */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Not sure how to do this because in this example the Agg_70 variable is closest to 25% so in subsequent&amp;nbsp;code&amp;nbsp;I will need to continue with this project I will need to use the SITE_70 variable. The Agg_70 is the representation of SITE_70, Agg_65 is representing the SITE_65, etc.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;psuedo code that I want:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;distance_n = abs(AGG_n - .25) &amp;nbsp;;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;if min(distance_n) then variablewant = SITE_n ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If Site_n = 1 and diagsite_pct &amp;lt; .05 then flag=1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The gist of my problem is if AGG_70 is closest to 25% then how do I refer to the associated SITE_ variable using the suffix?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Suggestions greatly appreciated!!&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/13038iEAFC89E3B6B8BCEF/image-size/large?v=1.0&amp;amp;px=600" border="0" alt="sample specialty.PNG" title="sample specialty.PNG" /&gt;</description>
      <pubDate>Fri, 07 Oct 2016 16:30:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303210#M64370</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2016-10-07T16:30:07Z</dc:date>
    </item>
    <item>
      <title>Re: select variable that has the closest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303346#M64423</link>
      <description>&lt;P&gt;You may try:&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 heveAgg;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array aggx Agg_60 Agg_65 Agg70;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array disx &amp;nbsp; dis1-dis3;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; array sitx $ 'Site_60' 'Site_65' 'Site_70' ;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; do i=1 to dim(aggx);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;disx(i) = abs( aggx(i) - 0.25);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; min_agg = min(dis: );&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; do i=1 to dim(aggx);&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; if disx(i) = min_agg then do;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;SPAN&gt;&lt;STRONG&gt;variablewant&lt;/STRONG&gt; = sitx(i);&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;call symput('&lt;STRONG&gt;aggSite&lt;/STRONG&gt;' , variablewant); &amp;nbsp; /* to be used on next steps */&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;STRONG&gt;leave&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; end;&lt;/P&gt;&lt;P&gt;&amp;nbsp;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;then next step should be in a macro:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro name_it;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%global &lt;STRONG&gt;aggSite&lt;/STRONG&gt;;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.... your code using &amp;amp;aggSite ....&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;%name_it;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Oct 2016 06:17:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303346#M64423</guid>
      <dc:creator>Shmuel</dc:creator>
      <dc:date>2016-10-08T06:17:22Z</dc:date>
    </item>
    <item>
      <title>Re: select variable that has the closest value</title>
      <link>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303567#M64507</link>
      <description>&lt;P&gt;Thank you! With a few tweaks (added "end" to close the if loop, added "of" within the min function), this code worked to create variablewant.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data want ;&lt;BR /&gt;set have ;&lt;/P&gt;&lt;P&gt;array aggx Agg_60 Agg_65 Agg_70 ;&lt;BR /&gt;array disx dis1-dis3;&lt;BR /&gt;array sitx $ Site_60 Site_65 Site_70 ;&lt;BR /&gt;&lt;BR /&gt;do i=1 to dim(aggx);&lt;BR /&gt;disx(i) = abs( aggx(i) - 0.25);&lt;BR /&gt;end;&lt;BR /&gt;min_agg = min(of dis: );&lt;BR /&gt;do i=1 to dim(aggx);&lt;BR /&gt;if disx(i) = min_agg then do;&lt;BR /&gt;variablewant = sitx(i);&lt;BR /&gt;call symput('aggSite' , variablewant); /* to be used on next steps */&lt;BR /&gt;leave;&lt;BR /&gt;end;&lt;BR /&gt;end;&lt;BR /&gt;run;&lt;BR /&gt;proc print ; run;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Oct 2016 15:11:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/select-variable-that-has-the-closest-value/m-p/303567#M64507</guid>
      <dc:creator>CP2</dc:creator>
      <dc:date>2016-10-10T15:11:39Z</dc:date>
    </item>
  </channel>
</rss>

