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
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!
;
Thank you all for your quick answers and usefull tips!!!!
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';
Thanks for info, very usefull!!!!
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.
All these are very usefull tips, thanks a lot for quick answer!
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;
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.