SAS Programming

DATA Step, Macro, Functions and more
BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
SarahDew
Obsidian | Level 7

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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
andreas_lds
Jade | Level 19

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;

 

View solution in original post

5 REPLIES 5
Jagadishkatam
Amethyst | Level 16

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;
Thanks,
Jag
SarahDew
Obsidian | Level 7

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?

 

 

andreas_lds
Jade | Level 19

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;

 

DanielLangley
Quartz | Level 8

@andreas_ldsI would suggest modifying

 

Word = scan(Company, i);
/*TO*/
Word = upcase( scan(Company, i) );

 

andreas_lds
Jade | Level 19

@DanielLangley wrote:

@andreas_ldsI would suggest modifying

 

Word = scan(Company, i);
/*TO*/
Word = upcase( scan(Company, i) );

 


Well, of course, good idea.

sas-innovate-white.png

Our biggest data and AI event of the year.

Don’t miss the livestream kicking off May 7. It’s free. It’s easy. And it’s the best seat in the house.

Join us virtually with our complimentary SAS Innovate Digital Pass. Watch live or on-demand in multiple languages, with translations available to help you get the most out of every session.

 

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 5 replies
  • 2188 views
  • 3 likes
  • 4 in conversation