SAS Compress函数返回FALSE值

问题描述 投票:0回答:1

我在SAS中使用'压缩'功能时遇到了一些麻烦。我的目的是从此数据步骤中的“注释”字段中删除任何字母字符。

但是,当我执行下面的代码时,'NewPrice'字段显示FALSE而不是预期值。

data WORK.basefile2;

        length TempFileName Filenameused $300.;
         %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
         infile CSV2 delimiter = ',' MISSOVER DSD lrecl=32767 firstobs=3 Filename=TempFileName;             
            Filenameused= TRANWRD(substr(TempFileName, 48), ".xlsx.csv", "");
            informat customer_id $8.;
            informat Name $50.;
            informat Reco_issue $50.;
            informat Reco_action $50.;
            informat ICD $50.;
            informat Start_date $50.;
            format customer_id $8.;
            format Name $50.;
            format Reco_issue $50.;
            format Reco_action $50.;
            format ICD $50.;
            format Start_date $50.;
            format Comments $255.;
            format Description $255.;

            NewPrice=COMPRESS(Comments, '', 'kd');

            input
            customer_id $
            Name $
            'Total Spend'n $
            Template $
            Product_id $
            Description $
            Start_date $
            CD_Sales 
            CD_Lines
            CD_Level $
            CD_Price $
            CD_uom $
            CD_Discount $
            Reco_issue $
            Reco_action $
            Reco_price $
            Reco_discount $
            Impact_£
            Impact
            Agree $
            Comments $
            ICD $
            Structure_Code $
            Deal_type $
            NewPrice;
           run;

Output when code is executed

示例数据(逗号分隔):

ASDFGH,TEST,“£31,333.00”,15AH,156907,TEST,2016年8月18日,“£4,003.10”,222,5,£5.19,M ,,低于硬地板,变动率,£6.63 ,,£0.48, 21.72%,N,在与客户谈判新价格时,ASDFGH,TEST,“31,333.00英镑”,15AH,475266,TEST,2016年11月11日,49.61,29,5英镑,2.52英镑,EA ,,硬地板,变动率,£6.36 ,,£1.28,60.38%,N,与客户谈判新价格ASDFGH,TEST,“£31,333.00”,15AH,404740,TEST,21/09/2017,£38.69,1, 5,£116.07,EA ,,低于硬地板,变动率,£163.80 ,,£15.91,29.14%,N,在与客户谈判新价格时ASDFGH,TEST,“£31,333.00”,15AH,476557,TEST, 2016年11月11日,32.13,25,5英镑,1.32英镑,EA,低于硬地板,变动率,£2.76,£0.48,52.17%,N,在与客户谈判新价格时ASDFGH,TEST, “£31,333.00”,15AH,476553,TEST,11/11/2016,£29.17,11,5,£1.29,EA ,,低于硬地板,变动率,£3.39 ,,£0.70,61.95%,N,In与客户谈判新价格ASDFGH,TEST,“£31,333.00”,15AH,476557,TEST,2016年11月11日,£17.61,5,5,£3.96,EA ,,低于硬地板,变动率,£9.69 ,,£1.91,59.13%,N,在与客户谈判新价格时ASDFGH,TEST,“£31,333.00”,15AH,475261,TEST,11/11/2016,£16.70,4,5 ,£10.92,EA ,,低于硬地板,变动率,26.67英镑,£5.25,59.06%,N,在与客户谈判新价格时ASDFGH,TEST,“£31,333.00”,15AH,476546,TEST,11 / 11/2016,>15.73,10,5,>0.96,EA,,Below Hard Floor,变动率,£2.67 ,,£0.57,64.04%,N,在与客户谈判新价格时ASDFGH,TEST,“ £31,333.00“,15AH,476549,TEST,11/11/2016,£5.84,3,5,£1.86,EA ,,在硬地板,变动率,£6.00 ,,£1.38,69.00%,N,在谈判中与客户的新价格ASDFGH,TEST,“£31,333.00”,15AH,477340,TEST,2016年11月11日,£3.75,2,5,£4.11,EA ,,低于硬地板,变化率,11.40英镑, ,£2.43,63.95%,N,在与客户谈判新价格ASDFGH,TEST,“£31,333.00”,, 259738,TEST,13/01/2018,“£45,173.66”,403,5,£10.35,EA ,20,低于硬地板,变动率,£10.80 ,,£0.15,4.17%,N,新价格同意£3.52 ASDFGH,TEST,“£31,333.00”,, 297622,TEST ,13/01/2018,£736.60,5,5,£10.95,EA,20,低于硬地板,变动率,11.46英镑,£0.17,4.45%,N,新价格同意£3.75 ASDFGH,TEST,“ £31,333.00“,, 105384,TEST,19/07/2017,£223.44,1,5,£11.25,BG,42.5,低于硬地板,变动率,11.49英镑,£0.08,2.09%,N,新价格同意£3.76

任何帮助将不胜感激!

谢谢,

亨利

sas
1个回答
0
投票

首先,我认为你的意思是NEWPRICE=' ',而不是FALSE,因为SAS本身并没有“假”。不过,' '在布尔表达式中将被视为FALSE

COMPRESS在这里返回' ',因为COMMENTS的值完全是字母字符。你的COMPRESS论证要求它保留数字和空格(''仍然是一个单独的空格,即使它看起来不像它 - 如果你不想要空间则将参数保持为空),这意味着它只会保留空格和数字。对于大多数记录,COMMENTS字段中没有数字,因此您只有空格,被视为等同于SAS缺失的空格。

其他记录,你有问题,你保持空间,所以它不会变成一个整齐的数字。您将需要使用input,并且可能不保留空格,以获得您想要的值。 (你也可能想保留.,不是吗?)或者让NEWPRICE成为一个字符字段。

最后,你的行:

NewPrice=COMPRESS(Comments, '', 'kd');

在输入语句之前,这是一个问题 - comments的值在运行时尚未定义。

例如,这有效。注意我不明白为什么你在输入中列出了NewPrice(以及其他一些也没有定义的字段)...

data WORK.basefile2;


         %let _EFIERR_ = 0; /* set the ERROR detection macro variable */
         infile datalines delimiter = ',' MISSOVER DSD ;                        
            informat customer_id $8.;
            informat Name $50.;
            informat Reco_issue $50.;
            informat Reco_action $50.;
            informat ICD $50.;
            informat Start_date $50.;
            format customer_id $8.;
            format Name $50.;
            format Reco_issue $50.;
            format Reco_action $50.;
            format ICD $50.;
            format Start_date $50.;
            format Comments $255.;
            format Description $255.;

            input
            customer_id $
            Name $
            'Total Spend'n $
            Template $
            Product_id $
            Description $
            Start_date $
            CD_Sales 
            CD_Lines
            CD_Level $
            CD_Price $
            CD_uom $
            CD_Discount $
            Reco_issue $
            Reco_action $
            Reco_price $
            Reco_discount $
            Impact_Pound
            Impact
            Agree $
            Comments $
            ICD $
            Structure_Code $
            Deal_type $
            ;


            NewPrice=input(COMPRESS(Comments, '.', 'kd'),best32.);
datalines;
ASDFGH,TEST,"£31,333.00",15AH,156907,TEST,18/10/2016,"£4,003.10",222,5,£5.19,M,,Below Hard Floor,Change Rate,£6.63,,£0.48,21.72%,N,New Prices Agreed £3.76
ASDFGH,TEST,"£31,333.00",15AH,475266,TEST,11/11/2016,£49.61,29,5,£2.52,EA,,At Hard Floor,Change Rate,£6.36,,£1.28,60.38%,N,In negotiations with the customer for new prices 
ASDFGH,TEST,"£31,333.00",15AH,404740,TEST,21/09/2017,£38.69,1,5,£116.07,EA,,Below Hard Floor,Change Rate,£163.80,,£15.91,29.14%,N,In negotiations with the customer for new prices 
;;;;
           run;
© www.soinside.com 2019 - 2024. All rights reserved.