<?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 Is there a macro of function can do any base conversion of numbers? in SAS Programming</title>
    <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823263#M325062</link>
    <description>&lt;P&gt;42 in decimal, is&amp;nbsp;101010 in binary, 2A in hex, 52 in octal, or 222 in&amp;nbsp;quaternary.&lt;/P&gt;
&lt;P&gt;Is&amp;nbsp;there a macro of function can do any base conversion of numbers?&lt;/P&gt;</description>
    <pubDate>Thu, 14 Jul 2022 09:38:32 GMT</pubDate>
    <dc:creator>whymath</dc:creator>
    <dc:date>2022-07-14T09:38:32Z</dc:date>
    <item>
      <title>Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823263#M325062</link>
      <description>&lt;P&gt;42 in decimal, is&amp;nbsp;101010 in binary, 2A in hex, 52 in octal, or 222 in&amp;nbsp;quaternary.&lt;/P&gt;
&lt;P&gt;Is&amp;nbsp;there a macro of function can do any base conversion of numbers?&lt;/P&gt;</description>
      <pubDate>Thu, 14 Jul 2022 09:38:32 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823263#M325062</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2022-07-14T09:38:32Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823270#M325068</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For positive integers I'm aware of&amp;nbsp;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/16961"&gt;@ChrisNZ&lt;/a&gt;'s macro CONVERTBASE attached to his 2020 post&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/653973/highlight/true#M196445" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/653973/highlight/true#M196445&lt;/A&gt;. For fractions between 0 and 1 I wrote a function BASECONVFP (using ideas from ChrisNZ's macro) and posted it in the same thread:&amp;nbsp;&lt;A href="https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/654626/highlight/true#M196566" target="_blank" rel="noopener"&gt;https://communities.sas.com/t5/SAS-Programming/Converting-a-19-length-number-to-hex-and-then-to-a-string/m-p/654626/highlight/true#M196566&lt;/A&gt;. In both cases allowable bases are 2, 3, ..., 36 and numbers with many (e.g. 100) digits are no problem.*&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;For the conversion of positive binary, hexadecimal and octal integers of moderate size to and from the decimal system there are the SAS-supplied BINARY, HEX and OCTAL &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/p13eggx7qc7l6in1oprinvadzubl.htm" target="_blank" rel="noopener"&gt;informats&lt;/A&gt;&amp;nbsp;and&amp;nbsp;&lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/leforinforref/p0z62k899n6a7wn1r5in6q5253v1.htm" target="_blank" rel="noopener"&gt;formats&lt;/A&gt;.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;* Edit: And of course you can combine the macro and the function to convert non-integer numbers &amp;gt;1, as shown in the code example (log) below:&lt;/P&gt;
&lt;PRE&gt;1    data _null_;
2    n=42;
3    put n binary6. / n hex2. / n octal2.;
4    call symputx('dec',n+0.38);
5    run;

101010
2A
52
NOTE: DATA statement used (Total process time):
      real time           0.11 seconds
      cpu time            0.11 seconds


6
7    %put &amp;amp;=dec;
DEC=42.38
8    %put %ConvertBase(10,4,%scan(&amp;amp;dec,1)).%sysfunc(baseconvfp(%scan(&amp;amp;dec,2),10,4,61));
222.1201101322320110132232011013223201101322320110132232011013223&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jul 2022 11:48:37 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823270#M325068</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-14T11:48:37Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823283#M325075</link>
      <description>&lt;P&gt;There is not a SAS function for performing arbitrary radix to radix conversion.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can write your own in Proc FCMP.&amp;nbsp; For an arbitrary value representation in the originating radix, you would need to supply a list of glyphs representing the digits of the radix - likewise for the destination radix.&amp;nbsp; You can find the use of base 5, 10, 12, 20, 60 in antiquity and nominally angular radixes of 360, 400 and &lt;SPAN&gt;π&lt;/SPAN&gt;.&amp;nbsp; A V7 SAS Name could be construed in a numeric sense as base 37.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Example:&lt;/P&gt;
&lt;P&gt;Simplified for representing integer number in an integer radix from 2 to 33 using glyphs&amp;nbsp;&lt;CODE class=" language-sas"&gt;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_&lt;/CODE&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;options cmplib = ();

proc fcmp outlib=sasuser.utility.number;
  function IntToRadix(in_integer, in_radix) $33;

    integer = abs(floor(in_integer));
    radix = floor(in_radix);

    glyphs = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_';

    if radix &amp;gt; length(glyphs) or radix &amp;lt; 2 then
      return (catx(' ', 'Radix', radix, 'not implemented.'));

    sign = sign(in_integer);

    length result $33;
    result = '';

    if radix &amp;gt; 1 then do place = 1 by 1 while (integer &amp;gt; 0);
      remainder = mod(integer,radix);
      result = cats(substr(glyphs,remainder+1,1), result);
      integer = (integer - remainder) / radix;

      if place &amp;gt;=7 then 
        leave;

    end;
    else do; 
      /* todo, stick marks for base 1*/
    end;

    if missing (result) then
      result = '0';
    else
    if sign &amp;lt; 0 then
      result = '-' || result;

    return (result);
  endsub;

quit;

options cmplib = sasuser.utility;

data representations;
  do radix = 2 to 33;
    do num = 0 to 100;
      representation = IntToRadix(num,radix);
      output;
    end;
  end;
run;

proc sort; by num radix;
proc transpose data=representations out=reptable prefix=radix_;
  by num;
  var representation;
  id radix;
run;&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Thu, 14 Jul 2022 12:48:08 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823283#M325075</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2022-07-14T12:48:08Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823447#M325130</link>
      <description>&lt;P&gt;Thanks, @&lt;A class="trigger-hovercard" href="https://communities.sas.com/t5/user/viewprofilepage/user-id/32733" target="_blank"&gt;FreelanceReinhard&lt;/A&gt;, Very impressive discussion.&lt;/P&gt;
&lt;P&gt;The only weird thing&amp;nbsp;is I run your example from &amp;lt;baseconvfp.sas&amp;gt; and get nothing output in euc-cn enviroment, but get right output in unicode&amp;nbsp;enviroment. How do you think of that?&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 01:25:22 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823447#M325130</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2022-07-15T01:25:22Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823448#M325131</link>
      <description>Your code always amazing me.&lt;BR /&gt;Could you please explain the meaning of "if place &amp;gt;=7 then leave;"?</description>
      <pubDate>Fri, 15 Jul 2022 01:30:15 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823448#M325131</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2022-07-15T01:30:15Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823451#M325132</link>
      <description>Ahhhh... if place &amp;gt;=7 then leave; is a developmental artifact that stopped the algorithm at the 7th representational glyph -- essentially a guard against a run-away loop while testing.  It should be removed from the code.</description>
      <pubDate>Fri, 15 Jul 2022 03:18:19 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823451#M325132</guid>
      <dc:creator>RichardDeVen</dc:creator>
      <dc:date>2022-07-15T03:18:19Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823469#M325145</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;The only weird thing&amp;nbsp;is I run your example from &amp;lt;baseconvfp.sas&amp;gt; and get nothing output in euc-cn enviroment, but get right output in unicode&amp;nbsp;enviroment. How do you think of that?&lt;/P&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;The BASECONVFP function uses the SAS character functions LENGTH, CHAR, RANK, BYTE and SUBSTR, all of which are "designed for SBCS [single-byte character set] data" (documentation), so it's well possible that problems arise in a MBCS (multi-byte character set) environment. I hadn't thought of this possibility during development. Glad to hear that it works in a Unicode environment nevertheless. I assume that a version not relying on the ASCII collating sequence would require a comprehensive revision of the code.&lt;/P&gt;</description>
      <pubDate>Fri, 15 Jul 2022 07:57:45 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/823469#M325145</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-15T07:57:45Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/824013#M325396</link>
      <description>&lt;P&gt;Hi @FreelanceReinhard , When I use hex. or octal. format, I always need to specify the length of format or I will get many leading zeros in result. Like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;1    data _null_;
2      n=42;
3      put n hex.;
4    run;

0000002A&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;Now I try use substr() and findc() function to remove the leading zeros, just like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;5    data _null_;
6      n=42;
7      hex=put(n,hex.);
8      hexc=substr(hex,findc(hex,'123456789'));
9      put hexc;
10   run;

2A&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;But do you have an easy way to slove this?&lt;/P&gt;</description>
      <pubDate>Tue, 19 Jul 2022 02:17:42 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/824013#M325396</guid>
      <dc:creator>whymath</dc:creator>
      <dc:date>2022-07-19T02:17:42Z</dc:date>
    </item>
    <item>
      <title>Re: Is there a macro of function can do any base conversion of numbers?</title>
      <link>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/824040#M325403</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://communities.sas.com/t5/user/viewprofilepage/user-id/270406"&gt;@whymath&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;
&lt;P&gt;Now I try use substr() and findc() function to remove the leading zeros, just like:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;5    data _null_;
6      n=42;
7      hex=put(n,hex.);
8      hexc=substr(hex,findc(hex,'123456789'));
9      put hexc;
10   run;

2A&lt;/CODE&gt;&lt;/PRE&gt;
&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;
&lt;P&gt;You would need to extend the second argument of FINDC to &lt;FONT face="courier new,courier"&gt;'123456789&lt;STRONG&gt;ABCDEF&lt;/STRONG&gt;'&lt;/FONT&gt; to make this work in general (e.g., for n=160). It's easier to use the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p0aesccozrvg19n1bg83z1jb9nid.htm" target="_blank" rel="noopener"&gt;VERIFY function&lt;/A&gt;:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hexc=substr(hex,verify(hex,'0'));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Another option is the &lt;A href="https://documentation.sas.com/doc/en/pgmsascdc/9.4_3.5/lefunctionsref/p12zqzvwx4dv6kn1p9crijxswolk.htm" target="_blank" rel="noopener"&gt;PUTN function&lt;/A&gt; where you can supply the format width (as a function of &lt;FONT face="courier new,courier"&gt;n&lt;/FONT&gt;) at run time:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;hex=putn(n,cat('hex',int(log2(n)/4)+1));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;and similarly&amp;nbsp;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-sas"&gt;oct=putn(n,cat('octal',int(log2(n)/3)+1));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Tue, 19 Jul 2022 06:27:06 GMT</pubDate>
      <guid>https://communities.sas.com/t5/SAS-Programming/Is-there-a-macro-of-function-can-do-any-base-conversion-of/m-p/824040#M325403</guid>
      <dc:creator>FreelanceReinh</dc:creator>
      <dc:date>2022-07-19T06:27:06Z</dc:date>
    </item>
  </channel>
</rss>

