<?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 statement is not working in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614532#M179666</link>
    <description>&lt;P&gt;I mean I want the code to only create the new fields for selected scenarios ('Up','Down'). However it runs for all.&lt;/P&gt;</description>
    <pubDate>Tue, 31 Dec 2019 10:00:29 GMT</pubDate>
    <dc:creator>sas_user_null</dc:creator>
    <dc:date>2019-12-31T10:00:29Z</dc:date>
    <item>
      <title>If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614412#M179599</link>
      <description>&lt;P&gt;Hi all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a question for using &lt;STRONG&gt;"if statement".&lt;/STRONG&gt;&amp;nbsp;I created a static list prompt. When selected, prompt outputs following variables:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%LET Selected_Scenarios_count = 3;&lt;BR /&gt;%LET Selected_Scenarios = Base;&lt;BR /&gt;%LET Selected_Scenarios0 = 3;&lt;BR /&gt;%LET Selected_Scenarios3 = Down;&lt;BR /&gt;%LET Selected_Scenarios1 = Base;&lt;BR /&gt;%LET Selected_Scenarios2 = Up;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here I share a sample piece of my code. I am using proc sql within sas macro. In the bold part I wish to calculate only for the Up and Down scenarios. However the code &lt;U&gt;runs for all the scenarios.&amp;nbsp;&lt;/U&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can you help?&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;%macro MY_SAMPLE();&lt;/P&gt;&lt;P&gt;proc sql; create table WORK.MY_SAMPLE_TABLE as&lt;BR /&gt;select&lt;BR /&gt;Field1,&lt;BR /&gt;Field2,&lt;BR /&gt;%do s=1 %to &amp;amp;Selected_Scenarios_count.;&lt;BR /&gt;sum(Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..) as Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..,&lt;BR /&gt;&lt;STRONG&gt;if &amp;amp;&amp;amp;selected_scenarios&amp;amp;s.. in('Up', 'Down') then do;&lt;/STRONG&gt;&lt;BR /&gt;sum(Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s.._Y&amp;amp;y.) as Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..,&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;Field5&lt;BR /&gt;from WORK.MY_SOURCE&lt;BR /&gt;group by 1,2&lt;BR /&gt;;quit;&lt;/P&gt;&lt;P&gt;%mend MY_SAMPLE;&lt;BR /&gt;%MY_SAMPLE();&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 16:13:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614412#M179599</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-30T16:13:49Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614415#M179601</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/305117"&gt;@sas_user_null&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Hi all,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a question for using &lt;STRONG&gt;"if statement".&lt;/STRONG&gt;&amp;nbsp;I created a static list prompt. When selected, prompt outputs following variables:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%LET Selected_Scenarios_count = 3;&lt;BR /&gt;%LET Selected_Scenarios = Base;&lt;BR /&gt;%LET Selected_Scenarios0 = 3;&lt;BR /&gt;%LET Selected_Scenarios3 = Down;&lt;BR /&gt;%LET Selected_Scenarios1 = Base;&lt;BR /&gt;%LET Selected_Scenarios2 = Up;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here I share a sample piece of my code. I am using proc sql within sas macro. In the bold part I wish to calculate only for the Up and Down scenarios. However the code &lt;U&gt;runs for all the scenarios.&amp;nbsp;&lt;/U&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Can you help?&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;%macro MY_SAMPLE();&lt;/P&gt;
&lt;P&gt;proc sql; create table WORK.MY_SAMPLE_TABLE as&lt;BR /&gt;select&lt;BR /&gt;Field1,&lt;BR /&gt;Field2,&lt;BR /&gt;%do s=1 %to &amp;amp;Selected_Scenarios_count.;&lt;BR /&gt;sum(Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..) as Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..,&lt;BR /&gt;&lt;STRONG&gt;if &amp;amp;&amp;amp;selected_scenarios&amp;amp;s.. in('Up', 'Down') then do;&lt;/STRONG&gt;&lt;BR /&gt;sum(Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s.._Y&amp;amp;y.) as Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..,&lt;BR /&gt;%end;&lt;BR /&gt;%end;&lt;BR /&gt;Field5&lt;BR /&gt;from WORK.MY_SOURCE&lt;BR /&gt;group by 1,2&lt;BR /&gt;;quit;&lt;/P&gt;
&lt;P&gt;%mend MY_SAMPLE;&lt;BR /&gt;%MY_SAMPLE();&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;Your code won't run at all, as it has an unbalanced %end, so the macro won't even compile.&lt;/P&gt;
&lt;P&gt;Start using proper code formatting, it helps in detecting such mistakes.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 16:19:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614415#M179601</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-30T16:19:28Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614416#M179602</link>
      <description>&lt;P&gt;Proc SQL does not have an IF. Use CASE WHEN&lt;/P&gt;
&lt;P&gt;Maybe&lt;/P&gt;
&lt;PRE&gt;case when  &amp;amp;&amp;amp;selected_scenarios&amp;amp;s. in('Up', 'Down') then  sum(Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s._Y&amp;amp;y.) 
  end as Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s.&lt;/PRE&gt;
&lt;P&gt;Note that you have several times included too many . with the &amp;amp;&amp;amp;selected_scenarios&amp;amp;s. If you use&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;s.&lt;/FONT&gt;&lt;FONT color="#008080" face="SAS Monospace" size="2"&gt;&lt;FONT size="4"&gt;&lt;STRONG&gt;&lt;FONT color="#ff0000"&gt;.&lt;/FONT&gt;&lt;/STRONG&gt;&lt;/FONT&gt;_&lt;/FONT&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Y&amp;amp;&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;y&lt;/FONT&gt; then you get something like name3text._y95 which is not going to fly in SQL either.&lt;/P&gt;
&lt;P&gt;&lt;FONT face="SAS Monospace" size="2"&gt;Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;&lt;/FONT&gt;&lt;FONT color="#804040" face="SAS Monospace" size="2"&gt;s.&lt;/FONT&gt;&lt;STRONG&gt;&lt;FONT color="#ff0000" face="SAS Monospace" size="4"&gt;. &lt;/FONT&gt;&lt;/STRONG&gt;&lt;FONT color="#000000" face="SAS Monospace" size="3"&gt;Will create a variable with a period as the last character also not valid.&lt;/FONT&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 16:21:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614416#M179602</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-30T16:21:04Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614436#M179615</link>
      <description>&lt;P&gt;&lt;STRONG&gt;Important first step&lt;/STRONG&gt;: Create working legal valid SAS code without macros and without macro variables for one or two cases (for example, in this case, when s=1 and s=2). Once you have that working, you can attempt to turn it into a SAS macro. If you don't have it working without macros and without macro variables, it will never work when you try to turn it into working macro code.&lt;/P&gt;</description>
      <pubDate>Mon, 30 Dec 2019 18:00:28 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614436#M179615</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-30T18:00:28Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614502#M179650</link>
      <description>&lt;P&gt;You need to use %IF not IF to conditionally generate code.&amp;nbsp; Also you need to tell SAS if you want the macro processor to treat IN as an operator.&amp;nbsp; Indenting your code will also make it easier to check that your logic makes sense.&amp;nbsp; Also it is much easier to scan and check multiple line statements if you place conjunctions, like the commas in SQL statements, at the beginning of the lines instead of the end of the lines.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So first you need to define the macro:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MY_SAMPLE() /minoperator mindelimiter=' ';
%local s ;
proc sql;
create table WORK.MY_SAMPLE_TABLE as
  select
    Field1
   ,Field2
%do s=1 %to &amp;amp;Selected_Scenarios_count.;
   ,sum(Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..) as Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..
  %if &amp;amp;&amp;amp;selected_scenarios&amp;amp;s.. in (Up Down) %then %do;
   ,sum(Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s.._Y&amp;amp;y.) as Name3_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..
  %end;
%end;
   ,Field5
  from WORK.MY_SOURCE
  group by 1,2
;
quit;
%mend MY_SAMPLE;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then you can set your macro variables and call the macro.&amp;nbsp; Make sure to set the macro variable Y that your code is using, but you didn't provide in your example code. You didn't provide an input dataset description, so I just made a dummy dataset with the variables needed to prevent SAS from throwing errors when trying to run the generated SQL query.&amp;nbsp;Turn on the mprint option to see what statements it generates.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%LET Selected_Scenarios_count = 3;
%LET Selected_Scenarios = Base;
%LET Selected_Scenarios0 = 3;
%LET Selected_Scenarios3 = Down;
%LET Selected_Scenarios1 = Base;
%LET Selected_Scenarios2 = Up;
%LET Y=2019;

options mprint;

data my_source;
  length field1 field2 field5 $1 name_base name_up name_down 
         Name3_Down_Y2019 Name3_Up_Y2019 8
  ;
run;

%MY_SAMPLE();&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Results:&lt;/P&gt;
&lt;PRE&gt;380   %MY_SAMPLE();
MPRINT(MY_SAMPLE):   proc sql;
MPRINT(MY_SAMPLE):   create table WORK.MY_SAMPLE_TABLE as select Field1 ,Field2 ,sum(Name_Base) as
Name_Base ,sum(Name_Up) as Name_Up ,sum(Name3_Up_Y2019) as Name3_Up ,sum(Name_Down) as Name_Down
,sum(Name3_Down_Y2019) as Name3_Down ,Field5 from WORK.MY_SOURCE group by 1,2 ;
NOTE: The query requires remerging summary statistics back with the original data.
NOTE: Table WORK.MY_SAMPLE_TABLE created, with 1 rows and 8 columns.

MPRINT(MY_SAMPLE):   quit;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 01:59:18 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614502#M179650</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-31T01:59:18Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614503#M179651</link>
      <description>&lt;P&gt;When you use &amp;amp;&amp;amp; in macro code then the macro processor will scan the token twice, consuming two of the periods.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 01:49:30 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614503#M179651</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-31T01:49:30Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614529#M179663</link>
      <description>&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thank you for your answers but I still have the problem. I altered the code with case statement as shown below. However it still does the calculation for all the scenarios. Let me also mention that I also have another code for a similar purpose which runs within data step. It also does not do the filter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I hope someone can help. Thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro MY_SAMPLE();
proc sql; create table WORK.MY_SAMPLE_TABLE as
	select 
Field1,
Field2,
	%do s=1 %to &amp;amp;Selected_Scenarios_count.;
		(case when &amp;amp;&amp;amp;selected_scenarios&amp;amp;s.. in ('Up', 'Down') then sum(Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..) end) as Name_&amp;amp;&amp;amp;selected_scenarios&amp;amp;s..,
	%end;		
Field4
		from WORK.MY_SOURCE
	group by 1,2
;quit;
%mend MY_SAMPLE;
%MY_SAMPLE();&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 31 Dec 2019 08:38:39 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614529#M179663</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-31T08:38:39Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614531#M179665</link>
      <description>&lt;P&gt;Why do you think your code would filter for anything, when there is no where clause at all?&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 09:56:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614531#M179665</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-31T09:56:23Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614532#M179666</link>
      <description>&lt;P&gt;I mean I want the code to only create the new fields for selected scenarios ('Up','Down'). However it runs for all.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 10:00:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614532#M179666</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-31T10:00:29Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614533#M179667</link>
      <description>&lt;P&gt;Please see this post for my question with data step sample.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Cannot-filter-selection-in-prompt/td-p/614530" target="_blank"&gt;https://communities.sas.com/t5/SAS-Programming/Cannot-filter-selection-in-prompt/td-p/614530&lt;/A&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 10:03:13 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614533#M179667</guid>
      <dc:creator>sas_user_null</dc:creator>
      <dc:date>2019-12-31T10:03:13Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614534#M179668</link>
      <description>&lt;P&gt;Of course it will run for all, as the loop loops through all possible selections. If you want only the selected scenarion, you must use only that, and without a %do loop, you won't even need a macro.&lt;/P&gt;
&lt;P&gt;Start by creating a working code for the selected scenarion, without using the prompt value. Once you have that, you will see how to insert the macro variable from the prompt.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 10:05:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614534#M179668</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-12-31T10:05:57Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614541#M179674</link>
      <description>&lt;P&gt;Again, you need to create valid legal working SAS code without macros and without macro variables for two cases (two iterations of your loop). If you don't have that, then your macros will never work.&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 12:00:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614541#M179674</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-31T12:00:59Z</dc:date>
    </item>
    <item>
      <title>Re: If statement is not working</title>
      <link>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614555#M179682</link>
      <description>&lt;P&gt;I don't understand what you are trying to do.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Your macro logic does not have any conditional logic to prevent if from generating one variable in the select list for each iteration of the %DO loop.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;What are the names of your dataset variables?&amp;nbsp; Does you dataset even have variables named UP, DOWN or BASE? Are they character variables? Do any of them ever have values like 'Up' or 'Down'?&lt;/P&gt;</description>
      <pubDate>Tue, 31 Dec 2019 14:35:12 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/If-statement-is-not-working/m-p/614555#M179682</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2019-12-31T14:35:12Z</dc:date>
    </item>
  </channel>
</rss>

