如何选择一定长度的数值数据

时间:2019-03-26 17:19:11

标签: sql sas

我正在尝试选择大于5个字节的数字数据。我已输入以下内容,但收到错误消息:错误:函数长度需要一个字符表达式作为参数1。不知道我在做什么错。任何帮助将不胜感激。

29         PROC SQL;
30            CREATE TABLE WORK.FILTER_FOR_FDRMASTER_DAILY__0000 AS
31            SELECT t1.Account_id_nr,
32                   t1.Predecessor_CC_SK,
33                   t1.Successor_CC_SK,
34                   t1.Related_CC_SK,
35                   t1.Original_CC_SK,
36                   t1.Predecessor_Argt_SK,
37                   t1.Successor_Argt_SK,
38                   t1.Predecessor_OK,
39                   t1.Successor_OK,
40                   t1.CHD_SSR_IN,
41                   t1.CHD_SYSTEM_BANK,
42                   t1.USAANR
43               FROM WORK.FILTER_FOR_FDRMASTER_DAILY_2 t1
44               WHERE t1.Successor_CC_SK NOT = . AND t1.CHD_SSR_IN = 'Y' AND t1.CHD_SYSTEM_BANK NOT BETWEEN '867700000100' AND
45                    '867700009500' AND LENGTH(t1.Successor_CC_SK) > '5'
46               ORDER BY t1.Successor_CC_SK;

1 个答案:

答案 0 :(得分:0)

由于您要同时处理信用卡号和5位数字,所以我们假设它们都可以以以下三种格式之一存储:

Num:  1234567890123456
Char: 1234-5678-9012-3456
Char: 1234 5678 9012 3456

让我们从第一种情况开始,它假定变量是数字。您要排除所有5位数字值。您可以使用Rick Wicklin的this trick来计算整数中的位数:

proc sql noprint;
    create table want as
         select number
         from have
         where ceil(log10(number+1)) > 5
    ;
quit;

对于第二和第三种情况,它们将被存储为字符。您可以使用countc()选项和D来计算字符串中数字字符的数量。

proc sql noprint;
    create table want as
         select compress(number, ' -')
         from have
         where countc(number,'','D') > 5
    ;
quit;