BookmarkSubscribeRSS Feed
SciFiGuy0
Obsidian | Level 7


I've got some code which creates an acronym from a department name:

acronym=propcase(prxchange('s/ for | of the | \& |, cio/ /io',1,FullDepartmentName));

acronym=prxchange('s/[a-z ]+//',-1,acronym);

It's used in a data step and creates a variable called: acronym - which is the shortened version of FullDepartmentName.

For example: FullDepartmentName="Department of the Interior & Toxicology for Cancer"  would create a variable acronym='DITC"

With all the quotes, parens, ampersands, etc,  how would i turn this into a macro?  Something like:

%macro acronym(FullDepartmentName);

%let acronym=;;;

%put('acronym',acronym);

%mend;

Note: I was trying to see how little code i could use to create the acronym.   If you think you can do it with less code, I'd like to see it  Smiley Happy

5 REPLIES 5
Reeza
Super User

Wouldn't you use PROC FCMP instead of a macro in this case?

or in 9.3, even a format?

SciFiGuy0
Obsidian | Level 7

Wow - that would be much easier.  is there a format for acronym which takes out the "and", "the", "&" and other irrelevant words??

Reeza
Super User

I think you're looking for acronymns instead of anagrams?

But no, there isn't a format, but you can put a function into a format.

art297
Opal | Level 21

: Not sure if this is exactly what you are looking for, but it may at least be close:

%macro acronym;

  acronym=propcase(prxchange('s/ for | of the | \& |, cio/ /io',-1,department));

  acronym=prxchange('s/[a-z ]+//',-1,acronym);

%mend acronym;

data have;

  informat department $80.;

  input department &;

  %acronym

  cards;

Department of the Interior & Toxicology for Cancer

Office for Something

Statistical Analysis System

;

SteveNZ
Obsidian | Level 7

If case might be an issue then this works:

%macro acronym;

    acronym=prxchange('s/\bof\b|\bthe\b|&|\bfor\b//io',-1,department);

    acronym=upcase(compress(prxchange('s/(?<=\b\w{1})\w*//io',-1,acronym),,'s')) ;

%mend ;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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
  • 1328 views
  • 3 likes
  • 4 in conversation