Advanced Uses of the Colon Modifier【转载】

In SAS, the colon (:) can be used in conjunction with all of the comparison operators (=, >, <, >=, <=, ne, in) to compare prefix's.

Consider the following examples, given the following surnames:  

  • Bevan

  • Bosley

  • Bowen

  • Burden

  • Bush

Consider the following data step statements using the colon modifier:

  

if surname =: 'B' then ...
 

will find all 5 surnames.

 

if surname =: 'Bo' then ...
 

will find Bosley and Bowen only.

 

if surname >=: 'Bo' then ...
 

will find Bosley, Bowen, Burden and Bush only.

 

if surname <: 'Bo' then ...
 

will find Bevan only.

 

if surname ne: 'Be' then ...
 

will find Bosley, Bowen, Burden and Bush only.

 

if surname in: ('Be','Bu') then ...
 

will find Bevan, Burden and Bush only.

 

Using the colon modifier with equals (=:) is commonly referred to as ‘begins with’. BEWARE! What SAS actually does is adjust the character string to be the same length before they can be compared – it will truncate the longer string to the length of the shorter string during the comparison.

 

So, given the list of surnames – Tom, Tomlinson, Tomson , the following conditional statement will return the results Tom and Tomson

 

if surname =: 'Tomson' then ...
 

The surprising result here is that surname=’Tom’ actually meets this condition. In performing this comparison SAS has determined that the value of ‘surname’ is shorter than the right hand side of the equation, and so the right hand side get’s truncated to the same length.

 

A zero length character used in either in: or =: will always evaluate to 0, or false.

 

The colon modifier can also be used as a variable name wildcard. For example, if you have a dataset with many variables sharing the same prefix but with a different suffix (e.g. balance_012008, balance_022008, balance_032008) the colon modifier can save a lot of typing .

 

data subset;
set bigdataset (keep=balance:);
...processing statements…
run;


This will keep all the variables that begin with ‘balance’ that are on ‘bigdataset’.

This feature can be used in many ways, such as:

 

sum (of balance:) 
 

will sum all the variables beginning with ‘balance’

 

array bal (*) balance:; 
 

creates an array for all ‘balance’ variables

 

drop balance:; 
 

drops all variables beginning with ‘balance’. 

转载于:https://www.cnblogs.com/godxh/p/3471453.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值