DATA Step, Macro, Functions and more

how to extract perticular word.

Reply
Super Contributor
Posts: 276

how to extract perticular word.

NUMBERTEXT
1

1.YOUR NAME IS 'SANJEEV.'

22.YOUR NAME IS 'SRINATH.'

From Above data , i want to make some changes in text variable.

i want to exclude the numering in each text.

for example i want to display the text column like this  YOUR NAME IS 'SAJEEV.'(remove 1.)

Please advise Me..

Thanks..

Respected Advisor
Posts: 3,156

Re: how to extract perticular word.

Posted in reply to kuridisanjeev

Try this:

data have;

input number text&$30.;

cards;

1 1.YOUR NAME IS 'SANJEEV.'

2     2.YOUR NAME IS 'SRINATH.'

;

data want;

set have;

text=compress(text,'.','d');

proc print;run;

Haikuo

Super Contributor
Posts: 276

how to extract perticular word.

What is the Use of d??

Respected Advisor
Posts: 3,156

how to extract perticular word.

Posted in reply to kuridisanjeev

'd' denotes 'digits', which tells compress() to get rid of any digits. Compress() is far more powerful than it looks at the first sight.

Super Contributor
Posts: 276

Re: how to extract perticular word.

if you use ' . '  in compress function,all the dot's is going to be deleted.

but i want to retain the dot's after Sanjeev and SRINATH words.

Respected Advisor
Posts: 3,156

Re: how to extract perticular word.

Posted in reply to kuridisanjeev

All right, then compress() can't help you on this case. The last resort would be allmighty prx:

data have;

input number text&$30.;

cards;

1   1.YOUR NAME IS 'SANJEEV.'

2     2.YOUR NAME IS 'SRINATH.'

1132    1132.YOUR NAME IS 'SANJEEV.'

23     23.YOUR NAME IS 'SRINATH.'

;

data want;

set have;

text=prxchange('s/\d+\.//',1,text);

proc print;run;

Haikuo

Edit: The reason I am not big fan of prx is that they are easy to WRITE, but impossible to READ. However, it seems that there is nothing they can't do in term of string manipulation, I have updated raw data to show its' power.

Super User
Posts: 5,498

Re: how to extract perticular word.

Posted in reply to kuridisanjeev

It's a little messy, but it can be crammed into one statement:

text = substr(text, length(scan(text, 1, '.'))+2);

This assumes that every observation needs to remove something from the beginning of TEXT, so that IF/THEN is not required.

Good luck.

Edited:

Actually, this is a little safer (as well as simpler) in case there is are spaces between the number and the dot:

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

Super Contributor
Posts: 276

how to extract perticular word.

Posted in reply to Astounding

sorry i didn't get you..

for 2nd observation,substr works like this..

scan(text,1,'.') value is 2.

so

text=substr(text,2+2)

which means text=substr(text,4).

So i think above logic not suitable for my requirement...

Correct me if i did any thing wrong..

Thank you..

Super User
Posts: 5,498

Re: how to extract perticular word.

Posted in reply to kuridisanjeev

K,

See my revised approach, for a simpler, better way.  But note that the original approach should have generated the equivalent of:

text = substr(text, length("2") +2)

Good luck.

Super Contributor
Posts: 276

Re: how to extract perticular word.

Posted in reply to Astounding

I think after length function we need add ',' right????

Super User
Posts: 5,498

Re: how to extract perticular word.

Posted in reply to kuridisanjeev

No, no comma.  The idea is that we'll specify just 2 arguments to SUBSTR. When the third argument is omitted, SUBSTR will take all remaining characters.  So we really need to compute the second argument, telling SUBSTR where to begin.

Respected Advisor
Posts: 3,156

Re: how to extract perticular word.

Posted in reply to Astounding

It would be a little less brain-twisting if we just first to use compress() to get rid of the digits and then substr from the second char.

data have;

input number text&$30.;

cards;

1   1.YOUR NAME IS 'SANJEEV.'

2     2.YOUR NAME IS 'SRINATH.'

1132    1132.YOUR NAME IS 'SANJEEV.'

23     23.YOUR NAME IS 'SRINATH.'

;

data want;

set have;

text=substr(compress(text,,'d'),2);

proc print;run;

Regards,

Haikuo

Super User
Posts: 5,498

Re: how to extract perticular word.

Hai.kuo,

Yes, if you are willing to rely on removing all digits.  You wouldn't want to encounter:

25.YOUR NAME IS 'R2D2.'

Smiley Happy

Respected Advisor
Posts: 3,156

Re: how to extract perticular word.

Posted in reply to Astounding

LOL. I have to agree. Even I have never seen a human name embeded with number (inmates id not included), but like John Maccain just said, 'if you live long enough, anything can happen'.

Haikuo

Super Contributor
Posts: 276

Re: how to extract perticular word.

Posted in reply to Astounding

oh sorry.

i made some wrong assumptions while understanding the logic..

Now i got it..

Thank you very much...

Ask a Question
Discussion stats
  • 18 replies
  • 558 views
  • 6 likes
  • 7 in conversation