- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
To whom it may concern,
I have an issue, I must extract only part of a string variable. The string variable is very diverse (ie below).
‘ORGINAL’ String variable | ‘TO BE’ New string variable |
BESLAGNR: 31099488 | BE31099488-0101 |
BESLAGNR: 310-00147 | BE31000147-0101 |
BESLAGNR: 10012390 - LANGE METHODE! | BE10012390-0101 |
I found your address on a tuto on, the internet. Would you be able to help me with the current issue?
Thanking you in advance,
Kind regards,
Sarah
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
In this situation you're going to need more than just the SUBSTR() function. Here's some sample code I wrote to mock up your scenario that gives the results you asked for:
data want;
length original $ 50;
infile datalines DLM=",";
input original $;
number = SCAN(original, 2, " ");
myString = CATS("BE", COMPRESS(number, "-"), "-0101");
datalines;
BESLAGNR: 31099488
BESLAGNR: 310-00147
BESLAGNR: 10012390 - LANGE METHODE!
;
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thank you all for your quick answers and usefull tips!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
It depends on how much you know about the incoming values.
Do you always want "BE" a the beginning, or does it depend on the incoming string?
Do you always want "-0101" at the end?
Does the middle portion always contain all the numbers from the incoming string, or could there be numbers at the end of it that you want to ignore?
Here is one possibility:
new = substr(old, 1, 2) || compress(old, , 'kd') || '-0101';
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Thanks for info, very usefull!!!!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Assuming that you want to have the first two letters of the old variable in the beginning, then the number part, with dashes removed, then '-0101', this may work:
data have;
length original $ 50;
infile datalines DLM=",";
input original $;
datalines;
BESLAGNR: 31099488
BESLAGNR: 310-00147
BESLAGNR: 10012390 - LANGE METHODE!
;
data want;
set have;
retain NewVar ' -0101';
substr(NewVar,1,2)=Original;
substr(NewVar,3,8)=compress(scan(Original,2,' '),'-');
run;
In other words, the substring function is also very useful when assigning to part of a variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
All these are very usefull tips, thanks a lot for quick answer!
- Mark as New
- Bookmark
- Subscribe
- Mute
- RSS Feed
- Permalink
- Report Inappropriate Content
Hi,
Try this code if the string is not in specific pattern to use SUBSTR to extract numbers.
data have;
length original $ 50;
infile datalines DLM=",";
input original $;
datalines;
BESLAGNR: 31099488
BESLAGNR: 310-00147
BESLAGNR: 10012390 - LANGE METHODE!
;
RUN;
data want;
set have;
Str=COMPRESS(CAT(SUBSTR(Original,1,2),compress(original,compress(original," ",'nt'),'iat'),'-0101'));
run;
Suryakiran