BookmarkSubscribeRSS Feed
trekvana
Calcite | Level 5
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
6 REPLIES 6
SASPhile
Quartz | Level 8
Can you share what you are trying to achieve?
trekvana
Calcite | Level 5
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)
Patrick
Opal | Level 21
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
trekvana
Calcite | Level 5
Thanks Pat!!
ArtC
Rhodochrosite | Level 12
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
ChrisNZ
Tourmaline | Level 20
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]

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 6 replies
  • 808 views
  • 0 likes
  • 5 in conversation