Building models with SAS Enterprise Miner, SAS Factory Miner, SAS Visual Data Mining and Machine Learning or just with programming

scan and remove the first few character of a sentence

Accepted Solution Solved
Reply
Regular Contributor
Posts: 218
Accepted Solution

scan and remove the first few character of a sentence

Hi All,

Can anyone tell me how do I remove everything including the first period (.) and space from the onservation?   

ID        Description

101      1. My Name is John

202      10. I am a boy. I go to school

302      502. I work. I read.        

My final output will be:

ID        Description                                 Description2

101      1. My Name is John                    My Name is John

202      10. I am a boy. I go to school.      I am a boy. I go to school.

302      502. I work. I read.                      I work. I read.

    

I wrote the following code, but it does not help if period (.) and space occurs second time.

data out;

set have;

Description2 = scan(Description,2,'.');

run;


Accepted Solutions
Solution
‎06-19-2015 03:58 PM
Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Posted in reply to RobertWoodruff

HI Robert,

can you please tell me what does this 1+index function does?


Also just found out from the other member that 2+index also does eliminate the space.

View solution in original post


All Replies
Contributor
Posts: 42

Re: scan and remove the first few character of a sentence

I too have the same issue so I will be interested to see what the solution might be.

I have a text field that can contain the word "ZIP" somewhere in the string. I need to remove everything from the word ZIP to the end of the string. The word ZIP can occur anyplace so I'm not at all sure how to do this.

Thanks for asking the question. I'll follow and hopefully it will work for me as well.

Jeff

SAS Employee
Posts: 10

Re: scan and remove the first few character of a sentence

Description = substr(Description, 1+index(Description, '.'));

Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Posted in reply to RobertWoodruff

Hi Robert,

Thanks for your code. I worked, but still leave a space at the beginning of the sentence. Can you please tell me how to fix.

Super User
Posts: 11,343

Re: scan and remove the first few character of a sentence

Description = substr(Description, 2+index(Description, '.'));

SAS Employee
Posts: 10

Re: scan and remove the first few character of a sentence

Different ways to handle this. A general method could be this:

Description = strip(substr(Description, 1+index(Description, '.')));

Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Posted in reply to RobertWoodruff

Thanks a bunch Robert, it worked!!!

Solution
‎06-19-2015 03:58 PM
Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Posted in reply to RobertWoodruff

HI Robert,

can you please tell me what does this 1+index function does?


Also just found out from the other member that 2+index also does eliminate the space.

Super User
Posts: 11,343

Re: scan and remove the first few character of a sentence

The index function returns the position in the searched string of the start of the target string. Since that is a number then you can do arithmetic to adjust a position.

For example:

1. My Name is John

The . is in position 2 and that is what Index returned. Add 1 and it gets the position of the space after the first '.'

So the result of starting at position 3 with the Substr function is " My Name is John".

Trusted Advisor
Posts: 1,137

Re: scan and remove the first few character of a sentence

You may also try the pearl regular expression

data have;

input id description &:$200.;

description2=prxchange('s/\d+[.]//',-1,description);

cards;

101      1. My Name is John

202      10. I am a boy. I go to school

302    502. I work. I read.   

;

Thanks,

Jag

Thanks,
Jag
Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Posted in reply to Jagadishkatam

Thanks Xia, that's helpful!

Respected Advisor
Posts: 4,173

Re: scan and remove the first few character of a sentence

You could also search for the position of first character which is an alphabetic character - findc(description,,'a') - and then substr() from there.

SAS(R) 9.4 Functions and CALL Routines: Reference, Third Edition 

data sample;

  infile datalines dsd dlm='|';

  input id description:$40.;

  description2=substrn(description,findc(description,,'a'));

  datalines;

101|1. My Name is John

202|10. I am a boy. I go to school

302|502. I work. I read.   

;

run;

Super User
Posts: 10,028

Re: scan and remove the first few character of a sentence

Code: Program

data have;
input id description &:$200.;
description2=prxchange('s/^\d+\.\s+//',-1,description);
cards;
101 1. My Name is John
202 10. I am a boy. I go to school
302 502. I work. I read. 
;
run;

Regular Contributor
Posts: 218

Re: scan and remove the first few character of a sentence

Thanks Xia, that's helpful!

Community Manager
Posts: 564

Re: scan and remove the first few character of a sentence

So glad you're getting some help, mlogan!

Please mark correct or helpful answers for other community members' reference.

Many thanks!

Anna

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 14 replies
  • 638 views
  • 8 likes
  • 8 in conversation