DATA Step, Macro, Functions and more

renaming datasets

Reply
Frequent Contributor
Posts: 75

renaming datasets

Hello all-

lets say I have 5 data sets, data1, data2, ... , data5 and I also have a 5 dimensional vector x=(3,5,6,7,9).

Is it possible to rename my data sets according to the numbers in my x vector? So I would get data3, data5, data6, etc. I'm sure I can do with with a macro but dont know how to proceed.

George
Super Contributor
Posts: 673

Re: renaming datasets

Can you share what you are trying to achieve?
Frequent Contributor
Posts: 75

Re: renaming datasets

I am using the rci option in proc mixed to give me the inverse choleski matrix of each subject used in proc mixed.

So for example if i use subjects 3,5,6,7, and 10 to run the analysis then rci=1 would give me the matrix of the first subject used (in this case subject 3), rci=2 would be the second subject (in this case subject 5), etc,

so in proc mixed when i specify:
proc mixed;
model ..... / rci=1 to 5;
repeated ... / ....;
ods output InvCholR(match_all)=data1;
run;


the ods output will create data1,data2,...,data5 which are the 5 matrices. I just want to rename the matrices to coincide with the actual subject numbers (in this case 3,5,6,...)

i found i can use

proc datasets;
change data1=data3;
run;


to rename data1 to data3. now i need to know how to run a macro that would take the data set and rename it. something like;

%macro rename (numb_subj,ind);
do i=1 to numb_subj;
proc datasets;
change data&i=data&ind[&i];
run;
end;
%mend rename


where ind is the index of subject numbers, for this example ind=(3,5,6,7,10)
Respected Advisor
Posts: 4,173

Re: renaming datasets

Hi

More elegant than renaming data set would be to solve this issue already on the level of ODS. Not sure if and how this would be done.

The code below should do what you asked for:


data data data1 data2 data3;
a=1;
run;


%let subjectlist=2,3,5;

proc sql;
create view OldDataSets as
select memname as OldName length=32 format=$32.
from dictionary.tables
where libname='WORK' and memname like 'DATA%' and memtype='DATA'
and input(compress(memname,,'kd'),8.) le countw("&subjectlist", ',')
and not missing(input(compress(memname,,'kd'),8.))
order by memname
;
quit;

filename temp1 temp;
data _null_;
set OldDataSets end=last;
/* file print;*/
file temp1;

if _n_=1 then
do;
put 'proc datasets lib=work nolist nowarn;';
end;

NewName=cats(compress(OldName,,'d'),'n',scan("&subjectlist",_n_,','));
put ' delete ' NewName ';';
put ' change ' OldName ' = ' NewName ';';

if last then
do;
put 'quit;';
end;
run;

%include temp1;

filename temp1 clear;


HTH
Patrick Message was edited by: Patrick
Frequent Contributor
Posts: 75

Re: renaming datasets

Thanks Pat!!
Valued Guide
Posts: 634

Re: renaming datasets

You have Patrick's solution, but as an aside to answer one of you questions you have the macro rename. Here it is rewritten to perform the renames you suggested
[pre]
%macro rename (numb_subj=,ind=);
proc datasets;
%do i=1 %to &numb_subj;
change data&i=datax%left(%scan(&ind,&i));
%end;
quit;
%mend rename;
[/pre]
Where the macro call becomes something like:
[pre]
%rename(numb_subj=5,ind=2 3 5 7 9)
[/pre]
notice that the DO loop is now %DO and I have changed the statements a bit.
HOWEVER there could be overlaps in the names as you had them so I have added an X to the new name. Message was edited by: ArtC
PROC Star
Posts: 1,760

Re: renaming datasets

Or if you know you have no overlaps:

[pre]
%macro rename_ds(order);
%local i;
proc datasets nolist;
%do i=1 %to %sysfunc(countw(&order));
change data&i=____&i ;
%end;
%do i=1 %to %sysfunc(countw(&order));
change ____&i=data%scan(&order,&i) ;
%end;
quit;
%mend rename_ds;

%rename_ds(2 3 4 1)
[pre]
Ask a Question
Discussion stats
  • 6 replies
  • 169 views
  • 0 likes
  • 5 in conversation