<?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: Help with counting the number of record meeting condition from seperate file in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342821#M78616</link>
    <description>&lt;P&gt;It is really powerful that proc IML. I never do anything like that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really appreciate it, Ksharp.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 21 Mar 2017 02:34:36 GMT</pubDate>
    <dc:creator>hhchenfx</dc:creator>
    <dc:date>2017-03-21T02:34:36Z</dc:date>
    <item>
      <title>Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342724#M78583</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I want to count the number of records that meet&amp;nbsp;each pair of (variable and value) specified in a seperate file.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So for the 1st combination of a=4 and b=5, there are 2 record out of total 5 (with either a=4 or b=5).&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I write a code that get it done but that approach is defintely not efficient. It take too long.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I guess there should be a different way.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Could you please help me with that?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Thanks a lot,&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 9 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 9 0
6 4 5 9 2
7 6 0 9 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

*a sample for 1 combination of condition a=4 AND b=5 ;

	data BOTH; set have;
	if a=4 AND b=5 THEN DO;
		BOTH=1;OUTPUT;END;
	ELSE 
	if a=4 OR b=5 THEN DO;
		BOTH=0;OUTPUT; END;
	run;

	proc MEANS data=BOTH noprint;
	var both; 
	output out=N_same sum=N_same;
	run;
	
	data N_same; set N_same;
	drop _TYPE_;
	a_name="&amp;amp;a_name";
	b_name="&amp;amp;b_name";
	a_value=&amp;amp;a_value;
	b_value=&amp;amp;b_value;
	run;




*----This MAcro works but take too much time ------------------------------------------------------------------------;
data WANT; set xx; run;	*the marco below will build all final result into the WANT_CORR;

%MACRO correlation(a_name=,a_value=,b_name=,b_value=);
	data BOTH; set have;
	if &amp;amp;a_name=&amp;amp;a_value AND &amp;amp;b_name=&amp;amp;b_value THEN DO;
		BOTH=1;OUTPUT;END;
	ELSE 
	if &amp;amp;a_name=&amp;amp;a_value OR &amp;amp;b_name=&amp;amp;b_value THEN DO;
		BOTH=0;OUTPUT; END;
	run;

	proc MEANS data=BOTH noprint;
	var both; 
	output out=N_same sum=N_same;
	run;
	
	data N_same; set N_same;
	drop _TYPE_;
	a_name="&amp;amp;a_name";
	b_name="&amp;amp;b_name";
	a_value=&amp;amp;a_value;
	b_value=&amp;amp;b_value;
	run;

	data WANT; set WANT N_same; run;

%MEND;

	*Create combination of any 2 condition;
		data combination;									
		set have_var nobs=totalobs;
		drop i ;
		i+1;
		do j=i+1 to totalobs ;
			set have_var(keep = a_name a_value rename=(a_name=b_name a_value=b_value)) point=j;
			output;
		end;
		run;
	*Write code to temp file named "code";
		filename code temp;
		data _null_;
		  set combination end=last_cond;
		  file code;
			put '%correlation (a_name=' a_NAME ',a_value=' a_VALUE ', b_name=' b_NAME ',b_value=' b_VALUE ');'
		;run;
	*Run the temp file;
		%include code /source2;

&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 20 Mar 2017 19:02:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342724#M78583</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-20T19:02:28Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342733#M78587</link>
      <description>&lt;P&gt;Here's an approach that adds two variables to each observation:&amp;nbsp; n_matches (number of matched variables) and matching_list (names of variables that match).&amp;nbsp; You can extract, summarize, or do what you will with the results.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Assuming you have both data sets (HAVE and HAVE_VAR):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc sql noprint;&lt;/P&gt;
&lt;P&gt;select trim(a_name) into : varnames separated by ' ' from have_var;&lt;/P&gt;
&lt;P&gt;select a_value into : varvalues separated by ' ' from have_var;&lt;/P&gt;
&lt;P&gt;quit;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set have;&lt;/P&gt;
&lt;P&gt;array values {&amp;amp;sqlobs}&amp;nbsp;_temporary_ (&amp;amp;varvalues);&lt;/P&gt;
&lt;P&gt;array names {*} &amp;amp;varnames;&lt;/P&gt;
&lt;P&gt;n_matches=0;&lt;/P&gt;
&lt;P&gt;length matching_list $ 500;&lt;/P&gt;
&lt;P&gt;do _n_=1 to &amp;amp;sqlobs;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; if names{_n_} = values{_n_} then do;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; n_matches + 1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; matching_varlist = catx(',' , matching_varlist, vname(names{_n_}));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp; end;&lt;/P&gt;
&lt;P&gt;end;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;One of the keys to getting this to work is that the SELECT statements do not change the order of data retrieved from HAVE_VAR.&amp;nbsp; While one SELECT statement retrieves the variable names and the other retrieves the values, the order matches up within both macro variables.&amp;nbsp; Do not use DISTINCT or you will modify the order improperly.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The code is untested, but looks like it should work.&amp;nbsp; Let me know if you have any problems with it.&amp;nbsp; And examine the two added variables to make sure they give you the information you want.&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 19:32:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342733#M78587</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-20T19:32:50Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342791#M78602</link>
      <description>&lt;P&gt;You could use SQL to read have_var and build a set of statements like.&lt;/P&gt;
&lt;P&gt;&amp;nbsp; if a=4 and b=5 then N_a4_b5+1;&lt;BR /&gt;&amp;nbsp; if a=4 and c=9 then N_a4_c9+1;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If SQL writes those statements&amp;nbsp;to a macro var&amp;nbsp; (into &lt;EM&gt;&lt;STRONG&gt;:if_statements&lt;/STRONG&gt;&lt;/EM&gt; below), you could easily apply them to a data set reading HAVE and writiing one obs to WANT with the totals for each pairwise combination:&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 9 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 9 0
6 4 5 9 2
7 6 0 9 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;


proc sql noprint;
  select distinct  
    catx(' '
        ,'if'
        ,cats(L.a_name,'=',L.a_value)
        ,'and'
        ,cats(R.a_name,'=',R.a_value)
        ,'then'
        ,cats('N_',L.a_name,L.a_value,'_',R.a_name,R.a_value,'+1;')
        )
  into :if_statements separated by ' '
  from have_var as L inner join have_var as R
  on L.a_name&amp;lt;R.a_name;
quit;

data want (keep=N_:);
  set have end=end_of_have;
  &amp;amp;if_statements ;
  if end_of_have then output;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 20 Mar 2017 23:39:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342791#M78602</guid>
      <dc:creator>mkeintz</dc:creator>
      <dc:date>2017-03-20T23:39:49Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342806#M78607</link>
      <description>&lt;P&gt;Thank you for your responses.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am not sure if the code by Astounding generate the results, I find it difficult to read the “want” file.&lt;/P&gt;
&lt;P&gt;The code by Mkeintz give the results. Can you make it vertically with “a_name” “a_value” “b_name” “b_value”?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 01:45:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342806#M78607</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-21T01:45:51Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342812#M78610</link>
      <description>&lt;P&gt;If you have run my program, the intent is that you produce your own report. &amp;nbsp;For example, you could try:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;proc freq data=want;&lt;/P&gt;
&lt;P&gt;tables n_matches * matching_varlist / missing list;&lt;/P&gt;
&lt;P&gt;run;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can really slice and dice it however you want to see it.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 02:12:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342812#M78610</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-03-21T02:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342815#M78613</link>
      <description>&lt;PRE&gt;
Here is .Check the last column.




data have; 
input date a b c d;
datalines;
1 4 4 9 5
2 4 4 5 9
3 4 5 5 0
4 3 6 8 9
5 3 5 9 0
6 4 5 9 2
7 6 0 9 1
;run;

data have_var; 
input a_name $ a_value;
datalines;
a 4
b 5
c 9
d 9
;run;

proc iml;
use have(drop=date);
read all var _all_ into have[c=vnames];
close;

use have_var;
read all var{a_name a_value};
close;

n=nrow(a_name);
do i=1 to n-1;
 do j=i+1 to n;
   temp=have[,a_name[i]]||have[,a_name[j]];
   idx=loc(temp[,1]=a_value[i] | temp[,2]=a_value[j]);
   want=temp[idx,];
   corr=corr(want);
   var1=var1//a_name[i];
   var2=var2//a_name[j];
   cor=cor//corr[2];
   Total_N=Total_N//nrow(want);
   N_non_missing=N_non_missing//ncol(loc(countmiss(want,'row')=0));
   N_match=N_match//ncol(loc(temp[,1]=a_value[i] &amp;amp; temp[,2]=a_value[j]));
 end;
end;

create want var {var1 var2 cor Total_N N_non_missing N_match};
append;
close;
quit;

&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Mar 2017 02:22:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342815#M78613</guid>
      <dc:creator>Ksharp</dc:creator>
      <dc:date>2017-03-21T02:22:31Z</dc:date>
    </item>
    <item>
      <title>Re: Help with counting the number of record meeting condition from seperate file</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342821#M78616</link>
      <description>&lt;P&gt;It is really powerful that proc IML. I never do anything like that.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Really appreciate it, Ksharp.&lt;/P&gt;
&lt;P&gt;HHC&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Mar 2017 02:34:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Help-with-counting-the-number-of-record-meeting-condition-from/m-p/342821#M78616</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2017-03-21T02:34:36Z</dc:date>
    </item>
  </channel>
</rss>

