BookmarkSubscribeRSS Feed
srdhoble
Calcite | Level 5

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!

2 REPLIES 2
ChrisNZ
Tourmaline | Level 20

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.

 

Ksharp
Super User
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;

sas-innovate-2024.png

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.

 

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
  • 2 replies
  • 1020 views
  • 0 likes
  • 3 in conversation