<?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 variable multiplication and division in New SAS User</title>
    <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700391#M25709</link>
    <description>&lt;P&gt;PS the macro processor is for &lt;EM&gt;creating code&lt;/EM&gt;, not for manipulating data. For data, you always use the DATA or PROC steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you just started learning SAS, it is WAY too early to dive into macro programming. You need to learn the Base language first, before you can use the macro processor to create code in that language.&lt;/P&gt;</description>
    <pubDate>Fri, 20 Nov 2020 03:28:27 GMT</pubDate>
    <dc:creator>Kurt_Bremser</dc:creator>
    <dc:date>2020-11-20T03:28:27Z</dc:date>
    <item>
      <title>Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700367#M25700</link>
      <description>&lt;P&gt;I just started learning SAS.&lt;/P&gt;&lt;P&gt;I am trying to multiply variable by macro function.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;DATA DATA1;
input SCORE;
DATALINES;
100
;&lt;/PRE&gt;&lt;P&gt;I would like to multiple score by 10 or divide score by 10&lt;/P&gt;&lt;P&gt;I tried like this&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;%macro multiple;
DATA try;
set DATA1;
call symput("anotherscore",SCORE);
%let anotherscore=%eval(&amp;amp;anotherscore*10);
%put &amp;amp;anotherscore;
run;
%mend;&lt;/PRE&gt;&lt;P&gt;try dataset has been created but it is not multiplied by 10. How can I do it?&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 23:26:35 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700367#M25700</guid>
      <dc:creator>mutohai</dc:creator>
      <dc:date>2020-11-19T23:26:35Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700370#M25703</link>
      <description>What are you trying to do overall? Create another macro variable with the value multiplied by 10? A macro variable created in a data step isn't available until later but since you're in a data step there are other options.</description>
      <pubDate>Thu, 19 Nov 2020 23:45:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700370#M25703</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-11-19T23:45:07Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700371#M25704</link>
      <description>&lt;P&gt;UCLA introductory tutorial on macro variables and macros&lt;BR /&gt;&lt;A href="https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/" target="_blank"&gt;https://stats.idre.ucla.edu/sas/seminars/sas-macros-introduction/&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Tutorial on converting a working program to a macro&lt;BR /&gt;This method is pretty robust and helps prevent errors and makes it much easier to debug your code. Obviously biased, because I wrote it &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt; &lt;A href="https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md" target="_blank"&gt;https://github.com/statgeek/SAS-Tutorials/blob/master/Turning%20a%20program%20into%20a%20macro.md&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;Examples of common macro usage&lt;BR /&gt;&lt;A href="https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716" target="_blank"&gt;https://communities.sas.com/t5/SAS-Communities-Library/SAS-9-4-Macro-Language-Reference-Has-a-New-Appendix/ta-p/291716&lt;/A&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%macro multiple;
DATA try;
set DATA1;
*creates macro variable - no need to create intermediate variable;
call symputx('anotherscore', score*10, g);
run;
%mend;

%multiple;
%put &amp;amp;anotherscore.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You have a couple of issues going on here:&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;mixing macro and data step code (%LET versus CALL SYMPUTX())&lt;/LI&gt;
&lt;LI&gt;macro scope - what exists inside macro is different than outside&lt;/LI&gt;
&lt;LI&gt;when macro variables are compiled/available&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;So overall, the optimal solution depends on what you're trying to do....another option&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let score = 100;
%let anotherscore = %sysevalf(&amp;amp;score*10);

%put score.;
%put anotherScore.;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/357828"&gt;@mutohai&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I just started learning SAS.&lt;/P&gt;
&lt;P&gt;I am trying to multiply variable by macro function.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I have a dataset&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;DATA DATA1;
input SCORE;
DATALINES;
100
;&lt;/PRE&gt;
&lt;P&gt;I would like to multiple score by 10 or divide score by 10&lt;/P&gt;
&lt;P&gt;I tried like this&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;%macro multiple;
DATA try;
set DATA1;
call symput("anotherscore",SCORE);
%let anotherscore=%eval(&amp;amp;anotherscore*10);
%put &amp;amp;anotherscore;
run;
%mend;&lt;/PRE&gt;
&lt;P&gt;try dataset has been created but it is not multiplied by 10. How can I do it?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Nov 2020 23:50:58 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700371#M25704</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2020-11-19T23:50:58Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700373#M25705</link>
      <description>&lt;P&gt;Macros aren't the best tool to use when you want to perform arithmetic (other than integer addition). You'd be better off doing the arithmetic in a DATA step.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 00:01:55 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700373#M25705</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2020-11-20T00:01:55Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700390#M25708</link>
      <description>&lt;P&gt;You don't need a macro definition (no macro logic used), and you don't need macro arithmetic:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA try;
set DATA1;
call symputx("anotherscore",SCORE * 10);
run;
%put &amp;amp;anotherscore.;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Fri, 20 Nov 2020 03:23:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700390#M25708</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-20T03:23:23Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700391#M25709</link>
      <description>&lt;P&gt;PS the macro processor is for &lt;EM&gt;creating code&lt;/EM&gt;, not for manipulating data. For data, you always use the DATA or PROC steps.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;And if you just started learning SAS, it is WAY too early to dive into macro programming. You need to learn the Base language first, before you can use the macro processor to create code in that language.&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 03:28:27 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700391#M25709</guid>
      <dc:creator>Kurt_Bremser</dc:creator>
      <dc:date>2020-11-20T03:28:27Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700568#M25712</link>
      <description>&lt;P&gt;Thank you for answering my question. I understand it. Thank you.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 20:36:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700568#M25712</guid>
      <dc:creator>mutohai</dc:creator>
      <dc:date>2020-11-20T20:36:09Z</dc:date>
    </item>
    <item>
      <title>Re: Macro variable multiplication and division</title>
      <link>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700571#M25713</link>
      <description>&lt;P&gt;Your code has a timing issue. The macro processor scans the text first and passes the results onto SAS to run.&amp;nbsp; So your %LET and %PUT will run before the SAS processor even starts to compile the data step and the macro variable ANOTHERSCORE will either not exist or have some other value.&amp;nbsp; You essentially ran this code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let anotherscore=%eval(&amp;amp;anotherscore*10);
%put &amp;amp;anotherscore;

DATA try;
  set DATA1;
  call symput("anotherscore",SCORE);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you change the order so that the macro code runs after the data step has run then the value of the ANOTHERSCORE macro variable will be multiplied by 10 and the result printed to the log.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;DATA try;
  set DATA1;
  call symput("anotherscore",SCORE);
run;

%let anotherscore=%eval(&amp;amp;anotherscore*10);
%put &amp;amp;anotherscore;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You also have two other problems here.&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;First the value of the macro variable changes over and over as the data step runs for each observation read from the input dataset DATA1. Only the value from the last observation will be in the macro variable when the data step is finished.&lt;/P&gt;
&lt;P&gt;Second the %EVAL() function only works for integer arithmetic.&amp;nbsp; If SCORE is not an integer then you need to use the %SYSEVALF() function instead.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Nov 2020 17:51:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/New-SAS-User/Macro-variable-multiplication-and-division/m-p/700571#M25713</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2020-11-20T17:51:42Z</dc:date>
    </item>
  </channel>
</rss>

