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

hackathon24-white-horiz.png

2025 SAS Hackathon: There is still time!

Good news: We've extended SAS Hackathon registration until Sept. 12, so you still have time to be part of our biggest event yet – our five-year anniversary!

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