楼主: nniebo
27432 2

[学习分享] 删除字符串中的指定字符方法 [推广有奖]

  • 0关注
  • 0粉丝

大专生

71%

还不是VIP/贵宾

-

威望
0
论坛币
259 个
通用积分
6.9000
学术水平
0 点
热心指数
1 点
信用等级
0 点
经验
1650 点
帖子
13
精华
0
在线时间
106 小时
注册时间
2015-7-13
最后登录
2023-6-27

相似文件 换一批

+2 论坛币
k人 参与回答

经管之家送您一份

应届毕业生专属福利!

求职就业群
赵安豆老师微信:zhaoandou666

经管之家联合CDA

送您一个全额奖学金名额~ !

感谢您参与论坛问题回答

经管之家送您两个论坛币!

+2 论坛币
今天遇到一个问题,如果字符串中既有字母又有数字,如何将数字删除,将字母保留下来?
之后发现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



二维码

扫码加我 拉你入群

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

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

关键词:字符串 punctuation Characters horizontal alphabetic 字符串

已有 2 人评分论坛币 热心指数 收起 理由
eijuhz + 1 精彩帖子
Rock2000 + 5 精彩帖子

总评分: 论坛币 + 5  热心指数 + 1   查看全部评分

沙发
sdsfmff 发表于 2018-9-25 11:25:50 |只看作者 |坛友微信交流群

使用道具

藤椅
gslzwjw 发表于 2023-1-29 16:30:24 |只看作者 |坛友微信交流群
赞!非常有帮助

使用道具

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群

京ICP备16021002-2号 京B2-20170662号 京公网安备 11010802022788号 论坛法律顾问:王进律师 知识产权保护声明   免责及隐私声明

GMT+8, 2024-5-4 23:16