<?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 Counting the number of record meeting condition from seperate file (proc IML ?) in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346366#M79867</link>
    <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have: 1 data-file with variable (a b c d...) and 1 condition-file.&lt;/P&gt;
&lt;P&gt;Each row in the condition file contains certain condition (say a=1 and b=3 and c=5)&lt;/P&gt;
&lt;P&gt;I want to count the number of records in the data-file that meet each condition in condition-file&lt;/P&gt;
&lt;P&gt;So for the first condition:&amp;nbsp;a =4 &amp;nbsp; b =4 &amp;nbsp; c =5 , there are 2 record in data-file meet that criteria.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;and for the first condition: b =5 c =9 d =6 , there are 1&amp;nbsp;record in data-file meet that criteria.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to change a PROC IML &amp;nbsp;code but it will not work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help to fix the code or make new code is very much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 9 6
;run;

data condition; 
input id a_name $ a_value b_name $ b_value c_name $ c_value;
datalines;
1 a 4 b 4 c 5 
2 b 5 c 9 d 6
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close;

use condition;
read all var{a_name a_value b_name b_value c_name c_value};
close;

n=nrow(a_name);
do i=1 to n;
   temp=have[,a_name[i]]||have[,b_name[i]||have[,c_name[i]];
   idx=loc(temp[,1]=a_value[i] | temp[,2]=b_value[i] | temp[,3]=c_value[i]);
   want=temp[idx,];
   var1=var1//a_name[i];
   var2=var2//b_name[i];
   var3=var3//c_name[i];

   	value1=value1//a_value[i];
	value2=value2//b_value[i];
	value3=value3//c_value[i];

   Total_N=Total_N//nrow(want);
   N_match=N_match//ncol(loc(temp[,1]=a_value[i] &amp;amp; temp[,2]=b_value[i]  &amp;amp; temp[,3]=c_value[i]));
end;

create want var {var1 var2 var3 value1 value2 value3 Total_N N_match};
append;
close;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 01 Apr 2017 04:14:56 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2017-04-01T04:14:56Z</dc:date>
    <item>
      <title>Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346366#M79867</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have: 1 data-file with variable (a b c d...) and 1 condition-file.&lt;/P&gt;
&lt;P&gt;Each row in the condition file contains certain condition (say a=1 and b=3 and c=5)&lt;/P&gt;
&lt;P&gt;I want to count the number of records in the data-file that meet each condition in condition-file&lt;/P&gt;
&lt;P&gt;So for the first condition:&amp;nbsp;a =4 &amp;nbsp; b =4 &amp;nbsp; c =5 , there are 2 record in data-file meet that criteria.&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;and for the first condition: b =5 c =9 d =6 , there are 1&amp;nbsp;record in data-file meet that criteria.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I try to change a PROC IML &amp;nbsp;code but it will not work.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Any help to fix the code or make new code is very much appreciated.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 9 6
;run;

data condition; 
input id a_name $ a_value b_name $ b_value c_name $ c_value;
datalines;
1 a 4 b 4 c 5 
2 b 5 c 9 d 6
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close;

use condition;
read all var{a_name a_value b_name b_value c_name c_value};
close;

n=nrow(a_name);
do i=1 to n;
   temp=have[,a_name[i]]||have[,b_name[i]||have[,c_name[i]];
   idx=loc(temp[,1]=a_value[i] | temp[,2]=b_value[i] | temp[,3]=c_value[i]);
   want=temp[idx,];
   var1=var1//a_name[i];
   var2=var2//b_name[i];
   var3=var3//c_name[i];

   	value1=value1//a_value[i];
	value2=value2//b_value[i];
	value3=value3//c_value[i];

   Total_N=Total_N//nrow(want);
   N_match=N_match//ncol(loc(temp[,1]=a_value[i] &amp;amp; temp[,2]=b_value[i]  &amp;amp; temp[,3]=c_value[i]));
end;

create want var {var1 var2 var3 value1 value2 value3 Total_N N_match};
append;
close;
quit;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 04:14:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346366#M79867</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-04-01T04:14:56Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346368#M79869</link>
      <description>Can you explain question again, what exactly you are looking for?</description>
      <pubDate>Sat, 01 Apr 2017 04:06:14 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346368#M79869</guid>
      <dc:creator>lakshmi_74</dc:creator>
      <dc:date>2017-04-01T04:06:14Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346370#M79871</link>
      <description>&lt;P&gt;I just updated the post, I hope it is clearer now.&lt;/P&gt;
&lt;P&gt;Thank you.&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 04:15:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346370#M79871</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-04-01T04:15:31Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346381#M79872</link>
      <description>&lt;P&gt;As far as I can see your code is correct, though you were missing a ] along the way.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This code should work and I have commented where the error was.&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;data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 9 6
;run;

data condition; 
input id a_name $ a_value b_name $ b_value c_name $ c_value;
datalines;
1 a 4 b 4 c 5 
2 b 5 c 9 d 6
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close have;

print have;

use condition;
read all var{a_name a_value b_name b_value c_name c_value};
close condition;

n=nrow(a_name);
do i=1 to n;
   temp=have[,a_name[i]] || have[,b_name[i]] || have[,c_name[i]]; /* Your mistake was here :) */
   idx=loc( temp[,1] = a_value[i] | temp[,2] = b_value[i] | temp[,3] = c_value[i] );
   want=temp[idx,];

   var1=var1//a_name[i];
   var2=var2//b_name[i];
   var3=var3//c_name[i];

   value1=value1//a_value[i];
	value2=value2//b_value[i];
	value3=value3//c_value[i];

   Total_N=Total_N//nrow(want);
   N_match=N_match//ncol(loc(temp[,1]=a_value[i] &amp;amp; temp[,2]=b_value[i]  &amp;amp; temp[,3]=c_value[i]));
end;

create want var {var1 var2 var3 value1 value2 value3 Total_N N_match};
   append;
close want;

quit;&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The N_MATCH variable gives the desired values as below&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;IMG src="https://communities.sas.com/t5/image/serverpage/image-id/8098i0D394DEF57304EF8/image-size/medium?v=1.0&amp;amp;px=-1" border="0" alt="IML.PNG" title="IML.PNG" /&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 09:47:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346381#M79872</guid>
      <dc:creator>PeterClemmensen</dc:creator>
      <dc:date>2017-04-01T09:47:30Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346396#M79880</link>
      <description>&lt;PRE&gt;
HaHa. You nailed it. you really should learn some IML code . it is good for you.



data have; 
input date a b c d;
datalines;
1 4 4 5 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 0 0
6 4 5 1 2
7 6 5 9 6
;run;

data condition; 
input id a_name $ a_value b_name $ b_value c_name $ c_value;
datalines;
1 a 4 b 4 c 5 
2 b 5 c 9 d 6
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close;

use condition;
read all var{id a_name a_value b_name b_value c_name c_value};
close;

n=nrow(a_name);
count=j(n,1,.);
do i=1 to n;
   count[i]=sum(have[,a_name[i]]=a_value[i] &amp;amp; 
            have[,b_name[i]]=b_value[i]&amp;amp;
            have[,c_name[i]]=c_value[i] );
end;

create want var{id a_name a_value b_name b_value c_name c_value count};
append;
close;
quit;

&lt;/PRE&gt;</description>
      <pubDate>Sat, 01 Apr 2017 13:12:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346396#M79880</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-01T13:12:27Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346402#M79883</link>
      <description>&lt;P&gt;Oh, that is great.&lt;/P&gt;
&lt;P&gt;Thanks a lot.&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 14:38:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346402#M79883</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-04-01T14:38:58Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346403#M79884</link>
      <description>&lt;P&gt;Can you tell me what the meaning of have[c=vnames];&lt;/P&gt;
&lt;P&gt;I dont see you use "c" anywhere but when I change "c" into something else, code not work. But "vnames", I can change to anything.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;read all var _all_ into have[c=vnames];&lt;BR /&gt;&lt;BR /&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Sat, 01 Apr 2017 14:43:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346403#M79884</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-04-01T14:43:43Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346466#M79912</link>
      <description>&lt;P&gt;Yes. you have to specify "&lt;SPAN&gt;c=vnames" ,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;It doesn't matter what 'vnames' is, you can name it any variable name.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Specify it is for using the variable name reference .&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;like&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;have[,a_name[i]]&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;note here a_name[i] is 'a' not 1 ,&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;so&amp;nbsp;"c=vnames" is making sure you can refer to column of matrix as character 'a'. &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Default &amp;nbsp;index &amp;nbsp;is like 1,2,........&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;"c=vnames" is copying variable names into row vector 'vnames'. C here is keyword means column&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 02 Apr 2017 02:43:44 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346466#M79912</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-04-02T02:43:44Z</dc:date>
    </item>
    <item>
      <title>Re: Counting the number of record meeting condition from seperate file (proc IML ?)</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346867#M80028</link>
      <description>&lt;P&gt;Thanks&amp;nbsp;a lot.&lt;/P&gt;
&lt;P&gt;I will try &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;HC&lt;/P&gt;</description>
      <pubDate>Tue, 04 Apr 2017 00:16:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Counting-the-number-of-record-meeting-condition-from-seperate/m-p/346867#M80028</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-04-04T00:16:33Z</dc:date>
    </item>
  </channel>
</rss>

