<?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: Improve Macro code that run line by line from a condition file. in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547521#M151734</link>
    <description>&lt;P&gt;Also not coding everything in lower case makes the code easier to read.&lt;/P&gt;
&lt;P&gt;My personal choice is to use lower case for the language and upper for user-defined names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sun, 31 Mar 2019 21:30:41 GMT</pubDate>
    <dc:creator>ChrisNZ</dc:creator>
    <dc:date>2019-03-31T21:30:41Z</dc:date>
    <item>
      <title>Improve Macro code that run line by line from a condition file.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547516#M151732</link>
      <description>&lt;P&gt;Hi Everyone,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My code below count number of record in file HAVE that meet each condition in file CONDITION.&lt;/P&gt;
&lt;P&gt;It works fine.&lt;/P&gt;
&lt;P&gt;Essentially, it create new file from a given line in Condition file, then turn ID and condition into macro variable and do the checking.&lt;/P&gt;
&lt;P&gt;Can you have a look and show me any way to improve it?&lt;/P&gt;
&lt;P&gt;Thank you,&lt;/P&gt;
&lt;P&gt;HHCFX&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 9
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; 
infile datalines dlm=' ' dsd;
informat id best.
		 condition $100. ;
input id condition $;
datalines;
1 "if a=4 and b=4"
2 "if a=4 and d=9"
;run;

*get number of record in codition file;
proc sql noprint; 
select count(*) into :nobs from condition; quit;

Data report_file; set _NULL_;run;

*Macro here;
%Macro counting();

	%Do i=1 %to &amp;amp;nobs;
	*get condition i;
	data cond_i; set condition; 
	if _N_=&amp;amp;i;run;

	*turn Id into macro variable;
	proc sql noprint; select id into :id from cond_i; quit;
	%put &amp;amp;id;

	*turn condition into macro variable;
	proc sql noprint; select condition into :condition from cond_i; quit;
	%put &amp;amp;condition;

		*get temporary file that meet condition i;
		data want;set have;
			id=&amp;amp;id;
			&amp;amp;condition ;
		run;

		proc means data=want noprint;
		by id;
		var id;
		output out=temp(keep= id _FREQ_) N=count ;run;

		*add to report file;
		data report_file; set report_file temp;run;
	%end;
%mend;

%counting();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;KEY&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;Data want;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;set have end=done;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if a=4 and b=4 then &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;cond_1+1&lt;/STRONG&gt;&lt;/FONT&gt;;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if a=4 and d=9 then cond_2+1;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;if done;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cond; set condition;
cond=catt(condition, ' then cond_',id,'+1;');
run;

proc sql noprint;
select cond into :condition_list separated by ' ' from cond;quit;
%put &amp;amp;condition_list;

Data want;set have end=done;
&amp;amp;condition_list;
if done;
run;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 01 Apr 2019 01:47:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547516#M151732</guid>
      <dc:creator>hhchenfx</dc:creator>
      <dc:date>2019-04-01T01:47:01Z</dc:date>
    </item>
    <item>
      <title>Re: Improve Macro code that run line by line from a condition file.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547520#M151733</link>
      <description>&lt;P&gt;No need for macros. 10 line of code can to this, and will run much faster too:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  set CONDITION nobs=NOBS end=LASTOBS;
  if _N_=1 then do ;
    call execute ('data ');
    do I=1 to NOBS;
      call execute (catt('COND_',I));
    end;
    call execute ('; set HAVE;');
  end;
  call execute (catt(CONDITION,' then output COND_',ID,';'));
  if LASTOBS then call execute ('run;');
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;FONT face="courier new,courier"&gt;NOTE: CALL EXECUTE generated line.&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;1 + data&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;2 + COND_1&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;3 + COND_2&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;4 + ; set HAVE;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;5 + if a=4 and b=4 then output COND_1;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;6 + if a=4 and d=9 then output COND_2;&lt;/FONT&gt;&lt;BR /&gt;&lt;FONT face="courier new,courier"&gt;7 + run;&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2019 21:29:20 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547520#M151733</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-03-31T21:29:20Z</dc:date>
    </item>
    <item>
      <title>Re: Improve Macro code that run line by line from a condition file.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547521#M151734</link>
      <description>&lt;P&gt;Also not coding everything in lower case makes the code easier to read.&lt;/P&gt;
&lt;P&gt;My personal choice is to use lower case for the language and upper for user-defined names.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 31 Mar 2019 21:30:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547521#M151734</guid>
      <dc:creator>ChrisNZ</dc:creator>
      <dc:date>2019-03-31T21:30:41Z</dc:date>
    </item>
    <item>
      <title>Re: Improve Macro code that run line by line from a condition file.</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547532#M151742</link>
      <description>Perhaps use a version of the suggested program that counts, without creating subsets.  Something like:&lt;BR /&gt;&lt;BR /&gt;Data want;&lt;BR /&gt;set have end=done;&lt;BR /&gt;if a=4 and b=4 then cond_1+1;&lt;BR /&gt;if a=4 and d=9 then cond_2+1;&lt;BR /&gt;if done;&lt;BR /&gt;run;</description>
      <pubDate>Sun, 31 Mar 2019 23:52:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Improve-Macro-code-that-run-line-by-line-from-a-condition-file/m-p/547532#M151742</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2019-03-31T23:52:03Z</dc:date>
    </item>
  </channel>
</rss>

