<?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: Compare a variable with a vector in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246179#M46041</link>
    <description>&lt;P&gt;If you make the LBOUND of the array 0 then you can use a "bodyless do".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data m;
   infile cards dsd;
   input m @@;
   cards;
10,20,30,40,50,60,70,100,120,130
;;;;
   run;
%let r0=%eval(&amp;amp;sysnobs-1);
proc transpose out=mwide prefix=m;
   var m;
   run;
proc print;
   run;
data scores;
   input Name $ Test_1 Test_2 Test_3;
   datalines;
Bill 187 97 103
Carlos 156 76 74
Monique 99 102 129
;
proc print;
   run;
data rank;
   if _n_ eq 1 then set mwide(keep=m:);
   array _m[0:&amp;amp;r0] m:;
   drop m:;
   set scores;
   do rank = lbound(_m) to hbound(_m) until(test_3 le _m[rank]); end;
   run;
proc print;
   run;
   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1621i4EF32E54E89E4762/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
    <pubDate>Tue, 26 Jan 2016 18:37:21 GMT</pubDate>
    <dc:creator>data_null__</dc:creator>
    <dc:date>2016-01-26T18:37:21Z</dc:date>
    <item>
      <title>Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246145#M46019</link>
      <description>&lt;P&gt;Hello,Everyone,&lt;/P&gt;
&lt;P&gt;I got a problem as follows,&lt;/P&gt;
&lt;P&gt;I have a vector with 10 values,say M= (10,20,30,40,50,60,70,100,120,130)&lt;/P&gt;
&lt;P&gt;I also have a dataset with several variables,I want to compare each observation in one of the variables with each value in the vector above and create a new rank variable-RANK&lt;/P&gt;
&lt;P&gt;For example, my dataset is&lt;/P&gt;
&lt;PRE&gt;data scores;
   &lt;SPAN class="codeFocus"&gt;input Name $ Test_1 Test_2 Test_3;&lt;/SPAN&gt;
   datalines;
Bill 187 97 103
Carlos 156 76 74
Monique 99 102 129
;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;I &amp;nbsp;want to compare Test_3 with M,for instance,&lt;/P&gt;
&lt;P&gt;103 is between 8th(100) and 9th(120),then RANK=8;&lt;/P&gt;
&lt;P&gt;74 is between 7th(70) and 8th(100),then RAND=7;&lt;/P&gt;
&lt;P&gt;129 is between 9th(120) and 10th(130),then RANK=9;&lt;/P&gt;
&lt;P&gt;For variable and vector are stored in different dataset,the length of the two are different,I don't know how to programming with it.&lt;/P&gt;
&lt;P&gt;Thank you very much!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:11:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246145#M46019</guid>
      <dc:creator>xxz3231</dc:creator>
      <dc:date>2016-01-26T17:11:33Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246147#M46021</link>
      <description>&lt;P&gt;One more thing, I don't have IML,is it possible to solve it by using the basic SAS programming techniques?&lt;/P&gt;
&lt;P&gt;Thank you very much!&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:15:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246147#M46021</guid>
      <dc:creator>xxz3231</dc:creator>
      <dc:date>2016-01-26T17:15:01Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246148#M46022</link>
      <description>&lt;P&gt;Yes you can solve in Base SAS, I suggest setting up a temporary array and storing the values there. If your values are in a data set you can also load the temporary array from the dataset.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The temporary array is retained across the dataset so you can compare for each row.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want;
set scores;

array M(10) _temporary_ (10,20,30,40,50,60,70,100,120,130);

rank=0;
do i=1 to 10 while (rank=0);
if m(i)&amp;gt; test_3 then rank=i-1;
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;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 17:20:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246148#M46022</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T17:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246150#M46024</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want(drop=i);
set have;
array M {10} _temporary_ (10,20,30,40,50,60,70,100,120,130);
do i = 1 to 10;
	if M{i} &amp;gt; test_3 then leave;
end;
rank=i-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:47:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246150#M46024</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-01-26T18:47:34Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246154#M46028</link>
      <description>Thank you so much for your answer!&lt;BR /&gt;I have another question: The vector is generated from other procedure and stored in a dateset,I want to use it directly in this data step instead of changing them by hand each time I run the code,can I do it?Thank you very much!</description>
      <pubDate>Tue, 26 Jan 2016 17:33:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246154#M46028</guid>
      <dc:creator>xxz3231</dc:creator>
      <dc:date>2016-01-26T17:33:28Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246155#M46029</link>
      <description>Thank you very much!</description>
      <pubDate>Tue, 26 Jan 2016 17:33:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246155#M46029</guid>
      <dc:creator>xxz3231</dc:creator>
      <dc:date>2016-01-26T17:33:55Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246166#M46036</link>
      <description>&lt;P&gt;Yes you can, as mentioned.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
   input Name $ Test_1 Test_2 Test_3;
   datalines;
Bill 187 97 103
Carlos 156 76 74
Monique 99 102 129
;

data M;
do i=10,20,30,40,50,60,70,100,120,130;
	output;
end;
run;



data want;
array M(10) _temporary_;
if _n_=1 then do j=1 to 10;
set M;
M(j)=i;
end;

set scores;

rank=0;
do i=1 to 10 while (rank=0);
if m(i)&amp;gt; test_3 then rank=i-1;
end;

drop i j;
run;
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:13:54 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246166#M46036</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T18:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246168#M46037</link>
      <description>&lt;P&gt;If you have a table with different vectors for each key value, then you'll need to attach them somehow, and load those values into an array. You can use whatever you'd like, but I prefer hashes.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data vector;
   input Name $ M_1 - M_10;
   datalines;
Bill 10 20 30 40 50 60 70 100 120 130
Carlos 10 20 30 40 50 60 70 100 120 130
Monique 10 20 30 40 50 60 70 100 120 130
;
run;

data want(drop= rc i m_1 - m_10);
if _n_ = 1 then do;
	if 0 then set vector;
	declare hash v (dataset:'vector');
	v.definekey('name');
	v.definedata(all:'y');
	v.definedone();
end;
call missing (of _all_);
set have;
rc=v.find();
array a {10} M_1 - M_10;
do i = 1 to 10;
	if a{i} &amp;gt; test_3 then leave;
end;
rank=i-1;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:20:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246168#M46037</guid>
      <dc:creator>DanZ</dc:creator>
      <dc:date>2016-01-26T18:20:14Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246174#M46038</link>
      <description>&lt;P&gt;So, you've already got two solutions (I'm referring to the two initial answers). However, your specifications were incomplete:&amp;nbsp;How should RANK be defined if&amp;nbsp;&lt;/P&gt;
&lt;OL&gt;
&lt;LI&gt;TEST_3 &amp;lt; M[1]?&lt;/LI&gt;
&lt;LI&gt;TEST_3 &amp;gt; M[10]?&lt;/LI&gt;
&lt;LI&gt;TEST_3 = M[i] for some&amp;nbsp;i?&lt;/LI&gt;
&lt;LI&gt;there are i ne j with M[i]=M[j]?&lt;/LI&gt;
&lt;/OL&gt;
&lt;P&gt;Not surprisingly,&amp;nbsp;the results of the two proposed algorithms differ in three&amp;nbsp;of the above special cases: 1, 2 and 4.&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:31:17 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246174#M46038</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2016-01-26T18:31:17Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246176#M46039</link>
      <description>&lt;P&gt;Always test edge and boundary conditions &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:34:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246176#M46039</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2016-01-26T18:34:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246179#M46041</link>
      <description>&lt;P&gt;If you make the LBOUND of the array 0 then you can use a "bodyless do".&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data m;
   infile cards dsd;
   input m @@;
   cards;
10,20,30,40,50,60,70,100,120,130
;;;;
   run;
%let r0=%eval(&amp;amp;sysnobs-1);
proc transpose out=mwide prefix=m;
   var m;
   run;
proc print;
   run;
data scores;
   input Name $ Test_1 Test_2 Test_3;
   datalines;
Bill 187 97 103
Carlos 156 76 74
Monique 99 102 129
;
proc print;
   run;
data rank;
   if _n_ eq 1 then set mwide(keep=m:);
   array _m[0:&amp;amp;r0] m:;
   drop m:;
   set scores;
   do rank = lbound(_m) to hbound(_m) until(test_3 le _m[rank]); end;
   run;
proc print;
   run;
   &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/1621i4EF32E54E89E4762/image-size/original?v=mpbl-1&amp;amp;px=-1" border="0" alt="Capture.PNG" title="Capture.PNG" /&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 26 Jan 2016 18:37:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246179#M46041</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-01-26T18:37:21Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246343#M46069</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data scores;
   input Name $ Test_1 Test_2 Test_3;
   datalines;
Bill 187 97 103
Carlos 156 76 74
Monique 99 102 129
;

data m;
	input m @@;
	cards;
	10	20 30 40 50 60 70 100 120 130
	;


%macro tt();
%do i=1 %to 10;
data _null_;
	set m;
	if _n_=&amp;amp;i. then	call symput("a&amp;amp;i.",m);
run;

%end;
proc format;
    value score  %do i=1 %to 9 ; %do j=2 %to 10; %if %eval(&amp;amp;j.-&amp;amp;i.)=1  %then &amp;amp;&amp;amp;a&amp;amp;i -&amp;lt; &amp;amp;&amp;amp;a&amp;amp;j = "&amp;amp;i." ; %end; %end; ;                     ; 
quit;



data scores;
	set scores;
	test_3_rank=put(test_3,score.);
run;

%mend;

%tt();


	&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 27 Jan 2016 14:36:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246343#M46069</guid>
      <dc:creator>hello_fj</dc:creator>
      <dc:date>2016-01-27T14:36:44Z</dc:date>
    </item>
    <item>
      <title>Re: Compare a variable with a vector</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246350#M46071</link>
      <description>You should look a the PROC FORMAT documentation regarding the CNTLIN parameter.  I allows you to create a format/informat using data from a data set.  Very useful and no need to involve macro language.</description>
      <pubDate>Wed, 27 Jan 2016 14:57:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Compare-a-variable-with-a-vector/m-p/246350#M46071</guid>
      <dc:creator>data_null__</dc:creator>
      <dc:date>2016-01-27T14:57:11Z</dc:date>
    </item>
  </channel>
</rss>

