Hi all,
I googled this topic and found a 2012 issue just like it where someone suggested adding a new function like this to the SAS Ballet. Since it's about 5 years later, I'm wondering if it made it to the ballet (if not, I'll put it there)?
I want to specify a particular character that I want stripped from beginning, end, or both of a character string field.
For example, I would like any records with a character value ending in a comma to have the comma stripped.
I know how to do it the long way, but it seems like a common enough need that a new function might be helpful to more folks than me.
Thanks so much, and sorry if it's out there already and I just missed it! 🙂
Barb
As long as there are no commas in the interior of the string, then you could use the SCAN function.
509 data x;
510 a=',,,word,,,';
511
512 w=scan(a,1,',');
513 put a= w=;
514 run;
a=,,,word,,, w=word
Not exactly a STRIP function, but possibly close enough.
You may need to provide examples of what you want if they are more complex. Are the values you are considering embedded inside a longer string? Do they occur at something that may be considered a "word boundary"?
You may consider looking into Proc FCMP if you want to move a bunch of possibly routine, tedious, long and ugly string functions out of the data step you that you can do something like: newvar = MySpecialString(oldvar); as a single call in the data step.
That way the function does what you need and not what someone else thinks you need.
Regular expressions do this easily.
Here, we remove any starting or ending comma or space; that's what the (,| ) group defines: comma or space.
data _null_;
STR1=',,aaaaa,aa, , ';
STR2=prxchange('s/^(,| )*(.*?)(,| )*$/$2/o',1,STR1);
putlog STR2= ;
run;
STR2=aaaaa,aa
This expression reads as follows:
1- match any number of spaces and commas right after the beginning of the string
^(,| )*
2- then match what ever else can be matched (but as little as possible in order maximize 1 and 3)
(.*?)
3- then match any number of spaces and commas right to the end of the string
(,| )*$
4- only keep 2- (i.e. discard starting and ending commas and spaces)
$2
You can even create the list of matched characters separately.
Here is a variation on the same theme:
%let list=%str(, );
data _null_;
STR1=', ,,aaaaa,aa, , ';
STR2=prxchange("s/^[&list]*(.*?)[&list]*$/$1/o",1,STR1);
putlog STR2= ;
run;
So you can easily create a macro or an fcmp function:
%macro stripchars(var,chars);
prxchange("s/^[&chars]*(.*?)[&chars]*$/$1/o",1,&var.)
%mend;
data _null_;
STR1=', ,,aaaa,aaa, , ';
STR1=%stripchars(STR1,%str(, ));
putlog STR1= ;
run;
Hi all,
Thanks for your assistance! Looks like there is not a function.
I'm all set and know how to work around this, and don't need any further help.
I was just wondering if there was a function to strip a specified charracter at the end of a string.
Barb
Available on demand!
Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.
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.