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-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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.

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