BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
somebody
Lapis Lazuli | Level 10

I have a variable that contains company names. However, if a company name contains abbreviation, it creates space between the letters. For example, instead of "ACCO Brand Corp", the variable is " A C C O BRAND CORP". My question is, is there a way to group the ACCO together? Thanks

1 ACCEPTED SOLUTION

Accepted Solutions
Kurt_Bremser
Super User

With "conventional" data step means:

data test;
mystr = ' A C C O BRAND CORP';
*mystr = left(mystr);
i = 2;
do until (i >= length(mystr) or x>10);
  if substr(mystr,i,1) ne ' ' and substr(mystr,i-1,1) = ' ' and substr(mystr,i+1,1) = ' '
  then do;
    if i > 2
    then mystr = substr(mystr,1,i-2) !! substr(mystr,i);
	else mystr = substr(mystr,2);
  end;
  else i + 1;
end;
run;

View solution in original post

6 REPLIES 6
Amir
PROC Star

Please share the code you are referring to when you say "it creates space between the letters".

 

1) Is the original data in a SAS data set?

 

2) Is the output data in a SAS data set?

 

3) Please share the log with any messages.

 

4) Please share some code and that can be run as is to demonstrate the problem, e.g., using dataines in a data step.

 

 

Thanks,

Amir.

FreelanceReinh
Jade | Level 19

Hi @somebody,

 

Try this:

data have;
company=" A C C O BRAND CORP";
run;

data want;
set have;
company=left(prxchange('s/(?<=(\b[A-Z])) (?=([A-Z]\b))//o',-1,propcase(company)));
run;

The Perl regular expression deletes single blanks which are preceded by a single uppercase letter (which is separated from preceding text, if any, by a word boundary) and followed by another single uppercase letter (which is separated from subsequent text, if any, by a word boundary).

Kurt_Bremser
Super User

With "conventional" data step means:

data test;
mystr = ' A C C O BRAND CORP';
*mystr = left(mystr);
i = 2;
do until (i >= length(mystr) or x>10);
  if substr(mystr,i,1) ne ' ' and substr(mystr,i-1,1) = ' ' and substr(mystr,i+1,1) = ' '
  then do;
    if i > 2
    then mystr = substr(mystr,1,i-2) !! substr(mystr,i);
	else mystr = substr(mystr,2);
  end;
  else i + 1;
end;
run;
Ksharp
Super User
data test;
mystr = 'A C C O BRAND CORP';
temp=prxchange('s/(\w{2,})/ $1 /',-1,mystr);
want=compbl(prxchange('s/ (\w) /$1/',-1,temp));
run;
somebody
Lapis Lazuli | Level 10

Thanks, your method works too. just one minor comment is that the result has a space at the start of the string.

Ksharp
Super User

Add one more function LEFT() around it .

SAS Innovate 2025: Call for Content

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 16. Read more here about why you should contribute and what is in it for you!

Submit your idea!

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
  • 6 replies
  • 800 views
  • 3 likes
  • 5 in conversation