从左侧开始从SAS字符串中删除字符

时间:2017-03-21 15:59:55

标签: sas substring

我有一个始终以日期开头的SAS字符串。我想从子字符串中删除日期。

数据示例如下(数据没有项目符号,包含项目符号以提高可读性)

  • 10/01/2016 | test_num15
  • 2016年11月15日| recom_1_test1
  • 2017年3月4日| test_0_8_i0 | vacc_previous0

我希望数据看起来像这样(数据没有子弹,包含子弹以提高可读性)

  • test_num15
  • recom_1_test1
  • test_0_8_i0 | vacc_previous0

2 个答案:

答案 0 :(得分:0)

这是call scan的一个很好的用例。如果您的日期长度不变(总是10),那么您实际上并不需要这个(start将是12然后跳到substr,如评论中所述的user667489),但是如果不是这样会有所帮助。

data have;
length textstr $100;
input textstr $;
datalines;
10/01/2016|test_num15
11/15/2016|recom_1_test1
03/04/2017|test_0_8_i0|vacc_previous0
;;;;
run;

data want;
  set have;
  call scan(textstr,2,start,length,'|');
  new_textstr = substr(textstr,start);
run;

如果它有用(使用length第三个参数作为substr),它也会让你抓住第二个单词。

答案 1 :(得分:0)

索引查找'|'在字符串中的位置,然后是substr子串;或使用正则表达式。

data have;
input x $50.;
x1=substr(x,index(x,'|')+1);
x2=prxchange('s/([^_]+\|)(?=\w+)//',1,x);
cards;
10/01/2016|test_num15
11/15/2016|recom_1_test1
03/04/2017|test_0_8_i0|vacc_previous0
;
run;
相关问题