<?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: %IF %THEN in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671062#M201470</link>
    <description>&lt;P&gt;Do not place macro definitions into the middle of a data step.&amp;nbsp; The macro processor will process the macro statements and generate text that is passed to SAS to evaluate. So the macro definition will be compiled (and your call to it will run) BEFORE that data step is compiled and so before the data step can start running.&amp;nbsp; Putting the macro definition into the middle of the data step will just confuse you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You passed the macro the string msrp which you then compared to 30000.&amp;nbsp; The letter m comes after the digit 3 so the macro always generates this statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; profitlabel= "greater than 30000";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to test the value of the variable MSRP you need your macro to generate an actual IF statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you run this program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tr (M=);
if &amp;amp;M &amp;lt;30000 then do;
  profitlabel= "greater than 30000";
end;
else if &amp;amp;M&amp;gt;30000 %then do;
  profitlabel = "Less than 30000";
end;
%mend;

data cars3;
  set cars;
  %tr (M=msrp);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is the same as running this data step without the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars3;
  set cars;
  if msrp &amp;lt;30000 then do;
    profitlabel= "greater than 30000";
  end;
  else if msrp&amp;gt;30000 %then do;
    profitlabel = "Less than 30000";
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Tue, 21 Jul 2020 15:11:50 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2020-07-21T15:11:50Z</dc:date>
    <item>
      <title>%IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671022#M201468</link>
      <description>&lt;P&gt;Hi, I am learning to work with %IF %THEN in macros and neither of my below codes are working. Could you please help me understand what is going here?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;data cars3;&lt;BR /&gt;set cars;&lt;BR /&gt;%macro tr (M=);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; %if &amp;amp;M &amp;lt;30000 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; profitlabel= "greater than 30000";&lt;BR /&gt;&amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%else %if &amp;amp;M&amp;gt;30000 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;profitlabel = "Less than 30000";&lt;BR /&gt;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;BR /&gt;%tr (M=msrp);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Above code did not work---all values are populated as "Less than 30000"*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;/* Below&amp;nbsp; code did not work---profitlabel is not created at all*/&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;data cars4;&lt;BR /&gt;set cars;&lt;BR /&gt;profit = msrp-invoice;&lt;/P&gt;&lt;P&gt;%macro msrp (T=, M=);&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%if &amp;amp;T = "&amp;amp;M" %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if profit&amp;gt;3000 then profitlabel= "greater than 3000";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else profitlabel = "Less than 3000";&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;%end;&lt;/P&gt;&lt;P&gt;%mend;&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%msrp (T=Make,M=Acura);&lt;/P&gt;&lt;P&gt;run;&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 14:58:24 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671022#M201468</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2020-07-21T14:58:24Z</dc:date>
    </item>
    <item>
      <title>Re: %IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671062#M201470</link>
      <description>&lt;P&gt;Do not place macro definitions into the middle of a data step.&amp;nbsp; The macro processor will process the macro statements and generate text that is passed to SAS to evaluate. So the macro definition will be compiled (and your call to it will run) BEFORE that data step is compiled and so before the data step can start running.&amp;nbsp; Putting the macro definition into the middle of the data step will just confuse you.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You passed the macro the string msrp which you then compared to 30000.&amp;nbsp; The letter m comes after the digit 3 so the macro always generates this statement.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; profitlabel= "greater than 30000";&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you want to test the value of the variable MSRP you need your macro to generate an actual IF statement.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you run this program:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tr (M=);
if &amp;amp;M &amp;lt;30000 then do;
  profitlabel= "greater than 30000";
end;
else if &amp;amp;M&amp;gt;30000 %then do;
  profitlabel = "Less than 30000";
end;
%mend;

data cars3;
  set cars;
  %tr (M=msrp);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is the same as running this data step without the macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars3;
  set cars;
  if msrp &amp;lt;30000 then do;
    profitlabel= "greater than 30000";
  end;
  else if msrp&amp;gt;30000 %then do;
    profitlabel = "Less than 30000";
  end;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:11:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671062#M201470</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-07-21T15:11:50Z</dc:date>
    </item>
    <item>
      <title>Re: %IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671064#M201472</link>
      <description>&lt;P&gt;Since the macro is resolved when code text is fetched for interpretation/compilation, it&amp;nbsp;&lt;STRONG&gt;NEVER&lt;/STRONG&gt; has access to the&amp;nbsp;&lt;EM&gt;values&lt;/EM&gt; of data step variables.&lt;/P&gt;
&lt;P&gt;This macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tr (M=);
    %if &amp;amp;M &amp;lt;30000 %then %do;
            profitlabel= "greater than 30000";
   %end;
%else %if &amp;amp;M&amp;gt;30000 %then %do;
       profitlabel = "Less than 30000";
%end;
%mend;
%tr (M=msrp);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;resolves macro variable &amp;amp;m to the&amp;nbsp;&lt;EM&gt;text(!)&lt;/EM&gt;&amp;nbsp;msrp, so the condition turns into&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%if msrp &amp;lt;30000 %then %do;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Since letters come after numbers in the ASCII character table, this condition resolves to false, and the following code is not created.&lt;/P&gt;
&lt;P&gt;But the next condition is true, so the code from it is sent to the data step compiler, which now gets this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data cars3;
set cars;
       profitlabel = "Less than 30000";
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;What you want for your first code is this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro tr (M=);
if &amp;amp;M ge 30000
then profitlabel= "greater than 30000";
else profitlabel = "Less than 30000";
%mend;

data cars3;
set cars;
%tr (M=msrp);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Similar issues happen with your second code.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:14:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671064#M201472</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T15:14:28Z</dc:date>
    </item>
    <item>
      <title>Re: %IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671072#M201477</link>
      <description>&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/11562"&gt;@Kurt_Bremser&lt;/a&gt; &lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159"&gt;@Tom&lt;/a&gt; thank you for your help! I am trying to learn how to use %if %then. Looks like the same can be achieved using if - then and macro variables. I could not find much documentation on %if %then in the advanced SAS certification prep guide for different scenarios to use %if %then. Could you by any chance point me in the right direction?</description>
      <pubDate>Tue, 21 Jul 2020 15:25:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671072#M201477</guid>
      <dc:creator>Tommer</dc:creator>
      <dc:date>2020-07-21T15:25:49Z</dc:date>
    </item>
    <item>
      <title>Re: %IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671075#M201480</link>
      <description>&lt;P&gt;A simple rule of thumb:&lt;/P&gt;
&lt;P&gt;%IF controls which code is&amp;nbsp;&lt;EM&gt;created&lt;/EM&gt; for the interpreter/compiler, while IF controls which part of the code (branch) is&amp;nbsp;&lt;EM&gt;executed&lt;/EM&gt;.&lt;/P&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:33:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671075#M201480</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-07-21T15:33:05Z</dc:date>
    </item>
    <item>
      <title>Re: %IF %THEN</title>
      <link>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671078#M201481</link>
      <description>Hello,&lt;BR /&gt;&lt;BR /&gt;Macro language is used for dynamic code generation. Macros allow to reuse code without copying/pasting. When a macro program is executed, a first phase transforms macro code into base SAS language (which is executed in a second phase). Code enclosed with %if; ... %then; will be integrated to the final code when the condition is true and will be ignored otherwise.&lt;BR /&gt;&lt;BR /&gt;For example, the program :&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;set sashelp.class;&lt;BR /&gt;%if 1 %then %do;&lt;BR /&gt;  keep name;&lt;BR /&gt;%end;&lt;BR /&gt;%if 0 %then %do;&lt;BR /&gt;  keep age;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;will become&lt;BR /&gt;&lt;BR /&gt;data have;&lt;BR /&gt;set sashelp.class;&lt;BR /&gt;keep name;&lt;BR /&gt;run;&lt;BR /&gt;&lt;BR /&gt;</description>
      <pubDate>Tue, 21 Jul 2020 15:40:48 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/IF-THEN/m-p/671078#M201481</guid>
      <dc:creator>gamotte</dc:creator>
      <dc:date>2020-07-21T15:40:48Z</dc:date>
    </item>
  </channel>
</rss>

