BookmarkSubscribeRSS Feed
sabuni
Calcite | Level 5

 

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

7 REPLIES 7
GinaRepole
SAS Employee

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!
;
sabuni
Calcite | Level 5

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

Astounding
PROC Star

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

 

sabuni
Calcite | Level 5

Thanks for info, very usefull!!!!

s_lassen
Meteorite | Level 14

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.

sabuni
Calcite | Level 5

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

SuryaKiran
Meteorite | Level 14

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

How to Concatenate Values

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 7 replies
  • 995 views
  • 1 like
  • 5 in conversation