<?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: Condition in a MACRO not working in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535112#M6410</link>
    <description>&lt;P&gt;First, write your code in structured way that lets you see logical blocks; as it is, that ugly spaghetti is close to un-decipherable. See Maxim 12.&lt;/P&gt;
&lt;P&gt;Next, use %put to follow your macro logic:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro central_bank (reg, begin, end); 
									   
%do i = &amp;amp;begin. %to &amp;amp;end.;

  %put loop iteration for &amp;amp;i.;


  %if &amp;amp;begin.= 28 or &amp;amp;begin.= 29 %then %do;

    %put branch for begin in (28,29);

  %end;

  %else %if &amp;amp;begin.= 30 or &amp;amp;begin.= 31 %then %do;

    %put branch for begin in (30,31);

  %end;

%end;

%mend central_bank;

%central_bank (US, 28, 31);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The log from that (see Maxim 2) tells you that your second %if prevents the second block from executing:&lt;/P&gt;
&lt;PRE&gt;37         %macro central_bank (reg, begin, end);
38         									
39         %do i = &amp;amp;begin. %to &amp;amp;end.;
40         
41           %put loop iteration for &amp;amp;i.;
42         
43         
44           %if &amp;amp;begin.= 28 or &amp;amp;begin.= 29 %then %do;
45         
46             %put branch for begin in (28,29);
47         
48           %end;
49         
50           %else %if &amp;amp;begin.= 30 or &amp;amp;begin.= 31 %then %do;
51         
52             %put branch for begin in (30,31);
53         
54           %end;
55         
56         %end;
2                                                          Das SAS System                         07:58 Wednesday, February 13, 2019

57         
58         %mend central_bank;
59         
60         %central_bank (US, 28, 31);
loop iteration for 28
branch for begin in (28,29)
loop iteration for 29
branch for begin in (28,29)
loop iteration for 30
branch for begin in (28,29)
loop iteration for 31
branch for begin in (28,29)
&lt;/PRE&gt;</description>
    <pubDate>Wed, 13 Feb 2019 07:01:57 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2019-02-13T07:01:57Z</dc:date>
    <item>
      <title>Condition in a MACRO not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535109#M6409</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have the following code running:&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro central_bank (reg, begin, end); 
									   
%do i= &amp;amp;begin. %to &amp;amp;end.;

data &amp;amp;reg.&amp;amp;i.;
set &amp;amp;reg.&amp;amp;i.;

if Code= "US28" or Code= "US29" then do;
if Actual= 9999 or Surv_Med= 9999 then delete;

Surp_B_&amp;amp;reg.&amp;amp;i.= Surp_Bloom;
SD_B_&amp;amp;reg.&amp;amp;i.= Std_Dev_Surv;

Diff_&amp;amp;reg.&amp;amp;i.= Actual-Surv_Med;
Ratio_&amp;amp;reg.&amp;amp;i.= Actual/Surv_Med; end;

PS_&amp;amp;reg.&amp;amp;i.= 1;
Year= year (Date);
Interval= (int(Time)/300)+1;
run;

proc sort data= &amp;amp;reg.&amp;amp;i.;
by Date Interval;
run;

%if &amp;amp;begin.= 28 or &amp;amp;begin.= 29 %then %do;

proc sql;
create table C_&amp;amp;reg.&amp;amp;i. as
select *, STD(Diff_&amp;amp;reg.&amp;amp;i.) as SD_&amp;amp;reg.&amp;amp;i., Diff_&amp;amp;reg.&amp;amp;i./STD(Diff_&amp;amp;reg.&amp;amp;i.) as Surp_&amp;amp;reg.&amp;amp;i. from &amp;amp;reg.&amp;amp;i.;
quit;

proc means data= C_&amp;amp;reg.&amp;amp;i. nway noprint;
class Year;
output out= YSC_&amp;amp;reg.&amp;amp;i. STD(Diff_&amp;amp;reg.&amp;amp;i.)= YSD_&amp;amp;reg.&amp;amp;i.;
run;

proc sort data= C_&amp;amp;reg.&amp;amp;i.;
by Date Year;
run;

proc sort data= YSC_&amp;amp;reg.&amp;amp;i.;
by Year;
run;

data C_&amp;amp;reg.&amp;amp;i.;
merge C_&amp;amp;reg.&amp;amp;i. YSC_&amp;amp;reg.&amp;amp;i.;
by Year;
run;

proc sort data= C_&amp;amp;reg.&amp;amp;i.;
by Date Interval;
run;

data C_&amp;amp;reg.&amp;amp;i.;
set C_&amp;amp;reg.&amp;amp;i.;

YSurp_&amp;amp;reg.&amp;amp;i.= Diff_&amp;amp;reg.&amp;amp;i./YSD_&amp;amp;reg.&amp;amp;i.;
run;

data C_&amp;amp;reg.&amp;amp;i.;
set C_&amp;amp;reg.&amp;amp;i.;

keep Date Time Interval PS_&amp;amp;reg.&amp;amp;i. Diff_&amp;amp;reg.&amp;amp;i. SD_&amp;amp;reg.&amp;amp;i. SD_B_&amp;amp;reg.&amp;amp;i. YSD_&amp;amp;reg.&amp;amp;i. Surp_&amp;amp;reg.&amp;amp;i. Surp_B_&amp;amp;reg.&amp;amp;i. YSurp_&amp;amp;reg.&amp;amp;i. Ratio_&amp;amp;reg.&amp;amp;i.;
run;

%end;

%else %if &amp;amp;begin.= 30 or &amp;amp;begin.= 31 %then %do;

data C_&amp;amp;reg.&amp;amp;i.;
set &amp;amp;reg.&amp;amp;i.;

keep Date Time Interval PS_&amp;amp;reg.&amp;amp;i.;
run;

%end;

%end;

%mend central_bank;

%central_bank (US, 28, 31);&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Though I have coded so that some part of the code gets executed for a certain value of the macro variable, I see that the whole code gets run, and %if condition does not work.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Where did I go wrong?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Much thanks.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Regards.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 06:36:49 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535109#M6409</guid>
      <dc:creator>d6k5d3</dc:creator>
      <dc:date>2019-02-13T06:36:49Z</dc:date>
    </item>
    <item>
      <title>Re: Condition in a MACRO not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535112#M6410</link>
      <description>&lt;P&gt;First, write your code in structured way that lets you see logical blocks; as it is, that ugly spaghetti is close to un-decipherable. See Maxim 12.&lt;/P&gt;
&lt;P&gt;Next, use %put to follow your macro logic:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro central_bank (reg, begin, end); 
									   
%do i = &amp;amp;begin. %to &amp;amp;end.;

  %put loop iteration for &amp;amp;i.;


  %if &amp;amp;begin.= 28 or &amp;amp;begin.= 29 %then %do;

    %put branch for begin in (28,29);

  %end;

  %else %if &amp;amp;begin.= 30 or &amp;amp;begin.= 31 %then %do;

    %put branch for begin in (30,31);

  %end;

%end;

%mend central_bank;

%central_bank (US, 28, 31);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The log from that (see Maxim 2) tells you that your second %if prevents the second block from executing:&lt;/P&gt;
&lt;PRE&gt;37         %macro central_bank (reg, begin, end);
38         									
39         %do i = &amp;amp;begin. %to &amp;amp;end.;
40         
41           %put loop iteration for &amp;amp;i.;
42         
43         
44           %if &amp;amp;begin.= 28 or &amp;amp;begin.= 29 %then %do;
45         
46             %put branch for begin in (28,29);
47         
48           %end;
49         
50           %else %if &amp;amp;begin.= 30 or &amp;amp;begin.= 31 %then %do;
51         
52             %put branch for begin in (30,31);
53         
54           %end;
55         
56         %end;
2                                                          Das SAS System                         07:58 Wednesday, February 13, 2019

57         
58         %mend central_bank;
59         
60         %central_bank (US, 28, 31);
loop iteration for 28
branch for begin in (28,29)
loop iteration for 29
branch for begin in (28,29)
loop iteration for 30
branch for begin in (28,29)
loop iteration for 31
branch for begin in (28,29)
&lt;/PRE&gt;</description>
      <pubDate>Wed, 13 Feb 2019 07:01:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535112#M6410</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2019-02-13T07:01:57Z</dc:date>
    </item>
    <item>
      <title>Re: Condition in a MACRO not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535133#M6416</link>
      <description>&lt;P&gt;Sideways to your question, you would save yourself&lt;/P&gt;
&lt;P&gt;- A lot of coding and maintenance&lt;/P&gt;
&lt;P&gt;- A lot of processor and storage space&lt;/P&gt;
&lt;P&gt;Simply by changing your approach to your problem.&amp;nbsp; I can tell this as soon as I see coding like:&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;&lt;SPAN class="token procnames"&gt;data&lt;/SPAN&gt; &lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;reg&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;;&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and&lt;/P&gt;
&lt;PRE class=" language-sas"&gt;&lt;CODE class="  language-sas"&gt;Surp_B_&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;&lt;SPAN class="token procnames"&gt;reg&lt;/SPAN&gt;&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;SPAN class="token operator"&gt;&amp;amp;&lt;/SPAN&gt;i&lt;SPAN class="token punctuation"&gt;.&lt;/SPAN&gt;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;This shows that your are creating lots of datasets for&amp;nbsp;the same data and that in those datasets you are creating lots of variables.&amp;nbsp; Both of these show a badly worked out data modelling structure.&lt;/P&gt;
&lt;P&gt;Without seeing the data/process in full my suggested change would be to create a record (observation) for each of the data items like:&lt;/P&gt;
&lt;P&gt;PARAM&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;RESULT...&lt;/P&gt;
&lt;P&gt;SURP_B_US_1&amp;nbsp; ....&lt;/P&gt;
&lt;P&gt;SURP_B_US_2&amp;nbsp; ...&lt;/P&gt;
&lt;P&gt;Then instead of creating a new dataset (i.e. read/writes for each group) use a by group to do the calculations in one simple datastep/means output by your by groups and params, to get one complete results dataset out.&amp;nbsp; This would give you 1 dataset in, 1 dataset out (1*read/write) and very simple Base SAS which is easy to code and maintain.&amp;nbsp; The only reason for transposed data is for output purposes and this can be achieved by a proc transpose before proc report.&lt;/P&gt;</description>
      <pubDate>Wed, 13 Feb 2019 10:01:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535133#M6416</guid>
      <dc:creator>RW9</dc:creator>
      <dc:date>2019-02-13T10:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Condition in a MACRO not working</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535245#M6431</link>
      <description>If you have any option to modify this code I would strongly recommend removing the macro logic entirely and switching to data step. It's faster, more efficient and easier to maintain in the long run.</description>
      <pubDate>Wed, 13 Feb 2019 16:02:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Condition-in-a-MACRO-not-working/m-p/535245#M6431</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2019-02-13T16:02:45Z</dc:date>
    </item>
  </channel>
</rss>

