<?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 Conditionally Set Macro Variable Embedded in Macro in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449365#M283590</link>
    <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The below macro will conditionally set the macro variables &amp;amp;r1 and &amp;amp;r2 to either 0 or 1&amp;nbsp;based&amp;nbsp;on the following criteria, where &amp;amp;sub. is a random number (which was generated by another macro variable).&lt;/P&gt;&lt;P&gt;if &amp;amp;sub. is a whole number&amp;nbsp; then r1=0 and r2 = 0&lt;/P&gt;&lt;P&gt;if&amp;nbsp;&lt;SPAN&gt;&amp;amp;sub. has a decimal that rounds down then r1 = 1 and r2 = 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&amp;nbsp;&amp;amp;sub. has a decimal around rounds up then r1 =1 and r2=1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, i can't get the macro to work. When i use this macro i get the error message "Statement&amp;nbsp;is not valid or is used out of proper order". Any tips?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro round_test (sub);&lt;BR /&gt;%let r1 = x;&lt;BR /&gt;%let r2 = y;&lt;BR /&gt;%let round = %eval(%sysfunc(int(&amp;amp;sub.))-&amp;amp;sub.);&lt;BR /&gt;%if &amp;amp;round = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x=0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if round(&amp;amp;round) = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y=1;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hey Guys, I beleive i solved this with the below code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro round_test (sub);&lt;BR /&gt;%let round = %eval(%sysfunc(int(&amp;amp;sub.))-&amp;amp;sub.);&lt;BR /&gt;%if &amp;amp;round = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r1=0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if round(&amp;amp;round) = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let r1=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r1=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=1;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
    <pubDate>Wed, 28 Mar 2018 15:04:10 GMT</pubDate>
    <dc:creator>dad316</dc:creator>
    <dc:date>2018-03-28T15:04:10Z</dc:date>
    <item>
      <title>Conditionally Set Macro Variable Embedded in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449365#M283590</link>
      <description>&lt;P&gt;Hi All!&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The below macro will conditionally set the macro variables &amp;amp;r1 and &amp;amp;r2 to either 0 or 1&amp;nbsp;based&amp;nbsp;on the following criteria, where &amp;amp;sub. is a random number (which was generated by another macro variable).&lt;/P&gt;&lt;P&gt;if &amp;amp;sub. is a whole number&amp;nbsp; then r1=0 and r2 = 0&lt;/P&gt;&lt;P&gt;if&amp;nbsp;&lt;SPAN&gt;&amp;amp;sub. has a decimal that rounds down then r1 = 1 and r2 = 0&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;if&amp;nbsp;&amp;amp;sub. has a decimal around rounds up then r1 =1 and r2=1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;SPAN&gt;However, i can't get the macro to work. When i use this macro i get the error message "Statement&amp;nbsp;is not valid or is used out of proper order". Any tips?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro round_test (sub);&lt;BR /&gt;%let r1 = x;&lt;BR /&gt;%let r2 = y;&lt;BR /&gt;%let round = %eval(%sysfunc(int(&amp;amp;sub.))-&amp;amp;sub.);&lt;BR /&gt;%if &amp;amp;round = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x=0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if round(&amp;amp;round) = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; x=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; y=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;x=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;y=1;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hey Guys, I beleive i solved this with the below code:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;%macro round_test (sub);&lt;BR /&gt;%let round = %eval(%sysfunc(int(&amp;amp;sub.))-&amp;amp;sub.);&lt;BR /&gt;%if &amp;amp;round = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r1=0;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %if round(&amp;amp;round) = 0 %then %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp;%let r1=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=0;&lt;BR /&gt;%end;&lt;BR /&gt;%else %do;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r1=1;&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; %let r2=1;&lt;BR /&gt;%end;&lt;BR /&gt;run;&lt;BR /&gt;%mend;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 15:04:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449365#M283590</guid>
      <dc:creator>dad316</dc:creator>
      <dc:date>2018-03-28T15:04:10Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Set Macro Variable Embedded in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449396#M283591</link>
      <description>&lt;P&gt;You are getting closer.&amp;nbsp; However, note that:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%EVAL cannot use decimal points, let alone decimal fractions.&amp;nbsp; Use %SYSEVALF instead if you want the math to be performed on decimal fractions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Macro language does not understand the ROUND function.&amp;nbsp; Apply %SYSFUNC to the ROUND function, to let macro language execute the DATA step version of the function.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 15:22:33 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449396#M283591</guid>
      <dc:creator>Astounding</dc:creator>
      <dc:date>2018-03-28T15:22:33Z</dc:date>
    </item>
    <item>
      <title>Re: Conditionally Set Macro Variable Embedded in Macro</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449495#M283592</link>
      <description>&lt;P&gt;Why are you manipulating floating point numbers in macro variables?&amp;nbsp; If you do then make sure to use %sysevalf() instead of the default %eval().&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Looks like you want something like this.&lt;/P&gt;
&lt;P&gt;Make sure to define ROUND, R1 or R2 before you call the macro if you want to use the value after the macro ends.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro round_test (sub);
%local sign ;
%let round = %sysfunc(round(&amp;amp;sub));
%let sign = %sysfunc(sign(&amp;amp;round-&amp;amp;sub));
%let r1=%eval(&amp;amp;sign^=0);
%let r2=%eval(&amp;amp;sign=1);
%put &amp;amp;=sub &amp;amp;=round &amp;amp;=sign &amp;amp;=r1 &amp;amp;=r2 ;
%mend;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;PRE&gt;518  %round_test(1);
SUB=1 ROUND=1 SIGN=0 R1=0 R2=0
519  %round_test(1.1);
SUB=1.1 ROUND=1 SIGN=-1 R1=1 R2=0
520  %round_test(1.9);
SUB=1.9 ROUND=2 SIGN=1 R1=1 R2=1&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;If you are going to use it on negative numbers then test those also to make sure if does what you want in that case.&lt;/P&gt;</description>
      <pubDate>Wed, 28 Mar 2018 20:42:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Conditionally-Set-Macro-Variable-Embedded-in-Macro/m-p/449495#M283592</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2018-03-28T20:42:29Z</dc:date>
    </item>
  </channel>
</rss>

