Hi @gurre003
I know all too well how it is to deal with take-as-is-and-deliver-as-wanted data. Here is another (tested) solution working after similar lines. It processes all numeric variables with a given name pattern, and if you have more than one group of numeric variables, the if prxmatch ... end section can be repeated and ajusted for each group.
If you have one or more groups of character variables, you must declare a character variable array also and repeat the whole do loop to deal with the char array.
Proc datasets changes names in situ, so I strongly adwise to make a copy in work and then replace input when everything works OK.
* Make test input data;
data have;
attrib
ID length=8
Department length=$30
MBA_1_VALUE length=8 label="Monthly Benefit Amount, 1/1962"
MBA_2_VALUE length=8 label="Monthly Benefit Amount, 2/1962"
MBA_3_VALUE length=8 label="Monthly Benefit Amount, 3/1962"
;
ID = 1; Department = 'A'; MBA_1_VALUE = 1; MBA_2_VALUE = 2; MBA_3_VALUE = 3; output;
ID = 2; Department = 'B'; MBA_1_VALUE = 11; MBA_2_VALUE = 22; MBA_3_VALUE = 33; output;
run;
* Rename variables in data set;
data _null_; set have (obs=1) end=end;
array nums _numeric_;
if _N_ = 1 then call execute('proc datasets nolist lib=work; modify have;');
do i = 1 to dim(nums);
vname = vname(nums{i});
if prxmatch("/MBA_\d+_VALUE/",vname) then do;
d = input(scan(vlabel(nums{i}),-1,' '),anydtdte.);
ren = 'rename ' || trim(vname) || ' = mba_' || put(d,monname3.) || '_' || put(d,year2.)||';';
call execute(ren);
end;
end;
if end then call execute('run; quit;');
run;
... View more