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!

Trim data by x characters

Status
Not open for further replies.

mikeymay

Programmer
May 2, 2007
10
GB
I have a dataset that holds a variable with a prefix code prior to the
description.

I want to trim the variable by the number of characters the prefix
code has but the prefix code can be 4, 5, 6, 7 or 8 characters long.


Is there a way of trimming the data using a procedure?


Thanks

 
There might be. Can you post what some sample data might look like?

The first thing I think of is the substr() function:
SUBSTR(string, position<,length>)

ex, substr("fastforward",5,8) would return "forward".

But seeing the data you have to work with will help.

~Dave
 
I'm intrigued, is the prefix code held in the same variable as the description? If you want to tease out the description - provided the description has no numbers - you could use the compress function. However, if you simply want to extract the text after a code that satisfies your rule of being between 4 and 8 digits in length then you may have to resort to regular expressions:

Code:
data x;
   input string $1-40;
   after_code = prxchange('s/^\d{4,8}//',-1,string);
   cards;
3454IHaveFourNumbers
24344IHave5Numbers
34322244IhaveEightNumbers
   ;run;
proc print;run;

output:

Code:
                       Obs             string                 after_code

                         1     3454IHaveFourNumbers         IHaveFourNumbers
                         2     24344IHave5Numbers           IHave5Numbers
                         3     34322244IhaveEightNumbers    IhaveEightNumbers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top