命甴己造 发表于 2019-7-1 14:27:32

SAS中compress函数的用法


COMPRESS 函数

【功能】从一个字符串移除特定的字符

Syntax:compress (<source, chars><, modifiers>)

source: 指定一个字符串来源
chars: 指定要删除或者保留的字符列表,需用引号
modifiers: 指定修饰符,不区分大小写,用来控制 compress 函数的具体功能。如:

a 增加(A – Z, a – z)到初始字符里(chars)。

d 增加数字到初始字符里(chars)。

f 增加下划线和字母(A – Z, a – z)到初始字符里(chars)。

g 增加图形字符到初始字符里(chars)。

k 不移除初始字符(chars)而是返回这些字符。

l 增加小写字母(a – z)。

n 增加数字、下划线和字母(A – Z, a – z)。

p 增加标点符号。

s 增加空格,包括空格、水平制表符、垂直制表符、回车符、换行符和换页符。

t 剪掉尾部空格。

u 增加大写字母(A – Z)。

w 增加可印刷的字符。

X 增加十六进制字符

【详细】

1.只有source,移除空格。

2.只有source,chars时,从source中移除chars。

3. source ,chars,modifiers都有时,modifiers K决定保留还是移除。无K时,移除chars加上modifiers指定的。例如这两都是移除数字,COMPRESS(source, “1234567890”);COMPRESS(source,, “d”);这两个是移除数字和加减号,COMPRESS(source, “1234567890+-”);COMPRESS(source, “+-”, “d”);

*Example 1移除空格;

data _null_;
a=’ABC D’;
b=compress(a);
put b;
run;
*Example 2:移除小写字母;

data test;
set have;
char1=compress(char,”E”,”l”);
run;
data test;
set have;
char1=compress(char,”abcdefghijklmnopqrstuvwxyzE”);
run;
*Example 3:移除Tab;

data _null_;
x=’1
2
3
4
5′;
y=compress(x,,’s’);
put y;
run;

*Example 4:保存字符;

data _null_;
x=’Math A English B Physics A’;
y=compress(x,’ABCD’,’k’);
put y;
run;
页: [1]
查看完整版本: SAS中compress函数的用法