Hi,
I have a macro variable like below
%let studybegin_period =MDY(1,1,2015)
%let studyend_period =MDY(12,1,2015)
i have a dataset with column names like M1, M2...M12(for 12 months),i need to replace column names with values like
Jan2015,FEB2015....DEC2015.NOTE: the columns increase or decrese depending on the time period i have on the macro variables.
Please help me know how to go about it.Thanks!
Like this?
%let studybegin_period =MDY(1,1,2015);
%let studyend_period =MDY(12,1,2015) ;
%let studybegin_period =MDY(12,1,2015);
%let studyend_period =MDY(1,1,2015) ;
data HAVE;
length M1-M12 $1;
run;
data _null_;
length STR $800;
do until(MTH=&studyend_period);
I+1;
MTH=intnx('month', &studybegin_period ,ifn(&studybegin_period <&studyend_period, I-1, -I+1));
STR=catt(STR,' M',I,'=',put(MTH,monyy7.));
end;
call symputx('rename', STR);
run;
proc datasets;
modify HAVE;
rename &rename.;
quit;
NOTE: Renaming variable M1 to DEC2015.
NOTE: Renaming variable M2 to NOV2015.
NOTE: Renaming variable M3 to OCT2015.
NOTE: Renaming variable M4 to SEP2015.
NOTE: Renaming variable M5 to AUG2015.
NOTE: Renaming variable M6 to JUL2015.
NOTE: Renaming variable M7 to JUN2015.
NOTE: Renaming variable M8 to MAY2015.
NOTE: Renaming variable M9 to APR2015.
NOTE: Renaming variable M10 to MAR2015.
NOTE: Renaming variable M11 to FEB2015.
NOTE: Renaming variable M12 to JAN2015.
data have;
array x{12} m1-m12 (12*1);
run;
%let start=01jan2015;
proc transpose data=have(obs=0) out=temp;
var m:;
run;
data temp;
set temp;
retain n -1;
n+1;
name=put(intnx('month',"&start"d,n),monyy7.);
run;
data _null_;
set temp end=last;
if _n_=1 then
call execute('proc datasets library=work nolist nodetails; modify have; rename');
call execute(catx('=',_name_,name));
if last then call execute(';quit;');
run;
Don't miss out on SAS Innovate - Register now for the FREE Livestream!
Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.
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.