SAS SYNTAX
case when
语法
CASE <case-operand> WHEN when-condition THEN result-expression <...WHEN when-condition THEN result-expression> <ELSE result-expression> END
例子
proc sql; select Name, case when Continent = 'North America' then 'Continental U.S.' when Continent = 'Oceania' then 'Pacific Islands' else 'None' end as Region from states; proc sql; select Name, case Continent when 'North America' then 'Continental U.S.' when 'Oceania' then 'Pacific Islands' else 'None' end as Region from states;
compress
功能
从一个字符串移除特定的字符
语法
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 增加十六进制字符
只有
source
,移除空格。- 只有
source
,chars
时,从source
中移除chars
。
source
,chars
,modifiers
都有时,modifiers K
决定保留还是移除。无K
时,移除chars
加上modifiers
指定的。
/*例如这两都是移除数字*/ COMPRESS(source, “1234567890”); COMPRESS(source,, “d”);
/*这两个是移除数字和加减号*/ COMPRESS(source, “1234567890+-”); COMPRESS(source, “+-”, “d”);
例子
移除空格
data _null_; a='ABC D '; b=compress(a); put b; run;
移除小写字母
/*将修饰符设定为”l”,代表 lowcase,即将所有的小写字母加入到要删除的字符列表中;如不用修饰符”l”,也可以直接把所有a-z的小写字母列入要删除的字符串列表当中,效果一样,但显然前者比较简单;本例可以将所有小写字母和大写的”E”从指定的字符串中删除。*/ data test; set have; char1=compress(char,"E","l"); run; data test; set have; char1=compress(char,"abcdefghijklmnopqrstuvwxyzE"); run;
移除TAB
data _null_; x='1 2 3 4 5'; y=compress(x,,'s'); put y; run;
结果是12345。
保存字符
data _null_; x='Math A English B Physics A'; y=compress(x,'ABCD','k'); put y; run;
结果是ABA。