BookmarkSubscribeRSS Feed
jmmedina25
Obsidian | Level 7

Hi! I'd like to rename variables using a macro-

So I have variables for 1-10 programs and I need to rename them the same name based on their suffix but I need to do the rename given prefix= prog1-prog10.

 

Example:

Prog1_ProgramCat_TEXT= prog_name

Prog2_ProgramCat_TEXT=prog_name

Prog3_ProgramCat_TEXT=prog_name

.. all the way to the prefix= Prog10_

 

(All info is contained in variable answers; Please let me know if you need more data info)

 

So here's my crazy attempt to make a code for this:

 

%macro rename(prgname=, prgchall=, prgven=, prgcost=, prgday=, prgdur=, prgdurun=, prgeval=, prgevent=, prgnum=, prgcat=, prgfreq=, prgsite=, prgstart=, prgconsid=);
data programcondensing;

	set WORK.TestExport (keep=agency prog1_: prog2_: prog3_: prog4_: prog5_: prog6_: prog7_: prog8_: prog9_: prog10_: responseid);
		 %rename &prgconsid = prog_considerations
				&prgname= prog_name
				&prgchall = prog_challenges
				&prgven = prog_vendor
				&prgcost= prog_cost
				&prgday = prog_date
				&prgdur = prog_duration
				&prgdurun = prog_duration_unit
				&prgeval = prog_eval
				&prgevent = prog_type
				&prgnum = prog_Num
				&prgcat = prog_cat
				&prgfreq = prog_freq
				&prgsite = prog_site
				&prgstart = prog_start;
			
run;
%mend rename;
options mlogic;

%rename (prgname=:ProgramCat_TEXT, prgchall= :_2, prgven= :_4, prgcost=_5, prgday=:_date_1, prgdur=:_duration, prgdurun=:_durationunit, prgeval=:_evalyesno, prgevent=:_eventtype, prgnum=:_numparticipants, prgcat=:_programcat, prgfreq=:_programfreq, prgsite= :_site_1, prgstart= :_starttime_1, prgconsid=:_1)


 

 

Please help:)

1 REPLY 1
PaigeMiller
Diamond | Level 26

Macro? Not needed. Macro variable? YES!

 

Your problem description isn't entirely clear, you can't rename all of them to prog_name as shown in the example

 

Here's the general outline of how to do this.

 

proc contents data=have noprint out=_contents_;
run;
proc sql noprint;
    select cats(name,'=','prog',scan(substr(name,4),1,'_'))
        into :renames separated by ' ' from _contents_
        where name eqt 'prog' and name ? '_ProgramCat_TEXT';
quit;
proc datasets library=work nolist;
    modify have;
    rename &renames;
    delete _contents_;
run;
quit;

 

--
Paige Miller

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
  • 1 reply
  • 1251 views
  • 1 like
  • 2 in conversation