Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

deleting part of the string

Status
Not open for further replies.

r3p0rt3r

IS-IT--Management
Feb 1, 2010
2
JP
Hello,

I hope someone could help me with my dilemna... :(
I have a set of table with a field called 'FileName_ID'. This field has sets of values which has no definite length but at the end of each entries are series of 6 numbers enclosed in a parenthesis().
I needed to remove the number groups including the parenthesis(), so I figured to use the RIGHT function to have them all aligned and start counting from the right. Now, what cannot find out is how I can remove those unwanted part of the entries?

Example:
FileName_ID: August_ES_AMBP_Default_LeftNews_1_UK_Visa (278414)
The result needed: August_ES_AMBP_Default_LeftNews_1_UK_Visa


Thank you soooo much in advance for all the help :)
 
Here is my solution, hope this helps.

Code:
/* create test dataset */
data have;
input FileName_ID $80.;
datalines;
August_ES_AMBP_Default_LeftNews_1_UK_Visa (278414)
September_ES_AMBP_Default_LeftNews_1_UK_Visa (278415)
October_ES_AMBP_Default_LeftNews_1_UK_Visa (278416)
November_ES_AMBP_Default_LeftNews_1_UK_Visa (278417)
;
run;

/* find position of the first ( */
data have;
set have;
pos = index(FileName_ID,'(');
run;

/* substring the field using the position to define the length */
data want;
set have;
NewFileName_ID = substr(FileName_ID, 1, pos-1);
run;

Dave
 
[ponytails2] Thank you soooo much Dave! This does the trick [2thumbsup]
 
Ooooh, I did something like this a while ago to remove strings inside parentheses from the middle of a bigger string. You obviously don't need this as Dave's method worked for you, but for completeness (and I like showing off my code) here's my method.

Code:
	    if indexc(some_string,'(') > 0 and indexc(some_string,')') > 0 then
		do;
			start = indexc(some_string,'(');
			end = indexc(some_string,')');
			len = length(some_string);
			cut = substr(some_string,start,end-start);
			if len = end then
				remainder = substr(some_string,1,start-1);
			else
				remainder = catt(substr(some_string,1,start-1),(substr(some_string,end+1,len-end)));
	    end;
The only downside to this is that if there are 2 sets of brackets within the string (either "this ((sort of)) thing" or "this (sort) of (thing)") only the first set brackets get removed. In the first case you'll be left with
Code:
"this ) thing"
and in the second case you'll be left with
Code:
"this of (thing)"
Maybe someone else can play with this to make it bullet proof. I can see a way to do this, using a do loop which keeps iterating throguh removing the pairs of brackets till there's none left, and removing any remaining close brackets at the end.

Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Just realised there is a line of code missing fromt he end of my code snippet there.
You'll also want
Code:
		else remainder = some_string;
otherwise it'll be empty if there's no parentheses in your original text string. :)


Chris
Business Analyst, Code Monkey, Data Wrangler.
SAS Guru.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top