I want to create a new variable (ABBR) that forms the abbreviation of another variable containing company names (Company) by selecting the first letters of the words in the name. However, I would like to avoid getting the first character of words like 'the" "for" "in" ...
For example:
THE INTERNATIONAL COMPANY FOR ELECTRICITY SUPPLY --> ICES
REGIONAL COMPANY OF DEVELOPMENT --> RCD
The goal is to match this with company names from another source which might contain only the abbreviation. This will not be flawless but might work on a small percentage of the unmatched cases.
PS: There might be a trick using the PROPCASE function, but as a beginner with SAS I have no idea how to even begin (underneath is how far I got). Probably an alternative option is using SCAN function in a way.
data want;
set have;
UpcaseFirst = Propcase(Company);
Abbr = Anyupper(UpcaseFirst);
run;
Another way of solving the problem:
data want;
set have;
length
Abbr $ 20
Word $ 100
;
do i = 1 to countw(Company);
Word = scan(Company, i);
if not (trim(Word) in ('OF', 'THE', 'FOR')) then do;
Abbr = cats(Abbr, first(Word));
end;
end;
drop i word;
run;
to accomplish the same we need to first compress or remove the words which are not part of the acronym like, 'the' 'for' 'of' etc.,
then we need to take the first letter from the remaining words which form the acronym and combine them to form the acronym.
I tried the same in the following code which derives the variable acronym which could be compare with the acronym from another source.
data have;
input text &:$200.;
countw=countw(text);
array cnt(*)$ char1-char100;
do i = 1 to countw;
cnt(i)=first(scan(prxchange('s/the|for|of//i',-1,text),i,''));
end;
acronym=cats(of char1-char100);
cards;
THE INTERNATIONAL COMPANY FOR ELECTRICITY SUPPLY
REGIONAL COMPANY OF DEVELOPMENT
;
run;
This works partially but can it be that this code also removes a part when a string starts with a "banned" word?
So for example turns:
OFFICIAL COMPANY OF COMPUTERS first to FICIAL COMPANY COMPUTERS to acronym FCC instead of OCC?
Another way of solving the problem:
data want;
set have;
length
Abbr $ 20
Word $ 100
;
do i = 1 to countw(Company);
Word = scan(Company, i);
if not (trim(Word) in ('OF', 'THE', 'FOR')) then do;
Abbr = cats(Abbr, first(Word));
end;
end;
drop i word;
run;
@andreas_ldsI would suggest modifying
Word = scan(Company, i);
/*TO*/
Word = upcase( scan(Company, i) );
@DanielLangley wrote:
@andreas_ldsI would suggest modifying
Word = scan(Company, i);/*TO*/
Word = upcase( scan(Company, i) );
Well, of course, good idea.
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.