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;
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.
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
Description = substr(Description, 1+index(Description, '.'));
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.
Description = substr(Description, 2+index(Description, '.'));
Different ways to handle this. A general method could be this:
Description = strip(substr(Description, 1+index(Description, '.')));
Thanks a bunch Robert, it worked!!!
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.
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".
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 Xia, that's helpful!
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;
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;
Thanks Xia, that's helpful!
So glad you're getting some help, mlogan!
Please mark correct or helpful answers for other community members' reference.
Many thanks!
Anna
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Use this tutorial as a handy guide to weigh the pros and cons of these commonly used machine learning algorithms.
Find more tutorials on the SAS Users YouTube channel.