<?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: How to use IF Then to end a loop in a macro in SAS Procedures</title>
    <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412398#M67402</link>
    <description>&lt;P&gt;You want a structure something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro whatever;
     ... do some stuff ...
    %if condition %then %do;
         ... do more stuff when condition is true ...
    %end;
%mend;
         &lt;/PRE&gt;</description>
    <pubDate>Fri, 10 Nov 2017 15:54:01 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2017-11-10T15:54:01Z</dc:date>
    <item>
      <title>How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412396#M67401</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;( I'm sorry I didn't clearly describe my questions, so I edit it a little bit, hope this time it's much clear.)&lt;/P&gt;
&lt;P&gt;I'm trying to do a iteration in a macro with do loop. But still have problems about using IF THEN to end the loop.&lt;/P&gt;
&lt;P&gt;The Logic here is: I have a initial K value, and after several calculations I got the Kt. If abs(Kt-K)&amp;gt;0.0001 then let the Kt be the K and do the calculation again until the last step to get the Kt, if&amp;nbsp;&lt;SPAN&gt;abs(Kt-K)&amp;lt;=0.0001 then end the loop and get the Kt.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;The equations I used to get Kt is Kt = 1/n * sum(log(k*x/sigma-k*theta/sigma+1)), where x is my sample, theta and sigma are constant. And at first what i was trying to do is to give a initial value of K, then calculate the Kt, and let the Kt be the new K, then do the loop for 100 times. As a result, I could get 100 values of Kt. And The code I use is as following:&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;* First clean up my dataset to get my sample X, constant theta and sigma, and give the initial K ;
data test1;
set test;
  x=prep_ad;
  theta=P75_pow;
  sigma=1/f75_pow;
  K=0.5;
keep station x theta sigma K;
run;

* Then I use macro to let it run 100 times ;

%macro EstimateK;
  %do i = 2 %to 100;
    * Calculate the value (log(K*x/sigma-K*theta/sigma+1)) for each X; 
    data test_after&amp;amp;i;
    set test_after1;
       LN =LOG((K / sigma) * x + 1 - K * theta / sigma);
    run;
    * Get the sum of LN of the former step and the total number of X (which is n in the equation). And then calculate Kt； 
    proc means data=test&amp;amp;i noprint;
       var LN;
       by station;
       output out=test_sum sum=SumLN;
    run;
    data test_sum;
    set test_sum;
       N=_FREQ_;
       Kt=SumLN/N;
    keep station Kt;
    run;
    * Record Kt for each run;
    data test_processK&amp;amp;i;
    set test_sum;
       K&amp;amp;i=K;
    keep station K&amp;amp;i;
    run;
    * Merge the new Kt to the old dataset;
    data test&amp;amp;i;
    set test&amp;amp;i;
       if _n_ =1 then set test_sum;
    run;
quit;
%end;
%mend;
%EstimateK;

* After run the macro, I merge all the Kt into one dataset;
data test_processK;
  merge test_processK2-test_processK100;
  by station;
run;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;This code force the loop run 100 times, but I'm trying to give it a condition, which is that the difference between K and Kt is less than 0.0001.&amp;nbsp;Once the condition is met, then jump out of the loop, else do it until 100 time.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;My question is I don't know where should I put the IF condition in the macro.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope anyone could help me with this problem!&lt;/P&gt;
&lt;P&gt;Thank you in advance!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Best,&lt;/P&gt;
&lt;P&gt;Hua&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 19:13:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412396#M67401</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T19:13:08Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412398#M67402</link>
      <description>&lt;P&gt;You want a structure something like this&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro whatever;
     ... do some stuff ...
    %if condition %then %do;
         ... do more stuff when condition is true ...
    %end;
%mend;
         &lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2017 15:54:01 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412398#M67402</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2017-11-10T15:54:01Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412399#M67403</link>
      <description>&lt;P&gt;1. Paste your code into a code box using the { i } icon&lt;/P&gt;
&lt;P&gt;2. Format!&lt;/P&gt;
&lt;P&gt;3. Why are you using macro logic in the data step? Use normal if/then&lt;/P&gt;
&lt;P&gt;4. To evaluate logic with decimal points in macro use SYSEVALF. Otherwise everything is text and you'll get results you don't expect.&lt;/P&gt;
&lt;P&gt;5. Why is there a quit?&amp;nbsp;&lt;/P&gt;
&lt;P&gt;6. I'm assuming this is simplified code, because as written it doesn't seem to accomplish anything...&lt;/P&gt;
&lt;P&gt;7. Add comments - then we may have an idea of what you expect the code to do, rather than what it's actually doing.&lt;/P&gt;
&lt;P&gt;8. Are you absolutely sure (I'm not) that this isn't better done in a data step or PROC OR than in a macro?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro EstimateK;
	%do i = 2 %to 100;

		data mydata&amp;amp;i;
			set mydata1;
			Kt=LOG((K / sigma) * x + 1 - K * theta / sigma);
		run;

		data mydata1;
			set mydata&amp;amp;i;

			%if abs(kt-k)&amp;gt;0.0001 %then
				K=Kt;
			%else %return;
		run;

		proc delete data=mydata&amp;amp;i;
		run;

		quit;

	%end;
%mend;

%EstimateK;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2017 15:58:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412399#M67403</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2017-11-10T15:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412414#M67406</link>
      <description>&lt;P&gt;I could be wrong, but this sounds like a problem that a single DATA step can handle:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;data want;&lt;/P&gt;
&lt;P&gt;set mydata1;&lt;/P&gt;
&lt;P&gt;do j=1 to 1000 until (&lt;SPAN&gt;abs(kt-k)&amp;gt;0.0001&lt;/SPAN&gt;);&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp; &amp;nbsp;Kt=LOG((K / sigma) * x + 1 - K * theta / sigma);&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;end;&lt;BR /&gt;&lt;SPAN&gt;run;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 16:23:26 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412414#M67406</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2017-11-10T16:23:26Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412439#M67410</link>
      <description>&lt;P&gt;Your %IF statement doesn't make any sense and is not even going to compile.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;7     %macro EstimateK;
8     %if abs(kt-k)&amp;gt;0.0001 %then K=Kt;
9     %else otherstring;
10    %mend;
11    %put %EstimateK;
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is
       required. The condition was: abs(kt-k)&amp;gt;0.0001
ERROR: The macro ESTIMATEK will stop executing.
&lt;/PRE&gt;
&lt;P&gt;Even if you got it to compile it wouldn't do what you need.&amp;nbsp; You are just comparing characters strings.&lt;/P&gt;
&lt;PRE&gt;12    %macro test ;
13    %if abs &amp;gt; 100 %then %put abs &amp;gt; 100 ; %else %put abs &amp;lt;= 100;
14    %if k &amp;gt; 100 %then %put k &amp;gt; 100; %else %put k &amp;lt;= 100;
15    %mend test ;
16    %test;
abs &amp;gt; 100
k &amp;gt; 100&lt;/PRE&gt;</description>
      <pubDate>Fri, 10 Nov 2017 17:42:31 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412439#M67410</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-10T17:42:31Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412450#M67411</link>
      <description>Yes, The logic is like this, but I tried several sentences, it doesn't work. Could you please give me more details? Thank you!</description>
      <pubDate>Fri, 10 Nov 2017 18:21:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412450#M67411</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T18:21:22Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412452#M67412</link>
      <description>Thank you for your recommendation, I will format the code next time. Yes, it is a simplified code. The reason why I don't think I could use the single step with data step is there has several steps in the process, and I was trying to use macro and do loop to let it calculate 100 times and output Kt for each run. But I don't know how to use a if condition to jump out the loop (if it meet the condition before 100 times). I had thought the quit is used for quit a do loop, isn't it?&lt;BR /&gt;Thank you for your patient reply!</description>
      <pubDate>Fri, 10 Nov 2017 18:29:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412452#M67412</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T18:29:46Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412453#M67413</link>
      <description>Thank you! I'm sorry! I simplified the code. Actually, it has several more steps before I get Kt, so a single DATA step may not help to do so.</description>
      <pubDate>Fri, 10 Nov 2017 18:31:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412453#M67413</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T18:31:28Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412457#M67414</link>
      <description>Thank you, Tom. You are right, My code doesn't work, I'm not sure where and how should I put the IF sentences. But if I put "%if abs(kt-k)&amp;gt;0.0001 %then K=Kt;" at the begin of macro, how could the code know where is the Kt and K variables?</description>
      <pubDate>Fri, 10 Nov 2017 18:35:43 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412457#M67414</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T18:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412469#M67415</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/55231"&gt;@hua&lt;/a&gt; wrote:&lt;BR /&gt;Thank you, Tom. You are right, My code doesn't work, I'm not sure where and how should I put the IF sentences. But if I put "%if abs(kt-k)&amp;gt;0.0001 %then K=Kt;" at the begin of macro, how could the code know where is the Kt and K variables?&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;First the macro code needs to test macro variables. It cannot test the values of variables in the data set.&lt;/P&gt;
&lt;P&gt;So if your success criteria depends on values of dataset variables then you need to test it with data step code and then generate a macro variable that the macro conditions can test.&lt;/P&gt;
&lt;P&gt;One way to do this might be to code your loop as a %DO %WHILE() or %DO UNTIL() and increment your counter yourself. Something like this.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro test ;
%let success=0;
%let i=0;
%do %until(&amp;amp;success or &amp;amp;i &amp;gt; 100);
  %let i=%eval(&amp;amp;i+1);
  %put Inside loop &amp;amp;=i ;

data _null_;
   if &amp;amp;i &amp;gt; 3 then call symputx('success',1);
run;

%end ;
%put Outside loop &amp;amp;=i ;
%mend test ;
%test;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;So basically you set a macro variable flag as false and then when your criteria is met you set it to true.&amp;nbsp; It is good to also include a loop counter even if you don't use it for anything just to set an upper bound on the number of times the loop will run to prevent infinite looping.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your test of K and KT would occur in whatever SAS code you are using to calculate them like the data step in the simple example above.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 18:50:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412469#M67415</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-10T18:50:37Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412501#M67417</link>
      <description>I understand "the macro code needs to test macro variables. It cannot test the values of variables in the data set." But I still don't know how to pass the value of variable to the macro variable. For example, I have the value of Kt-K in my dataset, how to make the macro variable change? Thank you!</description>
      <pubDate>Fri, 10 Nov 2017 20:52:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412501#M67417</guid>
      <dc:creator>hua</dc:creator>
      <dc:date>2017-11-10T20:52:55Z</dc:date>
    </item>
    <item>
      <title>Re: How to use IF Then to end a loop in a macro</title>
      <link>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412506#M67418</link>
      <description>&lt;P&gt;Use CALL SYMPUTX() to write to a macro variable.&lt;/P&gt;
&lt;P&gt;Your macro will only be able to test the value between data/proc steps.&lt;/P&gt;</description>
      <pubDate>Fri, 10 Nov 2017 21:02:41 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Procedures/How-to-use-IF-Then-to-end-a-loop-in-a-macro/m-p/412506#M67418</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2017-11-10T21:02:41Z</dc:date>
    </item>
  </channel>
</rss>

