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
Take a look at transtrn. http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a003254155.htm
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
try tranwrd see: http://support.sas.com/onlinedoc/913/docMainpage.jsp
Thanks Art
Works a treat!
Steve
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
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
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
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
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
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
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...
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.