<?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: Macro with IF statement that sometimes is condition and sometimes is null in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613611#M179225</link>
    <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** UNTESTED CODE ***/
%Macro RRR(City,t,condition);
    data Required&amp;amp;t.   ;
         set raw_tbl(where=(group=”&amp;amp;city”));
         Y=min(x1,x2);
         %if &amp;amp;condition ^= %then &amp;amp;condition;
    run;
%mend;
%RRR(city=London,t=1, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=London,t=2, condition=)
%RRR(city=Paris,t=3, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=Paris,t=4, condition=)
%RRR(city=Roma,t=5, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=Roma,t=6, condition=)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Special characters in the condition variable (the greater than sign, the equal sign and the division sign and the semi-colon) must be masked if they are going to be transmitted to your macro. Otherwise, you get errors.&lt;/P&gt;</description>
    <pubDate>Mon, 23 Dec 2019 19:37:34 GMT</pubDate>
    <dc:creator>PaigeMiller</dc:creator>
    <dc:date>2019-12-23T19:37:34Z</dc:date>
    <item>
      <title>Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613608#M179222</link>
      <description>&lt;P&gt;Hello&lt;/P&gt;
&lt;P&gt;I am not sure how to describe the question but I will try to explain.&lt;/P&gt;
&lt;P&gt;I need to create multiple data sets .&lt;/P&gt;
&lt;P&gt;The data sets differs from each other in 2 aspects:&lt;/P&gt;
&lt;P&gt;1- City value (for example: London. Paris,Roma)&lt;/P&gt;
&lt;P&gt;2-Existence of condition (IF statement). For some queries I need to use IF statement and for some not&lt;/P&gt;
&lt;P&gt;Regarding aspect 1 (City) I know to create SAS parameter and use it&lt;/P&gt;
&lt;P&gt;Regarding aspect 2 (IF statement),I don't know how to tell SAS to use it or not to use it.&lt;/P&gt;
&lt;P&gt;May anyone help to do it in one macro?&lt;/P&gt;
&lt;P&gt;note: paramater&amp;nbsp; t is just number of table that I create&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%Macro RRR(City,t);
Data    Required&amp;amp;t.   ;
Set     raw_tbl(where=( group=”&amp;amp;Par1.”));
Y=min(x1,x2);
Run;
%mend;


%RRR(city=London,t=1, condition= IF Y&amp;gt;X3  then Y=X3/2;)
%RRR(city=London,t=2, condition= without condition;)
%RRR(city=Paris,t=3, condition= IF Y&amp;gt;X3  then Y=X3/2;)
%RRR(city= Paris,t=4, condition= without condition;)
%RRR(city=Roma,t=5, condition= IF Y&amp;gt;X3  then Y=X3/2;)
%RRR(city= Roma,t=6, condition= without condition;)
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:17:05 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613608#M179222</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-23T19:17:05Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613609#M179223</link>
      <description>&lt;P&gt;The target is to create these 6 tables&amp;nbsp; by creating one Macro&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;Data    Required1;
Set     raw_tbl(where=( group=’ London’));
Y=min(x1,x2);
IF Y&amp;gt;X3  then Y=X3/2;
Run;

Data    Required2;
Set     raw_tbl(where=(group=’ London’));
Y=min(x1,x2);
Run;
 
Data    Required3;
Set     raw_tbl(where=( group=’ Paris’));
Y=min(x1,x2);
IF Y&amp;gt;X3  then Y=X3/2;
Run;

Data    Required4;
Set     raw_tbl(where=(group=’ Paris’));
Y=min(x1,x2);
Run;

 

Data    Required5;
Set     raw_tbl(where=( group=’ Roma’));
Y=min(x1,x2);
IF Y&amp;gt;X3  then Y=X3/2;
Run;

Data    Required6;
Set     raw_tbl(where=(group=’ Roma’));
Y=min(x1,x2);
Run;

 
&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:20:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613609#M179223</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-23T19:20:22Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613611#M179225</link>
      <description>&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;/*** UNTESTED CODE ***/
%Macro RRR(City,t,condition);
    data Required&amp;amp;t.   ;
         set raw_tbl(where=(group=”&amp;amp;city”));
         Y=min(x1,x2);
         %if &amp;amp;condition ^= %then &amp;amp;condition;
    run;
%mend;
%RRR(city=London,t=1, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=London,t=2, condition=)
%RRR(city=Paris,t=3, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=Paris,t=4, condition=)
%RRR(city=Roma,t=5, condition=%str(IF Y&amp;gt;X3  then Y=X3/2;))
%RRR(city=Roma,t=6, condition=)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Special characters in the condition variable (the greater than sign, the equal sign and the division sign and the semi-colon) must be masked if they are going to be transmitted to your macro. Otherwise, you get errors.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:37:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613611#M179225</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T19:37:34Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613613#M179227</link>
      <description>&lt;P&gt;By the way&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You see these "curly" quotes in your code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;where=( group=’ London’)&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;These will not be recognized by SAS if you copy and paste into SAS, and they indicate that your code took a trip through Microsoft Word or PowerPoint or some other program that recognizes curly quotes. DO NOT PASTE YOUR CODE into these applications. It will only cause grief, because SAS will not recognize them. Only use true text editors when working with SAS code.&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;Furthermore, don't put unneeded spaces in there, as group='London' is not the same as group=' London'.&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:42:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613613#M179227</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T19:42:33Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613614#M179228</link>
      <description>&lt;P&gt;May I ask please how to write it when have more than one IF statement??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; Data    Required1;
Set     raw_tbl(where=( group=’ London’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required2;
Set     raw_tbl(where=(group=’ London’));
Y=min(x1,x2);
Run;
 
Data    Required3;
Set     raw_tbl(where=( group=’ Paris’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required4;
Set     raw_tbl(where=(group=’ Paris’));
Y=min(x1,x2);
Run;

 

Data    Required5;
Set     raw_tbl(where=( group=’ Roma’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required6;
Set     raw_tbl(where=(group=’ Roma’));
Y=min(x1,x2);
Run;


&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:42:56 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613614#M179228</guid>
      <dc:creator>Ronein</dc:creator>
      <dc:date>2019-12-23T19:42:56Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613618#M179229</link>
      <description>&lt;P&gt;You put both IF statements in &amp;amp;CONDITION&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 19:50:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613618#M179229</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2019-12-23T19:50:45Z</dc:date>
    </item>
    <item>
      <title>Re: Macro with IF statement that sometimes is condition and sometimes is null</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613638#M179233</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/159549"&gt;@Ronein&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;May I ask please how to write it when have more than one IF statement??&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt; Data    Required1;
Set     raw_tbl(where=( group=’ London’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required2;
Set     raw_tbl(where=(group=’ London’));
Y=min(x1,x2);
Run;
 
Data    Required3;
Set     raw_tbl(where=( group=’ Paris’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required4;
Set     raw_tbl(where=(group=’ Paris’));
Y=min(x1,x2);
Run;

 

Data    Required5;
Set     raw_tbl(where=( group=’ Roma’));
Y=min(x1,x2);
IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
Run;

Data    Required6;
Set     raw_tbl(where=(group=’ Roma’));
Y=min(x1,x2);
Run;


&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;If the conditional code is always exactly the same then perhaps you just need an indicator parameter.&lt;/P&gt;
&lt;PRE&gt;%Macro RRR(City=,t=,option=Y);
    data Required&amp;amp;t.   ;
         set raw_tbl(where=(group="&amp;amp;city"));
         Y=min(x1,x2);
         %if &amp;amp;option=Y %then %do;
            IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
            Else IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
         %end;
    run;
%mend;

%rrr(city=London,t=1);
%rrr(city=London,t=2,option=N);&lt;/PRE&gt;
&lt;P&gt;Named parameters as used above have a couple of advantages. One is that you can set a default value. If you do not specifically set the value then it has the default. In this case I have set it to default to using those two extra lines of code. Any value other than Y and the two lines don't get executed.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you have LOTS of different codes to execute conditionally then this may not be quite as useful.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Additionally it appears as if you might want to do this as pairs of data sets. Is that actually the case where you &lt;STRONG&gt;ALWAYS&lt;/STRONG&gt; make two output sets for each city? If so then perhaps:&lt;/P&gt;
&lt;PRE&gt;%Macro RRR(City,t);
    %let t2=%eval(t+1);
    data Required&amp;amp;t.  Required&amp;amp;t2. ;
         set raw_tbl(where=(group="&amp;amp;city"));
         Y=min(x1,x2);
         output Required&amp;amp;t.;
         IF Y&amp;gt;X3  AND Y&amp;lt;X4   then Y=X3/2;
         Else IF Y&amp;gt;X3  AND Y&amp;gt;=X4   then Y=X3/3;
         output Required&amp;amp;t2.;
    run;
%mend;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 23 Dec 2019 22:05:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Macro-with-IF-statement-that-sometimes-is-condition-and/m-p/613638#M179233</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2019-12-23T22:05:21Z</dc:date>
    </item>
  </channel>
</rss>

