<?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 Any Wrong with this coding?! in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992655#M380445</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%global stind endind;
%let stind=0; %let endind=500; 
%macro m_sinx(nx, baseratio, noiseratio);
	/*%let nx=30;*/
	data temp_simu_sin_od;
	%do i=1 %to &amp;amp;nx;
		ybase=(round(ranuni(&amp;amp;i.),0.1)+0.1)*&amp;amp;baseratio.;
		%do x=&amp;amp;stind. %to &amp;amp;endind.;
			tick=&amp;amp;i.;
			ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
			y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
			ind=&amp;amp;x.;
			output;
		%end;
	%end;
	run;quit;

%mend;

%m_sinx(300,20,0.5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When nx=30, it takes only seconds. But when nx=300,&amp;nbsp; it takes forever. I have to stop in middle.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Any wrong with the coding?!&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;21830
21831  %m_sinx(30,20,0.5);

NOTE: The data set WORK.TEMP_SIMU_SIN_OD has 15030 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           3.52 seconds
      cpu time            3.56 seconds


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;21832
21833  %m_sinx(300,20,0.5);

NOTE: DATA statement used (Total process time):
      real time           4:48.31
      cpu time            4:03.35



&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 27 Aug 2026 11:54:34 GMT</pubDate>
    <dc:creator>hellohere</dc:creator>
    <dc:date>2026-08-27T11:54:34Z</dc:date>
    <item>
      <title>Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992655#M380445</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;
%global stind endind;
%let stind=0; %let endind=500; 
%macro m_sinx(nx, baseratio, noiseratio);
	/*%let nx=30;*/
	data temp_simu_sin_od;
	%do i=1 %to &amp;amp;nx;
		ybase=(round(ranuni(&amp;amp;i.),0.1)+0.1)*&amp;amp;baseratio.;
		%do x=&amp;amp;stind. %to &amp;amp;endind.;
			tick=&amp;amp;i.;
			ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
			y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
			ind=&amp;amp;x.;
			output;
		%end;
	%end;
	run;quit;

%mend;

%m_sinx(300,20,0.5);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;When nx=30, it takes only seconds. But when nx=300,&amp;nbsp; it takes forever. I have to stop in middle.&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;Any wrong with the coding?!&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;21830
21831  %m_sinx(30,20,0.5);

NOTE: The data set WORK.TEMP_SIMU_SIN_OD has 15030 observations and 5 variables.
NOTE: DATA statement used (Total process time):
      real time           3.52 seconds
      cpu time            3.56 seconds


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;CODE class=" language-sas"&gt;21832
21833  %m_sinx(300,20,0.5);

NOTE: DATA statement used (Total process time):
      real time           4:48.31
      cpu time            4:03.35



&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2026 11:54:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992655#M380445</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2026-08-27T11:54:34Z</dc:date>
    </item>
    <item>
      <title>Re: Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992656#M380446</link>
      <description>&lt;P&gt;Start by removing the quit statement.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Would replace the macro loops with normal loops:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro m_sinx(nx, baseratio, noiseratio);
	data temp_simu_sin_od;
    	do i = 1 to &amp;amp;nx.;
    		ybase = (round(ranuni(i) ,0.1) + 0.1) * &amp;amp;baseratio.;
    		do x = &amp;amp;stind. to &amp;amp;endind.;
    			tick = i;
    			ynoise = (ranuni(i) - 0.5) * &amp;amp;noiseratio. * 2 * ranuni(x);
    			y = ybase * sin(x / 200) + (ranuni(i) - 0.5) * &amp;amp;noiseratio. * 2 * ranuni(x);
    			ind = x;
    			output;
    		end;
    	end;

        drop i x;
	run;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Log:&lt;/P&gt;
&lt;PRE&gt;50         %m_sinx(300, 20, 0.5);

NOTE: The data set WORK.TEMP_SIMU_SIN_OD has 150300 observations and 5 variables.
NOTE:  Verwendet wurde: DATA statement - (Gesamtverarbeitungszeit):
      real time           0.03 seconds
      cpu time            0.03 seconds&lt;/PRE&gt;</description>
      <pubDate>Thu, 27 Aug 2026 12:14:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992656#M380446</guid>
      <dc:creator>andreas_lds</dc:creator>
      <dc:date>2026-08-27T12:14:15Z</dc:date>
    </item>
    <item>
      <title>Re: Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992657#M380447</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The main issue with your code -- that it creates thousands of lines of code, while it should just create data -- is resolved by&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/15475"&gt;@andreas_lds&lt;/a&gt;'s suggestion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also note that changing the seed of the RANUNI function within a DATA step is pointless: You get &lt;EM&gt;exactly&lt;/EM&gt; the same result if you replace all those seed values by the first, which is 1 in your case. There are good reasons to switch from the deprecated RANUNI function to the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0fpeei0opypg8n1b06qe4r040lv.htm#n0e7nxjw7qig3fn18z0ey7uqc91s" target="_blank" rel="noopener"&gt;RAND function&lt;/A&gt;&amp;nbsp;(here: with the 'UNIFORM' distribution) anyway: see &lt;A href="https://blogs.sas.com/content/iml/2013/07/10/stop-using-ranuni.html" target="_blank" rel="noopener"&gt;Six reasons you should stop using the RANUNI function to generate random numbers&lt;/A&gt;. (Then you would set the seed with the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0gw58qo85qp56n1kbpiz50ww8lv.htm" target="_blank" rel="noopener"&gt;CALL STREAMINIT routine&lt;/A&gt;.)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To streamline the code further, you could replace variable &lt;FONT face="courier new,courier"&gt;x&lt;/FONT&gt; by variable &lt;FONT face="courier new,courier"&gt;ind&lt;/FONT&gt; and variable &lt;FONT face="courier new,courier"&gt;i&lt;/FONT&gt; by &lt;FONT face="courier new,courier"&gt;tick&lt;/FONT&gt;. Then you could delete the two assignment statements (for &lt;FONT face="courier new,courier"&gt;tick&lt;/FONT&gt; and &lt;FONT face="courier new,courier"&gt;ind&lt;/FONT&gt;) and the DROP statement.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2026 12:46:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992657#M380447</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-08-27T12:46:03Z</dc:date>
    </item>
    <item>
      <title>Re: Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992658#M380448</link>
      <description>&lt;P&gt;Is this due to usage of %do instead of do inside macro?!&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2026 13:01:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992658#M380448</guid>
      <dc:creator>hellohere</dc:creator>
      <dc:date>2026-08-27T13:01:18Z</dc:date>
    </item>
    <item>
      <title>Re: Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992660#M380449</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Is this due to usage of %do instead of do inside macro?!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Yes, the macro %DO loops unnecessarily generated tons of DATA step code, whereas the DO loops together with the OUTPUT statement in the inner loop efficiently create the data, as intended.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2026 13:30:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992660#M380449</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2026-08-27T13:30:26Z</dc:date>
    </item>
    <item>
      <title>Re: Any Wrong with this coding?!</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992668#M380450</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/409584"&gt;@hellohere&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Is this due to usage of %do instead of do inside macro?!&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Become your own macro interpreter and see for yourself.&lt;/P&gt;
&lt;P&gt;With the %DO loops the macro is generating code like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data temp_simu_sin_od;
ybase=(round(ranuni(&amp;amp;i.),0.1)+0.1)*&amp;amp;baseratio.;
  tick=&amp;amp;i.;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
...
ybase=(round(ranuni(&amp;amp;i.),0.1)+0.1)*&amp;amp;baseratio.;
  tick=&amp;amp;i.;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
  ynoise=(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);
  y=ybase*sin(&amp;amp;x./200)+(ranuni(&amp;amp;i.)-0.5)*&amp;amp;noiseratio.*2*ranuni(&amp;amp;x.);	
  ind=&amp;amp;x.;
  output;
...

run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So the data step will take a very long time to compile before it can even begin to generate the data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;It is always best to know what code you want the macro to emit before you start writing the macro.&lt;/P&gt;</description>
      <pubDate>Thu, 27 Aug 2026 14:13:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Any-Wrong-with-this-coding/m-p/992668#M380450</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2026-08-27T14:13:37Z</dc:date>
    </item>
  </channel>
</rss>

