<?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: Calculations using Macro Variables in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746487#M234183</link>
    <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;,&amp;nbsp;the value of a macro variable is text.&amp;nbsp; Or you can think of it as code.&amp;nbsp; But it's not a "character value" like the data step language character value.&amp;nbsp; The (main) job of the macro language is to generate SAS code.&amp;nbsp; You can think of it as code substitution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you have working SAS code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set sashelp.class ;
  MyAnswer = 1 / 2;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let numerator=1 ;
%let denominator=2 ;

data want ;
  set sashelp.class ;
  MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because the below step works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set sashelp.class ;
  MyAnswer = height / weight;
run ;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let numerator=height ;
%let denominator=weight ;

data want ;
  set sashelp.class ;
  MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you think about macro variables resolving, think about generating code.&amp;nbsp; You're not dealing with a data step character value.&amp;nbsp; All the macro variables resolve before the step even compiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After your macro variables resolve, SAS will compile the step, then execute the step, and you will see if there were any errors.&lt;/P&gt;</description>
    <pubDate>Tue, 08 Jun 2021 15:06:34 GMT</pubDate>
    <dc:creator>Quentin</dc:creator>
    <dc:date>2021-06-08T15:06:34Z</dc:date>
    <item>
      <title>Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746440#M234157</link>
      <description>&lt;P&gt;I have saved two macro variables which I know is saved as text. But there are "numbers" and I would like to use them in a calculation. Any suggestions on how to do this nicely?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;How I thought, roughly, that I could solve it:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyCalculation;
MyAnswer = input(&amp;amp;=Numerator) / input(&amp;amp;=Denominator);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Any suggestions on how to do this nicely?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 12:48:46 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746440#M234157</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-06-08T12:48:46Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746443#M234159</link>
      <description>&lt;P&gt;You can do this:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyCalculation;
MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;It is NOT necessary to save numbers in macro variables in most cases, and you could find the macro variables truncate the number to a certain number of decimal places (depending on how you do it). Yes, there are unusual cases where that is necessary, but for simple division of two variables, use DATA step variables, where the full number of digits is available.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 13:04:11 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746443#M234159</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-08T13:04:11Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746452#M234165</link>
      <description>&lt;P&gt;Doing that gives the error:&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;ERROR 386-185: Expecting an arithmetic expression.

ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant, 
              a missing value, arrayname, (, +, -, INPUT, NOT, PUT, ^, _NEW_, ~.  

ERROR 200-322: The symbol is not recognized and will be ignored.

30         run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:08:21 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746452#M234165</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-06-08T14:08:21Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746454#M234166</link>
      <description>&lt;P&gt;To debug macro code and code with macro variables, add this command as the first line of your program and run it again.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options mprint symbolgen mlogic;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then show us the entire log for this DATA step, including code as it appears in the log, plus all ERRORs, NOTEs and WARNINGs.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;PS: you should &lt;FONT color="#FF0000"&gt;&lt;STRONG&gt;always&lt;/STRONG&gt; &lt;/FONT&gt;show us the entire log for the step that has the errors, not just the error messages.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:22:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746454#M234166</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2021-06-08T14:22:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746459#M234169</link>
      <description>&lt;P&gt;That suggests your macro variables may have something besides a number.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;1    %let numerator=1 ;
2    %let denominator=2 ;
3
4    data MyCalculation;
5    MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
6    put MyAnswer= ;
7    run;

MyAnswer=0.5
NOTE: The data set WORK.MYCALCULATION has 1 observations and 1 variables.
&lt;/PRE&gt;
&lt;P&gt;Can you show your full code, and full log?&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:18:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746459#M234169</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-06-08T14:18:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746475#M234173</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I calculate the number of rows in a data set (DataA) and store it in a Macro variable (wether this is a good way of doing it or not I don't know but it works):&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
	if 0 then set DataA nobs=n;
	call symputx('Numerator',n);
	stop;
run;
%put &amp;amp;=Numerator;&lt;/PRE&gt;
&lt;P&gt;Then I do the same for another data set (DataB) and store it in a Macro variable:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	if 0 then set DataB nobs=n;
	call symputx('Denominator',n);
	stop;
run;
%put &amp;amp;=Denominator;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I want to calculate the fraction (Numerator/Denominator) by using the macro variables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;What I tried was then:&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data MyCalculation;
MyAnswer = input(&amp;amp;=Numerator) / input(&amp;amp;=Denominator);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately I cannot give the complete log and original variable names now since it is company data I am using. But the principle can be seen by the code here i hope.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:35:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746475#M234173</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-06-08T14:35:45Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746476#M234174</link>
      <description>&lt;P&gt;I calculate the number of rows in a data set (DataA) and store it in a Macro variable (wether this is a good way of doing it or not I don't know but it works):&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;data _null_;
	if 0 then set DataA nobs=n;
	call symputx('Numerator',n);
	stop;
run;
%put &amp;amp;=Numerator;&lt;/PRE&gt;
&lt;P&gt;Then I do the same for another data set (DataB) and store it in a Macro variable:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
	if 0 then set DataB nobs=n;
	call symputx('Denominator',n);
	stop;
run;
%put &amp;amp;=Denominator;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Then I want to calculate the fraction (Numerator/Denominator) by using the macro variables.&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;What I tried was then:&amp;nbsp;&lt;/P&gt;
&lt;PRE class="language-sas"&gt;&lt;CODE&gt;data MyCalculation;
MyAnswer = input(&amp;amp;=Numerator) / input(&amp;amp;=Denominator);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Unfortunately I cannot give the complete log and original variable names now since it is company data I am using. But the principle can be seen by the code here i hope.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:36:04 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746476#M234174</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-06-08T14:36:04Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746481#M234177</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/381436"&gt;@SasStatistics&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;I have saved two macro variables which I know is saved as text. But there are "numbers" and I would like to use them in a calculation. Any suggestions on how to do this nicely?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;How I thought, roughly, that I could solve it:&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data MyCalculation;
MyAnswer = input(&amp;amp;=Numerator) / input(&amp;amp;=Denominator);
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;BR /&gt;Any suggestions on how to do this nicely?&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;Thanks.&amp;nbsp;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I don't know where you got the idea to use &amp;amp;=Numerator but that is &lt;STRONG&gt;not&lt;/STRONG&gt; the way to reference a macro variable.&lt;/P&gt;
&lt;P&gt;Macro variable reference&amp;nbsp; would be &amp;amp;numerator.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;INPUT requires a CHARACTER value so you have yet another problem. If you used Numerator properly and was actually text that resolves to a numeric value then the INPUT would &lt;STRONG&gt;fail&lt;/STRONG&gt; because input would expect unquoted text to be a variable name and numbers wouldn't be valid or the name of a variable. Third the Input function requires the Informat to be used for converting text to numeric.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;SHOW us the values of the macro variables concerned.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 14:55:50 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746481#M234177</guid>
      <dc:creator>ballardw</dc:creator>
      <dc:date>2021-06-08T14:55:50Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746482#M234178</link>
      <description>Notice the examples do not have INPUT() but your code does. You do not require the input function here as macro variables function as literal text replacement, think copy/paste. &lt;BR /&gt;&lt;BR /&gt;They're text, but not character variables if that makes sense. &lt;BR /&gt;&lt;BR /&gt;Anyways, remove the INPUT() from your code and it will work.</description>
      <pubDate>Tue, 08 Jun 2021 14:56:09 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746482#M234178</guid>
      <dc:creator>Reeza</dc:creator>
      <dc:date>2021-06-08T14:56:09Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746487#M234183</link>
      <description>&lt;P&gt;As&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/13879"&gt;@Reeza&lt;/a&gt;,&amp;nbsp;the value of a macro variable is text.&amp;nbsp; Or you can think of it as code.&amp;nbsp; But it's not a "character value" like the data step language character value.&amp;nbsp; The (main) job of the macro language is to generate SAS code.&amp;nbsp; You can think of it as code substitution.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;So if you have working SAS code:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set sashelp.class ;
  MyAnswer = 1 / 2;
run ;
&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You could also code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let numerator=1 ;
%let denominator=2 ;

data want ;
  set sashelp.class ;
  MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Because the below step works:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data want ;
  set sashelp.class ;
  MyAnswer = height / weight;
run ;

&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You could also code:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let numerator=height ;
%let denominator=weight ;

data want ;
  set sashelp.class ;
  MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator;
run ;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When you think about macro variables resolving, think about generating code.&amp;nbsp; You're not dealing with a data step character value.&amp;nbsp; All the macro variables resolve before the step even compiles.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;After your macro variables resolve, SAS will compile the step, then execute the step, and you will see if there were any errors.&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 15:06:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746487#M234183</guid>
      <dc:creator>Quentin</dc:creator>
      <dc:date>2021-06-08T15:06:34Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746491#M234186</link>
      <description>&lt;P&gt;First problem:&amp;nbsp; &amp;amp;=mvar is for %PUT statement only.&amp;nbsp; To reference the macro variable normally just use &amp;amp;mvar.&lt;/P&gt;
&lt;P&gt;Second problem:&amp;nbsp; There is no need for the INPUT() function call.&amp;nbsp; The macro variable value becomes the text of the SAS program.&amp;nbsp; &lt;STRONG&gt;You don't need to call INPUT() to type a numeric constant into your SAS program.&lt;/STRONG&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Minor issue:&amp;nbsp; You can simplify the logic of getting NOBS= value into a macro variable.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
  call symputx('Numerator',n);
  stop;
  set DataA nobs=n;
run;
data _null_;
  call symputx('Denominator',n);
  stop;
  set DataB nobs=n;
run;

data MyCalculation;
  MyAnswer = &amp;amp;Numerator / &amp;amp;Denominator ;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 08 Jun 2021 15:21:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746491#M234186</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2021-06-08T15:21:57Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations using Macro Variables</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746528#M234196</link>
      <description>I did an other error (used &amp;amp;=MacroVariable) which as pointed out the in the comments below should only be used in the put statement. Anyway, this worked. Thanks Paige!</description>
      <pubDate>Tue, 08 Jun 2021 17:46:23 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Calculations-using-Macro-Variables/m-p/746528#M234196</guid>
      <dc:creator>SasStatistics</dc:creator>
      <dc:date>2021-06-08T17:46:23Z</dc:date>
    </item>
  </channel>
</rss>

