<?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: Using an array efficantly with a macro in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611460#M18200</link>
    <description>&lt;P&gt;I don't know why a macro runs slowly here, but a macro simply is not needed and is an extra complication that provides no benefit. Oh, I should add that macros are not inherently more efficient than non-macro code, so your reason for going to macro code doesn't hold water.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, I don't really know what you intended macro %FREQS to accomplish, but you need to go back to the beginning and create SAS code that works for one or two situations without macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see nothing that requires macros here anyway, arrays ought to work properly, stick to arrays here.&lt;/P&gt;</description>
    <pubDate>Thu, 12 Dec 2019 22:48:58 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-12-12T22:48:58Z</dc:date>
    <item>
      <title>Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611457#M18199</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I used PROC MEANS to sum flags by three variables (ORSID RECORDID LOG).&amp;nbsp; I now want to create new variables that retain the sums for each flag, then set each flag back to 0 or 1.&amp;nbsp; I thought it would make my program more efficient if I wrote a macro program, then used an array to run it for each flag variable as below.&amp;nbsp; The program works...… VERY SLOWLY.&amp;nbsp; Is there a better way to do this in terms of programming efficiency?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=cohort_claims_1218 sum noprint idmin;
	var claimflag--fp_emergency sti:;
	by orsid recordid log;
	id date;
	output out=flagfreqs sum=;
run;

%macro freqs(var);
	if &amp;amp;var ge 1 then &amp;amp;var._freq = &amp;amp;var;
%mend;
data test;
	set flagfreqs;
	by orsid recordid log;
	array FREQS claimflag--STI_Total;
	do i = 1 to dim(FREQS);
		%freqs(i);
		if FREQS{i} ge 1 then FREQS{i} = 1;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 12 Dec 2019 22:04:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611457#M18199</guid>
      <dc:creator>LEINAARE</dc:creator>
      <dc:date>2019-12-12T22:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611460#M18200</link>
      <description>&lt;P&gt;I don't know why a macro runs slowly here, but a macro simply is not needed and is an extra complication that provides no benefit. Oh, I should add that macros are not inherently more efficient than non-macro code, so your reason for going to macro code doesn't hold water.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Now, I don't really know what you intended macro %FREQS to accomplish, but you need to go back to the beginning and create SAS code that works for one or two situations without macros.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I see nothing that requires macros here anyway, arrays ought to work properly, stick to arrays here.&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 22:48:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611460#M18200</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-12T22:48:58Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611465#M18201</link>
      <description>&lt;P&gt;The %freqs macro seems designed to take a variable name, but instead takes an index, i?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token keyword"&gt;if&lt;/SPAN&gt; 1 &lt;SPAN class="token operator"&gt;ge&lt;/SPAN&gt; &lt;SPAN class="token number"&gt;1&lt;/SPAN&gt; &lt;SPAN class="token keyword"&gt;then&lt;/SPAN&gt; 1_freq &lt;SPAN class="token operator"&gt;=&lt;/SPAN&gt; 1&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;ie if i=1 this resolves to the following&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Is that what you intended?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Fully worked examples of common macro usage&lt;BR /&gt;&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&amp;nbsp;" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&amp;nbsp;&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 12 Dec 2019 23:17:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611465#M18201</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-12-12T23:17:06Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611528#M18212</link>
      <description>&lt;P&gt;Your macro resolves to this code, once(!):&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;if i ge 1 then i_freq = i;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;which is then executed dim(freqs) times, so variable i_freq will always end up holding the value of dim(freqs) (the final value of i during the loop).&lt;/P&gt;
&lt;P&gt;I seriously doubt that this is what you want.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 07:41:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611528#M18212</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T07:41:15Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611552#M18218</link>
      <description>&lt;P&gt;I see.&amp;nbsp; Thank you for clarifying that.&amp;nbsp; I am trying to devise a way to prevent redundant lines of code.&amp;nbsp; I could code as below, but it would account for many lines.&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data test;
	set flagfreqs;
	by orsid recordid log;
	if Var1 ge 1 then Var1_freq = Var1;
	if Var2 ge 1 then Var2_freq = Var2;
	if Var3 ge 1 then Var3_freq = Var3;
	if Var4 ge 1 then Var4_freq = Var4;
	if Var5 ge 1 then Var5_freq = Var5;
	if Var6 ge 1 then Var6_freq = Var6;
	if Var7 ge 1 then Var7_freq = Var7;
	if Var8 ge 1 then Var8_freq = Var8;
	if Var9 ge 1 then Var9_freq = Var9;
	if Var10 ge 1 then Var10_freq = Var10;
	if Var11 ge 1 then Var11_freq = Var11;
	if Var12 ge 1 then Var12_freq = Var12;
	if Var13 ge 1 then Var13_freq = Var13;
	if Var14 ge 1 then Var14_freq = Var14;
	if Var15 ge 1 then Var15_freq = Var15;
	if Var16 ge 1 then Var16_freq = Var16;
	if Var17 ge 1 then Var17_freq = Var17;
	if Var18 ge 1 then Var18_freq = Var18;
	if Var19 ge 1 then Var19_freq = Var19;
	if Var20 ge 1 then Var20_freq = Var20;
	if Var21 ge 1 then Var21_freq = Var21;
	if Var22 ge 1 then Var22_freq = Var22;
	if Var23 ge 1 then Var23_freq = Var23;
	if Var24 ge 1 then Var24_freq = Var24;
	if Var25 ge 1 then Var25_freq = Var25;
	if Var26 ge 1 then Var26_freq = Var26;
	if Var27 ge 1 then Var27_freq = Var27;
	if Var28 ge 1 then Var28_freq = Var28;
	if Var29 ge 1 then Var29_freq = Var29;
	if Var30 ge 1 then Var30_freq = Var30;
	/*There are more than 30 variables, but this gives the gist*/
        array ONES Var1--Var30;
	do over ones;
		if ones GE 1 then ones = 1;
		else ones = 0;
	end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 12:22:51 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611552#M18218</guid>
      <dc:creator>LEINAARE</dc:creator>
      <dc:date>2019-12-13T12:22:51Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611556#M18219</link>
      <description>&lt;P&gt;Transpose the dataset to a long format, and the code will be dead simple.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 12:28:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611556#M18219</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T12:28:03Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611557#M18220</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt;,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for the advise.&amp;nbsp; Below is the program I wrote to experiment.&amp;nbsp; Is this in line with what you were suggesting?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;proc means data=cohort_claims_1218 sum noprint idmin;
	var claimflag--fp_emergency sti:;
	by orsid recordid log;
	id date;
	output out=flagfreqs sum=;
run;	*14,624,576;

proc transpose data=flagfreqs (obs=100 drop=_: date)
	out=middle;
	by orsid recordid log;
run;
data middle;
	set middle;
	if COL1 ge 1 then COL2 = 1;
		else COL2 = 0;
run;
proc transpose data=middle
	out=final (drop=_name_)
	suffix=_freq;
	id _name_;
	by orsid recordid log;
	var COL2;
run;
data want;
	merge flagfreqs (obs=100)
		final;
	by orsid recordid log;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 13 Dec 2019 13:22:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611557#M18220</guid>
      <dc:creator>LEINAARE</dc:creator>
      <dc:date>2019-12-13T13:22:30Z</dc:date>
    </item>
    <item>
      <title>Re: Using an array efficantly with a macro</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611587#M18224</link>
      <description>&lt;P&gt;Yes, this is exactly what I had in mind. Transposing to long lets you use the "natural loop" of the data step iteration without having to resort to array coding.&lt;/P&gt;</description>
      <pubDate>Fri, 13 Dec 2019 14:42:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Using-an-array-efficantly-with-a-macro/m-p/611587#M18224</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-13T14:42:28Z</dc:date>
    </item>
  </channel>
</rss>

