BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
mlogan
Lapis Lazuli | Level 10

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;

1 ACCEPTED SOLUTION

Accepted Solutions
mlogan
Lapis Lazuli | Level 10

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

14 REPLIES 14
Jeff_DOC
Pyrite | Level 9

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

RobertWoodruff
SAS Employee

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

mlogan
Lapis Lazuli | Level 10

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.

ballardw
Super User

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

RobertWoodruff
SAS Employee

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

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

mlogan
Lapis Lazuli | Level 10

Thanks a bunch Robert, it worked!!!

mlogan
Lapis Lazuli | Level 10

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.

ballardw
Super User

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".

Jagadishkatam
Amethyst | Level 16

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
mlogan
Lapis Lazuli | Level 10

Thanks Xia, that's helpful!

Patrick
Opal | Level 21

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;

Ksharp
Super User

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;

mlogan
Lapis Lazuli | Level 10

Thanks Xia, that's helpful!

AnnaBrown
Community Manager

So glad you're getting some help, mlogan!

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

Many thanks!

Anna


Join us for SAS Community Trivia
SAS Bowl XXIX, The SAS Hackathon
Wednesday, March 8, 2023, at 10 AM ET | #SASBowl

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to choose a machine learning algorithm

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.

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