DATA Step, Macro, Functions and more

substr function question

Reply
New Contributor
Posts: 4

substr function question

 

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

SAS Employee
Posts: 30

Re: substr function question

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!
;
New Contributor
Posts: 4

Re: substr function question

Posted in reply to GinaRepole

Thank you all for your quick answers and usefull tips!!!!

Super User
Posts: 6,638

Re: substr function question

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';

 

New Contributor
Posts: 4

Re: substr function question

Posted in reply to Astounding

Thanks for info, very usefull!!!!

PROC Star
Posts: 254

Re: substr function question

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.

New Contributor
Posts: 4

Re: substr function question

All these are very usefull tips, thanks a lot for quick answer!

Valued Guide
Posts: 560

Re: substr function question

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;
Thanks,
Suryakiran
Ask a Question
Discussion stats
  • 7 replies
  • 184 views
  • 1 like
  • 5 in conversation