BookmarkSubscribeRSS Feed
BRKS
Quartz | Level 8

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

5 REPLIES 5
mkeintz
PROC Star

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.

--------------------------
The hash OUTPUT method will overwrite a SAS data set, but not append. That can be costly. Consider voting for Add a HASH object method which would append a hash object to an existing SAS data set

Would enabling PROC SORT to simultaneously output multiple datasets be useful? Then vote for
Allow PROC SORT to output multiple datasets

--------------------------
ballardw
Super User

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.

ChrisNZ
Tourmaline | Level 20

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

 

ChrisNZ
Tourmaline | Level 20

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;

 

 

 

 

BRKS
Quartz | Level 8

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

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
  • 5 replies
  • 777 views
  • 0 likes
  • 4 in conversation