02-19-2018 03:49 PM
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: 10012390 - LANGE METHODE!
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,
02-19-2018 04:19 PM
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! ;
02-19-2018 04:19 PM
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';
02-20-2018 03:39 AM
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.
02-26-2018 04:55 PM
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;