<?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: How to use a percent7.2 format in the %let macro or the %put statement? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962463#M375119</link>
    <description>&lt;P&gt;Do you just want the number?&amp;nbsp; Or do you want the 7 character string that the 7.2 format would produce from that number?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%EVAL() only does integer arithmetic.&amp;nbsp; To do real valued arithmetic in macro logic use %SYSEVALF() instead.&lt;/P&gt;
&lt;PRE&gt;1    %put num=%eval((60+75)/2);
num=67
2    %put num=%sysevalf((60+75)/2);
num=67.5
&lt;/PRE&gt;
&lt;P&gt;If you want to use a format to produce a string in particular style the use %SYSFUNC().&amp;nbsp; You could use it to call the PUTN()&amp;nbsp; function.&lt;/P&gt;
&lt;PRE&gt;3    %put num=%sysfunc(putn((60+75)/2,7.2));
num=  67.50
&lt;/PRE&gt;
&lt;P&gt;Notice that generated string is now 7 characters long (including the two leading spaces) instead of the four required to represent the number 67.5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can just include the format specification in the %SYSFUNC() call.&amp;nbsp; For example it looks like want you want is the mean of 60 and 75.&amp;nbsp; So use %SYSFUNC() to call the MEAN() function instead of the PUTN() function.&lt;/P&gt;
&lt;PRE&gt;4    %put num=%sysfunc(mean(60,75),7.2);
num=  67.50
&lt;/PRE&gt;</description>
    <pubDate>Sat, 22 Mar 2025 20:34:07 GMT</pubDate>
    <dc:creator>Tom</dc:creator>
    <dc:date>2025-03-22T20:34:07Z</dc:date>
    <item>
      <title>How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962461#M375117</link>
      <description>&lt;P&gt;Hello,&lt;/P&gt;
&lt;P&gt;The result is 67, how can apply percent7.2 format to the result? Thanks!&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num=%eval((60+75)/2);
%put &amp;amp;num; &lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1742673539632.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105594i531C757C27973CB7/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_0-1742673539632.png" alt="dxiao2017_0-1742673539632.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 22 Mar 2025 20:03:25 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962461#M375117</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-22T20:03:25Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962462#M375118</link>
      <description>&lt;P&gt;The result is supposed to be 67, and the result is not 67.5. This has nothing to do with formatting. Why? Because %eval() does integer arithmetic. It doesn't know about fractions or decimals.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you want to do floating point arithmetic with macro variables, and you in this case you want the answer to be 67.5, use %sysevalf()&lt;/P&gt;</description>
      <pubDate>Sat, 22 Mar 2025 20:27:36 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962462#M375118</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-22T20:27:36Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962463#M375119</link>
      <description>&lt;P&gt;Do you just want the number?&amp;nbsp; Or do you want the 7 character string that the 7.2 format would produce from that number?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;%EVAL() only does integer arithmetic.&amp;nbsp; To do real valued arithmetic in macro logic use %SYSEVALF() instead.&lt;/P&gt;
&lt;PRE&gt;1    %put num=%eval((60+75)/2);
num=67
2    %put num=%sysevalf((60+75)/2);
num=67.5
&lt;/PRE&gt;
&lt;P&gt;If you want to use a format to produce a string in particular style the use %SYSFUNC().&amp;nbsp; You could use it to call the PUTN()&amp;nbsp; function.&lt;/P&gt;
&lt;PRE&gt;3    %put num=%sysfunc(putn((60+75)/2,7.2));
num=  67.50
&lt;/PRE&gt;
&lt;P&gt;Notice that generated string is now 7 characters long (including the two leading spaces) instead of the four required to represent the number 67.5.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Or you can just include the format specification in the %SYSFUNC() call.&amp;nbsp; For example it looks like want you want is the mean of 60 and 75.&amp;nbsp; So use %SYSFUNC() to call the MEAN() function instead of the PUTN() function.&lt;/P&gt;
&lt;PRE&gt;4    %put num=%sysfunc(mean(60,75),7.2);
num=  67.50
&lt;/PRE&gt;</description>
      <pubDate>Sat, 22 Mar 2025 20:34:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962463#M375119</guid>
      <dc:creator>Tom</dc:creator>
      <dc:date>2025-03-22T20:34:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962469#M375121</link>
      <description>&lt;P&gt;Also ... if your title is a good description of what you want to do, you really want a macro variable formatted at percent7.2 (not 7.2), please understand a few things&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;UL&gt;
&lt;LI&gt;The percent format multiplies the number by 100 and places a percent sign after the number, so you would get 6700%&lt;/LI&gt;
&lt;LI&gt;Creating text in macro variables that contains a percent sign, although it can be handled properly, is more difficult to use in a program than the same number without the percent sign. Putting a percent sign in a macro variable is something you should avoid if at all possible — don't make decisions that make your programming more difficult.&lt;/LI&gt;
&lt;/UL&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num=%sysfunc(putn(%eval((60+75)/2),percent7.2));
%put &amp;amp;=num;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;or&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num=%eval((60+75)/2);
%put %sysfunc(putn(&amp;amp;num,percent7.2));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Sun, 23 Mar 2025 12:21:29 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962469#M375121</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-23T12:21:29Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962474#M375122</link>
      <description>&lt;P&gt;Thanks a lot, Paige! this is what I tried out:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1742745709615.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105596i9735E951112DD26F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_1-1742745709615.png" alt="dxiao2017_1-1742745709615.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 16:04:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962474#M375122</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-23T16:04:03Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962476#M375123</link>
      <description>&lt;P&gt;Thanks a lot, Tom! This is what I tried and get, I think you solved several questions I encountered when struggled with this.&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1742747384595.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105597iE8C8DC920450ED7F/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1742747384595.png" alt="dxiao2017_0-1742747384595.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 16:37:10 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962476#M375123</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-23T16:37:10Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962477#M375124</link>
      <description>&lt;P&gt;Which brings up the question: WHY?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Are you just trying to learn macro functions? Or do you actually have a use for doing artihmetic and storing these decimal values in a macro variable? Usually, arithmetic like this and decimal values like this ought to be created and stored in a SAS data set, not in a macro variable.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Also, please from now on post the code and log as text, not as screen captures.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 16:40:07 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962477#M375124</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-23T16:40:07Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962482#M375125</link>
      <description>&lt;P&gt;Thanks a lot, Paige! This is what I tried:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put num=%sysfunc(putn(%eval((60+75)/2),percent7.2));

%let num=%eval((60+75)/2);
%put %sysfunc(putn(&amp;amp;num,percent7.2));
%put %sysfunc(putn(&amp;amp;num/100,percent7.2));
%put %sysfunc(putn(&amp;amp;num/100,percent8.2));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The results is as follows (I somehow cannot copy any text in my log so have to use screenshot):&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1742749690364.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105598iD3441AA5337A872A/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_0-1742749690364.png" alt="dxiao2017_0-1742749690364.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;Therefore, if I want to use a macro (either use a %let statement or a %let and a %put statement) to calculate the mean of several numbers and get a result in a format of xx.xx% (5 digits including two decimal places, in percent), the best steps (and it looks like the correct percent format is percent8.2, not percent7.2) are 1) use a %let and %eval to calculate the number, and 2) use&amp;nbsp;%sysfunc(putn(&amp;amp;number/100,percent8.2)) in the %put statement to display the number in a percent8.2 format.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%let num=%eval((60+75)/2);
%put %sysfunc(putn(&amp;amp;num/100,percent8.2));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1742750523201.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105599iFFD367638556CEA9/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_1-1742750523201.png" alt="dxiao2017_1-1742750523201.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I have not think the question of store or reuse a macro yet. What I am doing is just use macro to write something and get the result or output (a number, a calculation, a table, or a dataset) according to what I want to produce. As for the above example, the other simpler solution is, if I just want the number without the percent sign % (and this perhaps is the common practice in real scenario, as Paige mentioned in the post and what I myself agree, the % sign and other symbol such as &amp;amp; could cause trouble in macro), I can simply use 1) the %sysevalf() to get the result with up to 14 decimal places or 2) the %sysfunc(putn(),5.2) and %sysfunc(mean(),5.2) in Tom's above post, to get the result in a 5.2 format, like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;%put num=%sysevalf((62.5+76+80.5)/3);
%put num3=%sysfunc(putn((62.5+76+80.5)/3,5.2));
%put num4=%sysfunc(mean(70,60.5,62.87,82,70),5.2);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_3-1742752058651.png" style="width: 999px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105601iF76372FDB8E163B5/image-size/large?v=v2&amp;amp;px=999" role="button" title="dxiao2017_3-1742752058651.png" alt="dxiao2017_3-1742752058651.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 18:11:34 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962482#M375125</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-23T18:11:34Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962484#M375127</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/466238"&gt;@dxiao2017&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, if I want to use a macro (either use a %let statement or a %let and a %put statement) to calculate the mean of several numbers and get a result in a format of xx.xx% (5 digits including two decimal places, in percent), the best steps (and it looks like the correct percent format is percent8.2, not percent7.2) are &lt;STRIKE&gt;1) use a %let and %eval to calculate the number, and 2) use&amp;nbsp;%sysfunc(putn(&amp;amp;number/100,percent8.2)) in the %put statement to display the number in a percent8.2 format.&lt;CODE class=" language-sas"&gt;&lt;/CODE&gt;&lt;/STRIKE&gt;&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;I disagree with the above. Do the math in a DATA step. Don't do this type of math using macro variables.&lt;/P&gt;</description>
      <pubDate>Sun, 23 Mar 2025 20:43:57 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962484#M375127</guid>
      <dc:creator>PaigeMiller</dc:creator>
      <dc:date>2025-03-23T20:43:57Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962601#M375170</link>
      <description>&lt;P&gt;Hi Paige, thanks for the suggestion. You suggest that the good way to do the math is a data step, not a macro because a macro here is not necessary and perhaps easy to cause trouble. Below is what I have being doing before, and after I begin learning macro I tried calculate this through a macro because I want to learn macro.&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data _null_;
   num=(60+75)/2;
   format num 5.2;
   put num=;
run;
data _null_;
   num=((60+75)/2)/100;
   format num percent8.2;
   put num=;
run;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1742915084063.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105623iA0120E7AD32A27E9/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_0-1742915084063.png" alt="dxiao2017_0-1742915084063.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_1-1742915173625.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105624iFFE07143B7C386CA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_1-1742915173625.png" alt="dxiao2017_1-1742915173625.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Tue, 25 Mar 2025 15:07:59 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962601#M375170</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-25T15:07:59Z</dc:date>
    </item>
    <item>
      <title>Re: How to use a percent7.2 format in the %let macro or the %put statement?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962648#M375182</link>
      <description>&lt;P&gt;And when just began learn base and sql, most often I create a dataset and then use proc means or proc sql to calculate mean statistics like this:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;data num;
   input num;
   numpct=put(num/100,percent8.2);
   datalines;
70
60.5
62.87
82
70
;
run;
proc print data=num;run;
proc means data=num n mean maxdec=2;
   var num;
run;
proc sql number;
select * from num;
select count(num) as n,
       avg(num)/100 as mean format=percent8.2
   from num;
quit;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_0-1742978266858.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105638iB98988F9B4BE14A3/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_0-1742978266858.png" alt="dxiao2017_0-1742978266858.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="dxiao2017_2-1742978350572.png" style="width: 400px;"&gt;&lt;img src="https://communities.sas.com/t5/image/serverpage/image-id/105640iAB160B71401B8C1F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="dxiao2017_2-1742978350572.png" alt="dxiao2017_2-1742978350572.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 26 Mar 2025 08:41:03 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/How-to-use-a-percent7-2-format-in-the-let-macro-or-the-put/m-p/962648#M375182</guid>
      <dc:creator>dxiao2017</dc:creator>
      <dc:date>2025-03-26T08:41:03Z</dc:date>
    </item>
  </channel>
</rss>

