BookmarkSubscribeRSS Feed

Hi

I need to know how to create a substring please.

The value I have is

428552LCOR42

But I need to remove the 'LC' (7&8) to show as below, any ideas please?

428552OR42

4 REPLIES 4
Loko
Barite | Level 11

Hello,

data _null_;

a='428552LCOR42';

b=compress(a,'LC');

put b=;

run;

RW9
Diamond | Level 26 RW9
Diamond | Level 26

Or: b=tranwrd(a,'LC','');

dcruik
Lapis Lazuli | Level 10

Not knowing how dynamic you need the substring to be when removing it, but if there is just one instance of "LC" in your field value, and that's all you are looking to remove, I would suggest the compress() function.

newvar=strip(compress(oldvar,"LC"));

This will eliminate any "L" or "C" in your original variable, and remove any leading or trailing blanks with the strip() function.

Hope this helps!

MadhuKorni
Quartz | Level 8

If you want to remove only L&C in the String the following code will help.

data Sample;

Have='428552LCOR42';

Want=compress(Have,'LC');

run;

IF you want to remove the 7th and 8th characters from the entire variable then

data Want;

set Have;

wantVar=strip(substr(haveVar,1,6)||substr(haveVar,9));

run;

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 4 replies
  • 440 views
  • 0 likes
  • 5 in conversation