NUMBER | TEXT |
---|---|
1 | 1.YOUR NAME IS 'SANJEEV.' |
2 | 2.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..
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
What is the Use of d??
'd' denotes 'digits', which tells compress() to get rid of any digits. Compress() is far more powerful than it looks at the first sight.
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.
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.
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);
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..
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.
I think after length function we need add ',' right????
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.
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
Hai.kuo,
Yes, if you are willing to rely on removing all digits. You wouldn't want to encounter:
25.YOUR NAME IS 'R2D2.'
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
oh sorry.
i made some wrong assumptions while understanding the logic..
Now i got it..
Thank you very much...
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!
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.