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-2026-white.png



April 27 – 30 | Gaylord Texan | Grapevine, Texas

Registration is open

Walk in ready to learn. Walk out ready to deliver. This is the data and AI conference you can't afford to miss.
Register now and lock in 2025 pricing—just $495!

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