Help using Base SAS procedures

renaming

Reply
Regular Contributor
Posts: 161

renaming

hi
How do I rename all my variables when they just differ in their suffix. and rename the file at the same time/

Thanks

%let num=4

DATA Q_Matrix_MIss;
MODIFY Q_Matrix;
RENAME skills1-skills&num=att1-att#
run;
end;
Ex:
Super Contributor
Super Contributor
Posts: 365

Re: renaming

Hello R.A.G.,

I do not understand your syntax but renaming can be done with this simple macro:
[pre]%macro a(num=);
data r;
set i;
%do i=1 %to #
rename skills&i=att&i;
%end;
run;
%mend;
%a(num=2)
[/pre]
Sincerely,
SPR
Super User
Posts: 11,343

Re: renaming

Instead of creating additional datasets using a data step I would suggest looking into PROC DATASETS which lets you modify datasets and properties of the variables within the dataset.
Regular Contributor
Posts: 161

Re: renaming

Thank you and I'll look into the PROC as well.
Thanks again
Regular Contributor
Posts: 171

Re: renaming

Although a proc datasets solution might be more efficient, the rename statement from your original code is valid. There are two problems with the code you posted. Since you are creating a new dataset instead of modifying an existing dataset in place, you need to use a set statement instead of a modify statement. Also, the end statement is not needed.
N/A
Posts: 0

Re: renaming

hello,

a more general macro solution which takes advantage of dictionary tables:

[pre]
data test;
array skills{10};
do i=1 to 5;
do j=1 to 10;
skills{j}=ranuni(10);
end;
output;
end;
run;

proc sql noprint;
select name into :all_var separated by ' ' from dictionary.columns
where libname=upcase('work') and memname=upcase('test') and name contains 'skills';
quit;

%macro change(new_suf);

%let i=1;

%do %until(%qscan(&all_var,%eval(&i)) eq %nrquote());
proc datasets library=work nolist;
modify test;
rename %qscan(&all_var,%eval(&i))=%sysfunc(cats(&new_suf,&i));
quit;
%let i=%eval(&i+1);
%end;


%mend change;

%change(att)
[/pre]

Marius
Ask a Question
Discussion stats
  • 5 replies
  • 176 views
  • 0 likes
  • 5 in conversation