compress函数的用法:
格式:COMPRESS(<source><, chars><, modifiers>)。
说明:
source 指定一个要被移除字符的源字符串。
chars 指定一栏初始字符,默认它是要从source里移除的。如果指定"K"modifier,返回的结果则保存这些字符。
modifiers 指定一个修饰符(从下面的选项中选择):
k 不移除(保留)初始字符(chars),返回这些字符。
a (A - Z, a - z)
u 大写字母(A - Z)。
l 小写字母(a - z)。
d 数字
f 下划线和字母 (A - Z, a - z)
n 数字、下划线和字母(A - Z, a - z)
p 标点符号。
s 空格, 包括空格、水平制表符、垂直制表符、回车符、换行符和换页符。
t 剪掉尾部空格。
g 图形字符
w 可印刷的字符
X 十六进制字符
例
只有source,移除空格。
data _null_;
a='ABC D ';
b=compress(a);
put b;
run;
结果是ABCD。
例
只有source,chars时,从source中移除chars
data a;
input a$;
x= compress (a,'c');
cards;
cat dog ;
run;
例. source ,chars,modifiers都有
modifiers K决定保留还是移除。无K时,移除chars加上modifiers指定的。
例
移除数字,
COMPRESS(source, "1234567890");COMPRESS(source, , "d"); 例是移除数字和加减号
COMPRESS(source, "1234567890+-");COMPRESS(source, "+-", "d");
例移除小写字母data _null_;
x='123 B 234 c';
y=compress(x,'ABCD','l');
put y;
run;
结果是123 234。
例移除Tab(空格、水平制表符、垂直制表符、回车符、换行符和换页符等)data _null_;
x='1 2 3 4 5';
y=compress(x,,'s');
put y;
run;
结果是12345。
实际应用中的案例(不规则字符变量的处理)
proc import datafile="E:\正处理客户\a\年报披露时间判断\compress"
out=work.rf dbms=excel replace;
getnames=yes;
mixed=yes;
run;
data a;
length code1$12.;
set rf1;
format code1
$12.;
informat code1
$12.;
code1=compress(Stkcdy,,'kd');
if code1='' then delete;
run;
data aaa;
length d2 d3 8.;
set aa;
t1=input(Annodt,yymmdd10.);
format t1 yymmdd10.;
year=compress(scan(yydt,1,'-'),,'kd');
month=scan(yydt,2,'-');
day=compress(scan(yydt,3,'-'),,'kd') ;
d1=year-0;
d2=input(month,best12.);
format d2 best12.;
d3=input(day,best12.);
format d3 best12.;
dat=mdy(d2,d3,d1);
format dat yymmdd10.;
run;