全部版块 我的主页
论坛 数据科学与人工智能 数据分析与数据科学 SAS专版
29464 2
2016-03-11
今天遇到一个问题,如果字符串中既有字母又有数字,如何将数字删除,将字母保留下来?
之后发现compress不仅可以将字符串中空格删掉,加了modifier后也可以实现指定字符的删除。
于是对compress的用法做了一个总结,不全,欢迎补充~

data test;

string="ABC abcdef($1234)";

noblanks=compress(string);              /*去除字符串中的空格*/

nonum=compress(string,,'d');           /*去除字符串中的数字*/

nonumblank=compress(string,,'ds');/*去除字符串中的数字以及空格*/

nochar=compress(string,,'a');            /*去除字符串中所有字母*/

noabc1=compress(string,'ABC');       /*去除ABC三个大写字母*/

noabc2=compress(string,'abc','i');    /*此处加了条件i,忽略大小写,不论大小写,都将abc去除*/

nosign=compress(string,,'p');            /*去除字符串中的符号*/

nosignnum=compress(string,,'pd'); /*去除符号以及数字*/

retaina=compress(string,'a','ki');     /*不论大小写,将字符串中a字母保留下来,其他全都删掉*/

run;


结果如下:

1.png


2.png


下面是SAShelp中列出的Compress Modifier和说明:

a or A

addsalphabetic characters to the list of characters.


c or C

addscontrol characters to the list of characters.


d or D

addsdigits to the list of characters.


f or F

adds theunderscore character and English letters to the list of characters.


g or G

addsgraphic characters to the list of characters.


h or H

adds ahorizontal tab to the list of characters.


i or I

ignoresthe case of the characters to be kept or removed.


k or K

keeps thecharacters in the list instead of removing them.


l or L

addslowercase letters to the list of characters.


n or N

addsdigits, the underscore character, and English letters to the list ofcharacters.


o or O

processesthe second and third arguments once rather than every time the COMPRESSfunction is called. Using the O modifier in the DATA step (excluding WHEREclauses), or in the SQL procedure, can make COMPRESS run much faster when youcall it in a loop where the second and third arguments do not change.


p or P

addspunctuation marks to the list of characters.


s or S

addsspace characters (blank, horizontal tab, vertical tab, carriage return, linefeed, form feed, and NBSP ('A0'x, or 160 decimal ASCII) to the list ofcharacters.


t or T

trimstrailing blanks from the first and second arguments.


u or U

addsuppercase letters to the list of characters.


w or W

addsprintable characters to the list of characters.


x or X


addshexadecimal characters to the list of characters.


更多例子:

Example1:


x='123-4567-8901B 234-5678-9012 c';

y=compress(x,'ABCD', 'l');

put y;


123-4567-8901234-5678-9012


Example2:

x='1    2   3    4    5';

y=compress(x,, 's');

put y;


12345


Example3:

x='Math AEnglish B Physics A';

y=compress(x,'ABCD', 'k');

put y;


ABA


Example4:

x=' ';

l=lengthn(compress(x));

put l;



0



二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

全部回复
2018-9-25 11:25:50
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

2023-1-29 16:30:24
赞!非常有帮助
二维码

扫码加我 拉你入群

请注明:姓名-公司-职位

以便审核进群资格,未注明则拒绝

相关推荐
栏目导航
热门文章
推荐文章

说点什么

分享

扫码加好友,拉您进群
各岗位、行业、专业交流群