BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
slolay
Fluorite | Level 6

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

1 ACCEPTED SOLUTION
11 REPLIES 11
slolay
Fluorite | Level 6

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

slolay
Fluorite | Level 6

Thanks Art

Works a treat!

Steve

MikeZdeb
Rhodochrosite | Level 12

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

Tom
Super User Tom
Super User

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

Ksharp
Super User

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

slolay
Fluorite | Level 6

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

Ksharp
Super User

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

slolay
Fluorite | Level 6

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

ScottBass
Rhodochrosite | Level 12

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...


Please post your question as a self-contained data step in the form of "have" (source) and "want" (desired results).
I won't contribute to your post if I can't cut-and-paste your syntactically correct code into SAS.

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
  • 11 replies
  • 4930 views
  • 0 likes
  • 6 in conversation