AMI
AMI

Reputation: 1

Removing Characters from SAS String Starting on Left

I have a SAS string that always starts with a date. I want to remove the date from the substring.

Example of data is below (data does not have bullets, included bullets to increase readability)

I want the data to look like this (data does not have bullets, included bullets to increase readability)

Upvotes: 0

Views: 889

Answers (2)

Shenglin Chen
Shenglin Chen

Reputation: 4554

Index find '|' position in the string, then substr substring; or use regular expression.

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;

Upvotes: 1

Joe
Joe

Reputation: 63434

This is a great use case for call scan. If your length of date is constant (always 10), then you don't actually need this (start would be 12 then and skip to the substr, as user667489 noted in comments), but if it's not this would be helpful.

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;

It would also let you grab the second word only if that's useful (using length third argument for substr).

Upvotes: 0

Related Questions