DATA Step, Macro, Functions and more

How to turn this into a macro?

Reply
Occasional Contributor
Posts: 18

How to turn this into a macro?


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

Super User
Posts: 17,868

Re: How to turn this into a macro?

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

or in 9.3, even a format?

Occasional Contributor
Posts: 18

Re: How to turn this into a macro?

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

Super User
Posts: 17,868

Re: How to turn this into a macro?

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.

PROC Star
Posts: 7,363

Re: How to turn this into a macro?

: 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

;

Frequent Contributor
Posts: 87

Re: How to turn this into a macro?

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 ;

Ask a Question
Discussion stats
  • 5 replies
  • 232 views
  • 3 likes
  • 4 in conversation