01-04-2017 04:00 PM
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! :-)
01-04-2017 04:40 PM
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.
01-04-2017 06:40 PM
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.
01-04-2017 09:28 PM - edited 01-04-2017 09:28 PM
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;
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)
01-04-2017 09:39 PM
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;
01-05-2017 09:30 AM
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.