BookmarkSubscribeRSS Feed
kuridisanjeev
Quartz | Level 8
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..

18 REPLIES 18
Haikuo
Onyx | Level 15

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

kuridisanjeev
Quartz | Level 8

What is the Use of d??

Haikuo
Onyx | Level 15

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

kuridisanjeev
Quartz | Level 8

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.

Haikuo
Onyx | Level 15

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.

Astounding
PROC Star

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);

kuridisanjeev
Quartz | Level 8

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

Astounding
PROC Star

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.

kuridisanjeev
Quartz | Level 8

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

Astounding
PROC Star

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.

Haikuo
Onyx | Level 15

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

Astounding
PROC Star

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

Haikuo
Onyx | Level 15

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

kuridisanjeev
Quartz | Level 8

oh sorry.

i made some wrong assumptions while understanding the logic..

Now i got it..

Thank you very much...

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 Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 18 replies
  • 1554 views
  • 6 likes
  • 7 in conversation