<?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: Strange things with an if statement in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56209#M11994</link>
    <description>Hi:&lt;BR /&gt;
  I agree with the comment that you are mixing data step concepts and macro concepts in a possibly inappropriate way.&lt;BR /&gt;
          &lt;BR /&gt;
  The %PUT statement is intended to help you debug macro processing  by writing macro variable values and/or text strings to the SAS log. It is really not intended to be used in the way you attempt in your program (conditionally). Consider these 2 LOG outputs from one program that uses %PUT and another that uses PUTLOG: (There is NO student named 'Zorro' in the sashelp.class file.):&lt;BR /&gt;
[pre]&lt;BR /&gt;
18961  *** 1) try %PUT;&lt;BR /&gt;
18962  data _null_;&lt;BR /&gt;
18963    set sashelp.class;&lt;BR /&gt;
18964      if name = 'Alfred' then do;&lt;BR /&gt;
18965          %put 'Alfred';&lt;BR /&gt;
'Alfred'&lt;BR /&gt;
18966      end;&lt;BR /&gt;
18967      else if name = 'Zorro' then do;&lt;BR /&gt;
18968          %put 'Zorro';&lt;BR /&gt;
'Zorro'&lt;BR /&gt;
18969      end;&lt;BR /&gt;
18970  run;&lt;BR /&gt;
                          &lt;BR /&gt;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
                        &lt;BR /&gt;
                      &lt;BR /&gt;
18971&lt;BR /&gt;
18972  *** 2) Use PUTLOG;&lt;BR /&gt;
18973  data _null_;&lt;BR /&gt;
18974    set sashelp.class;&lt;BR /&gt;
18975      if name = 'Alfred' then do;&lt;BR /&gt;
18976          putlog 'Alfred';&lt;BR /&gt;
18977      end;&lt;BR /&gt;
18978      else if name = 'Zorro' then do;&lt;BR /&gt;
18979          putlog 'Zorro';&lt;BR /&gt;
18980      end;&lt;BR /&gt;
18981  run;&lt;BR /&gt;
                   &lt;BR /&gt;
Alfred&lt;BR /&gt;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Also, I generally find it unecessary to use quotes when setting macro variables (as in a %LET statement) because the inclusion of quotes for the macro variable value MIGHT work in your single instance, but then if you wanted to use &amp;amp;mm2 in a title statement, for example, then if you did this (and inadvertantly used double quotes):&lt;BR /&gt;
%let mm2 = "01";&lt;BR /&gt;
title "this is the report for the month &amp;amp;mm2";&lt;BR /&gt;
&lt;BR /&gt;
you would get this in the log:&lt;BR /&gt;
[pre]&lt;BR /&gt;
18991  %let mm2 = "01";&lt;BR /&gt;
SYMBOLGEN:  Macro variable MM2 resolves to "01"&lt;BR /&gt;
18992  title "this is the report for the month &amp;amp;mm2";&lt;BR /&gt;
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.&lt;BR /&gt;
NOTE: Line generated by the macro variable "MM2".&lt;BR /&gt;
1       "this is the report for the month "01"&lt;BR /&gt;
        -----------------------------------&lt;BR /&gt;
        49&lt;BR /&gt;
                               &lt;BR /&gt;
NOTE 49-169: The meaning of an identifier after a quoted string may change in a future SAS release.&lt;BR /&gt;
              Inserting white space between a quoted string and the succeeding identifier is&lt;BR /&gt;
             recommended.&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
This code might work for you, depending on what the rest of your macro program is doing:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%let mm2 =  01 ;&lt;BR /&gt;
... ...&lt;BR /&gt;
if "&amp;amp;mm2." in ('01','03','05','07','08','10','12') then do;&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                               &lt;BR /&gt;
Here are some good papers that discuss macro basics and issues with quotes and how to use macro "quoting" functions:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi29/243-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/243-29.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/forum2007/152-2007.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2007/152-2007.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi27/p067-27.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi27/p067-27.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://ssc.utexas.edu/docs/sashelp/sugi/23/Coders/p81.pdf" target="_blank"&gt;http://ssc.utexas.edu/docs/sashelp/sugi/23/Coders/p81.pdf&lt;/A&gt;&lt;BR /&gt;
                                            &lt;BR /&gt;
cynthia</description>
    <pubDate>Wed, 22 Oct 2008 15:43:40 GMT</pubDate>
    <dc:creator>Cynthia_sas</dc:creator>
    <dc:date>2008-10-22T15:43:40Z</dc:date>
    <item>
      <title>Strange things with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56205#M11990</link>
      <description>Hi&lt;BR /&gt;
&lt;BR /&gt;
im running this script and am getting very weird results, see below.&lt;BR /&gt;
SCRIPT:&lt;BR /&gt;
%let mm2 = '01';&lt;BR /&gt;
data _null_;&lt;BR /&gt;
	if &amp;amp;mm2. in ('01','03','05','07','08','10','12') then do;&lt;BR /&gt;
			%put 'yes';&lt;BR /&gt;
		end;&lt;BR /&gt;
		else do;&lt;BR /&gt;
			%put 'no'; &lt;BR /&gt;
		end;&lt;BR /&gt;
run;&lt;BR /&gt;
&lt;BR /&gt;
LOG:&lt;BR /&gt;
910  %let mm2 = '01';&lt;BR /&gt;
911  data _null_;&lt;BR /&gt;
913      if &amp;amp;mm2. in ('01','03','05','07','08','10','12') then do;&lt;BR /&gt;
914              %put 'yes';&lt;BR /&gt;
'yes'&lt;BR /&gt;
915          end;&lt;BR /&gt;
916          else do;&lt;BR /&gt;
917              %put 'no';&lt;BR /&gt;
'no'&lt;BR /&gt;
918          end;&lt;BR /&gt;
919  run;&lt;BR /&gt;
&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
&lt;BR /&gt;
As you can see it is running both the then and the else?!?!&lt;BR /&gt;
And ideas?&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
&lt;BR /&gt;
Simon</description>
      <pubDate>Wed, 22 Oct 2008 09:29:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56205#M11990</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-22T09:29:42Z</dc:date>
    </item>
    <item>
      <title>Re: Strange things with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56206#M11991</link>
      <description>I think you are mixing macro and data step programming in an unhealthy way. Decide if you want to use macro or data step logic to solve your problem.&lt;BR /&gt;
In your case, use put statement rather than %put. You can use macro statement to control non macro execution, but you cannot use data step logic to control macro statement execution.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Wed, 22 Oct 2008 10:21:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56206#M11991</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-10-22T10:21:32Z</dc:date>
    </item>
    <item>
      <title>Re: Strange things with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56207#M11992</link>
      <description>thanks for the quick response....&lt;BR /&gt;
so what would be the best way to do the following:&lt;BR /&gt;
&lt;BR /&gt;
%let mm2 = '01';&lt;BR /&gt;
&lt;BR /&gt;
if &amp;amp;mm2. in ('01','03','05','07','08','10','12') then do;&lt;BR /&gt;
***run macro***&lt;BR /&gt;
end;&lt;BR /&gt;
&lt;BR /&gt;
Thanks&lt;BR /&gt;
&lt;BR /&gt;
Spud</description>
      <pubDate>Wed, 22 Oct 2008 13:31:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56207#M11992</guid>
      <dc:creator>deleted_user</dc:creator>
      <dc:date>2008-10-22T13:31:49Z</dc:date>
    </item>
    <item>
      <title>Re: Strange things with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56208#M11993</link>
      <description>Not knowing what will happen in the ***run macro*** section, I would probably go for the macro:&lt;BR /&gt;
&lt;BR /&gt;
%macro xxx(mm2) ;&lt;BR /&gt;
&lt;BR /&gt;
	%if %sysfunc(indexw(01 03 05 07 08 10 12,&amp;amp;MM2))  %then %do;&lt;BR /&gt;
		%put Macro is running....;&lt;BR /&gt;
	%end;&lt;BR /&gt;
	%else %put No macro runs.;&lt;BR /&gt;
&lt;BR /&gt;
%mend xxx;&lt;BR /&gt;
&lt;BR /&gt;
%xxx(01);&lt;BR /&gt;
&lt;BR /&gt;
If you are on 9.2, you should be able to use the IN operator with %if, which would make the code a bit more neat.&lt;BR /&gt;
&lt;BR /&gt;
/Linus</description>
      <pubDate>Wed, 22 Oct 2008 14:54:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56208#M11993</guid>
      <dc:creator>LinusH</dc:creator>
      <dc:date>2008-10-22T14:54:37Z</dc:date>
    </item>
    <item>
      <title>Re: Strange things with an if statement</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56209#M11994</link>
      <description>Hi:&lt;BR /&gt;
  I agree with the comment that you are mixing data step concepts and macro concepts in a possibly inappropriate way.&lt;BR /&gt;
          &lt;BR /&gt;
  The %PUT statement is intended to help you debug macro processing  by writing macro variable values and/or text strings to the SAS log. It is really not intended to be used in the way you attempt in your program (conditionally). Consider these 2 LOG outputs from one program that uses %PUT and another that uses PUTLOG: (There is NO student named 'Zorro' in the sashelp.class file.):&lt;BR /&gt;
[pre]&lt;BR /&gt;
18961  *** 1) try %PUT;&lt;BR /&gt;
18962  data _null_;&lt;BR /&gt;
18963    set sashelp.class;&lt;BR /&gt;
18964      if name = 'Alfred' then do;&lt;BR /&gt;
18965          %put 'Alfred';&lt;BR /&gt;
'Alfred'&lt;BR /&gt;
18966      end;&lt;BR /&gt;
18967      else if name = 'Zorro' then do;&lt;BR /&gt;
18968          %put 'Zorro';&lt;BR /&gt;
'Zorro'&lt;BR /&gt;
18969      end;&lt;BR /&gt;
18970  run;&lt;BR /&gt;
                          &lt;BR /&gt;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
                        &lt;BR /&gt;
                      &lt;BR /&gt;
18971&lt;BR /&gt;
18972  *** 2) Use PUTLOG;&lt;BR /&gt;
18973  data _null_;&lt;BR /&gt;
18974    set sashelp.class;&lt;BR /&gt;
18975      if name = 'Alfred' then do;&lt;BR /&gt;
18976          putlog 'Alfred';&lt;BR /&gt;
18977      end;&lt;BR /&gt;
18978      else if name = 'Zorro' then do;&lt;BR /&gt;
18979          putlog 'Zorro';&lt;BR /&gt;
18980      end;&lt;BR /&gt;
18981  run;&lt;BR /&gt;
                   &lt;BR /&gt;
Alfred&lt;BR /&gt;
NOTE: There were 19 observations read from the data set SASHELP.CLASS.&lt;BR /&gt;
NOTE: DATA statement used (Total process time):&lt;BR /&gt;
      real time           0.00 seconds&lt;BR /&gt;
      cpu time            0.00 seconds&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
Also, I generally find it unecessary to use quotes when setting macro variables (as in a %LET statement) because the inclusion of quotes for the macro variable value MIGHT work in your single instance, but then if you wanted to use &amp;amp;mm2 in a title statement, for example, then if you did this (and inadvertantly used double quotes):&lt;BR /&gt;
%let mm2 = "01";&lt;BR /&gt;
title "this is the report for the month &amp;amp;mm2";&lt;BR /&gt;
&lt;BR /&gt;
you would get this in the log:&lt;BR /&gt;
[pre]&lt;BR /&gt;
18991  %let mm2 = "01";&lt;BR /&gt;
SYMBOLGEN:  Macro variable MM2 resolves to "01"&lt;BR /&gt;
18992  title "this is the report for the month &amp;amp;mm2";&lt;BR /&gt;
WARNING: The TITLE statement is ambiguous due to invalid options or unquoted text.&lt;BR /&gt;
NOTE: Line generated by the macro variable "MM2".&lt;BR /&gt;
1       "this is the report for the month "01"&lt;BR /&gt;
        -----------------------------------&lt;BR /&gt;
        49&lt;BR /&gt;
                               &lt;BR /&gt;
NOTE 49-169: The meaning of an identifier after a quoted string may change in a future SAS release.&lt;BR /&gt;
              Inserting white space between a quoted string and the succeeding identifier is&lt;BR /&gt;
             recommended.&lt;BR /&gt;
&lt;BR /&gt;
[/pre]&lt;BR /&gt;
&lt;BR /&gt;
This code might work for you, depending on what the rest of your macro program is doing:&lt;BR /&gt;
[pre]&lt;BR /&gt;
%let mm2 =  01 ;&lt;BR /&gt;
... ...&lt;BR /&gt;
if "&amp;amp;mm2." in ('01','03','05','07','08','10','12') then do;&lt;BR /&gt;
  &lt;BR /&gt;
[/pre]&lt;BR /&gt;
                                               &lt;BR /&gt;
Here are some good papers that discuss macro basics and issues with quotes and how to use macro "quoting" functions:&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi29/243-29.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi29/243-29.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/forum2007/152-2007.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/forum2007/152-2007.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://www2.sas.com/proceedings/sugi27/p067-27.pdf" target="_blank"&gt;http://www2.sas.com/proceedings/sugi27/p067-27.pdf&lt;/A&gt;&lt;BR /&gt;
&lt;A href="http://ssc.utexas.edu/docs/sashelp/sugi/23/Coders/p81.pdf" target="_blank"&gt;http://ssc.utexas.edu/docs/sashelp/sugi/23/Coders/p81.pdf&lt;/A&gt;&lt;BR /&gt;
                                            &lt;BR /&gt;
cynthia</description>
      <pubDate>Wed, 22 Oct 2008 15:43:40 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Strange-things-with-an-if-statement/m-p/56209#M11994</guid>
      <dc:creator>Cynthia_sas</dc:creator>
      <dc:date>2008-10-22T15:43:40Z</dc:date>
    </item>
  </channel>
</rss>

