将数字转换为基于基数 36 的字母数字值

发布于 2024-10-21 08:41:18 字数 370 浏览 6 评论 0原文

通过使用 $HEX 格式,可以轻松地将 SAS 中的数字转换为基于 16 基数的字母数字值。现在我正在寻找一种简单的方法来使用基数 36(10 个数字和 26 个字母)来做到这一点。

示例:

  • 100 -> '2s'
  • 2000 -> '1jk'
  • 30000 -> '1jk' 30000 -> 'n5c'
  • 400000 -> 'n5c' 400000 -> '8kn4'

在 Java 中,您可以通过 Integer.toString(mynumber, 36) 来完成此操作。有什么想法如何在 SAS Base 中做到这一点吗?

It is easy to transform a number into a alphanumeric value based on radix 16 in SAS by using the $HEX format. Now i'm looking for an easy way to do this with radix 36 (10 numerals & 26 letters).

Examples:

  • 100 -> '2s'
  • 2000 -> '1jk'
  • 30000 -> 'n5c'
  • 400000 -> '8kn4'

In Java you can do this by Integer.toString(mynumber, 36). Any ideas how to do this in SAS Base?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

安静被遗忘 2024-10-28 08:41:19

不幸的是,有一种简单的方法可以使用格式来完成此操作,但以下数据步骤应该可以解决问题。它仅适用于正整数。

data _null_;
 infile cards;
 input innumber;
 number = innumber;
 format base $32.;
 alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 if number = 0 then base = '0';
 else
  do while (number ne 0);
   mod = mod(number, length(alphabet));
   div = floor(number / (length(alphabet)));
   base = cats(substr(alphabet,mod+1,1),base);
   number = div;
  end;
 put innumber= base=;
cards;
0
100
2000
30000
400000
;
run;

Unfortunately there is easy way to do it usings formats, but the following data step should solve the problem. It works for positive integers only.

data _null_;
 infile cards;
 input innumber;
 number = innumber;
 format base $32.;
 alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
 if number = 0 then base = '0';
 else
  do while (number ne 0);
   mod = mod(number, length(alphabet));
   div = floor(number / (length(alphabet)));
   base = cats(substr(alphabet,mod+1,1),base);
   number = div;
  end;
 put innumber= base=;
cards;
0
100
2000
30000
400000
;
run;
瑶笙 2024-10-28 08:41:19

没有用于此目的的内置功能。您可以迭代地对您的数字取模 36,然后除以 36,直到余数为零。要转换模数序列,您必须添加 48 十进制或 30 十六进制以获取数字 0-9 的 ascii 字符,并添加 101 十进制或 65 十六进制以获取数字 AZ 的 ascii 字符。

There is no built-in functionality for this. You can iteratively modulo 36 your number, then divide by 36 until what remains is zero. To convert the sequence of modulos you get you have to add 48decimal or 30hex to get the ascii-character in case of the digits 0-9 and 101decimal or 65hex to get the ascii-character in the case of the digits A-Z.

初与友歌 2024-10-28 08:41:19

我建议使用 PROC FCMP 创建您自己的函数来执行格式化。然后您可以随时重用该代码:

proc fcmp outlib=sasuser.funcs.Radix36;
   function Radix36(innumber) $ 32; /* returns character string, length 32 */
     number = innumber;
     format base $32.;
     alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     if number = 0 then base = '0';
     else do while (number ne 0);
        mod = mod(number, length(alphabet));
        div = floor(number / (length(alphabet)));
        base = cats(substr(alphabet,mod+1,1),base);
        number = div;
     end;
     return (base);
   endsub;
run;

/*****************************************************/
options cmplib=sasuser.funcs; /* add to search path */
data _null_;
input innumber;
 base = Radix36(innumber); /* call function */
 put innumber= base=;
datalines;
0
100
2000
30000
400000
;
run;

I suggest using PROC FCMP to create your own function that does the formatting. Then you can reuse the code whenever you want:

proc fcmp outlib=sasuser.funcs.Radix36;
   function Radix36(innumber) $ 32; /* returns character string, length 32 */
     number = innumber;
     format base $32.;
     alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ';
     if number = 0 then base = '0';
     else do while (number ne 0);
        mod = mod(number, length(alphabet));
        div = floor(number / (length(alphabet)));
        base = cats(substr(alphabet,mod+1,1),base);
        number = div;
     end;
     return (base);
   endsub;
run;

/*****************************************************/
options cmplib=sasuser.funcs; /* add to search path */
data _null_;
input innumber;
 base = Radix36(innumber); /* call function */
 put innumber= base=;
datalines;
0
100
2000
30000
400000
;
run;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文