DATA Step, Macro, Functions and more

Removing a String from a Char Variable

Accepted Solution Solved
Reply
Contributor
Posts: 51
Accepted Solution

Removing a String from a Char Variable

Hi

I have a list of values which have '_STD' appended to the end of them.  I need to remove this suffix so that I am left with just the beginning part

eg

name= SAW_PRICE_STD want SAW_PRICE

I tried the compress function but doesn't seem it can compress out strings, only charachters.  I tried somehting like

name2=compress(name, "_STD") but it gives me 'AWPRICE'

Is there a function which can remove whole strings? or a solution to the above.  Could use tranwrd but would be good to know if what I am looking for is already available in one function

Help greatfully appreciated

Thanks

Steve


Accepted Solutions
Solution
‎08-30-2011 09:32 AM
PROC Star
Posts: 7,364

Removing a String from a Char Variable


All Replies
PROC Star
Posts: 7,364

Removing a String from a Char Variable

Contributor
Posts: 51

Removing a String from a Char Variable

Hi again Art

Thanks for this, but doesn't seem to be there for 9.1.3..wich is what i am working on.  Is it a 9.2 function?

I got around it using

name2=reverse(substr(left(reverse(name)),5));

but would be good to know if there is a function for 9.1.3 which will do what transtrn does.  Or even to strip strings out.

Cheers

steve

Ps sorry not got back to you on another post you wrote.  It's a lot of code to go through at the moment for me and looks like some new stuff for me in there.  Will try Art, hopefully. It is on the radar but drifting out of view. Sorry

Cheers

Solution
‎08-30-2011 09:32 AM
PROC Star
Posts: 7,364

Removing a String from a Char Variable

Contributor
Posts: 51

Removing a String from a Char Variable

Thanks Art

Works a treat!

Steve

Valued Guide
Posts: 765

Re: Removing a String from a Char Variable

Hi ... you got an answer from Art, but given that you tried ...

name2=reverse(substr(left(reverse(name)),5));

here's another function idea.  Just make the format in the PUT statement  4 less than the length of NAME

data x;

input name $20.;

datalines;

SAW_PRICE_STD

XYZ_STD

MIKE_STD

ABCDEFGHI_STD

;

run;

data x;

set x;

name = left(put(right(name),$16.));

run;

proc print data=x;

run;

name

SAW_PRICE

XYZ

MIKE

ABCDEFGHI

Super User
Super User
Posts: 6,502

Re: Removing a String from a Char Variable

The problem with TRANWRD is that it will replace all instances in the string.  So X_STD_Y_STD would become X_Y.

You might want to test that _STD are the last four characters of the name.

if reverse('_STD') =: reverse(trim(name)) then ...

or

if scan(name,-1,'_') = 'STD' then

Super User
Posts: 9,687

Re: Removing a String from a Char Variable

Function substr() would be helpful.

data x;
input name $20.;
datalines;
SAW_PRICE_STD
XYZ_STD
MIKE_STD
ABCDEFGHI_STD
;
run;
data want;
 set x;
 new=substr(name,1,length(name)-4);
run;

Ksharp

Contributor
Posts: 51

Removing a String from a Char Variable

Thanks everyone!

Good suggestions and things to consider..and learn

_STD only occurs once in the names..so they say ;-)  .. so may be an option to use a conditioning if statement with the tranwrd function or something like what Ksharp and MikeZDeb said. Or somthing else..but cheers for the replies..so many options when once there was none!

Regards
Steve

Super User
Posts: 9,687

Removing a String from a Char Variable

Then use:

data x;
input name $20.;
datalines;
SAW_PRICE_STD
XYZ_STD
MIKE
ABCDEFGHI
;
run;
data want;
 set x;
 new=substr(name,1,ifn(find(name,'_STD'),find(name,'_STD')-1,length(name))   );
run;

Ksharp

Contributor
Posts: 51

Removing a String from a Char Variable

Hi All

Just to let you know I went for:

*--- check that the last 4 charachters are 'STD_';

              if left(reverse(name))=: 'DTS_'

              then do;

                  name=substr(name,1,length(name)-4);

                  output stdvars;

              end;

Cheers for all your suggestions

Steve

Super Contributor
Posts: 377

Removing a String from a Char Variable

Or even:

data have;

input name $20.;

datalines;

SAW_PRICE_STD

XYZ_STD

ABC_STD_DEF

std_123

xxx_std

MIKE

ABCDEFGHI

;

run;

data want;

set have;

new=prxchange("s/_STD$//io",-1,trim(name));

run;

I'm really learning to like the PRX* functions...

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 11 replies
  • 283 views
  • 0 likes
  • 6 in conversation