Statistical programming, matrix languages, and more

using a macro to reference a vector element

Reply
Frequent Contributor
Posts: 75

using a macro to reference a vector element

Can proc iml be used within a macro to reference a specific element of a vector? Since this is quite vague I'll give an example:

Suppose I have 3 data sets: data1, data2, data3 and I want to rename then data3, data5, data7. I can do this with proc datasets:

proc datasets;
change data1=data3;
run;


and so forth. Now I want to generalize this with a macro and an index vector within proc iml. My conceptual code (that unfortunately doesnt work) is:

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


Here is my example ind={3 5 7}. So what I want to do is pass the number of data sets into the macro (in this case 3) and also an index vector. Then proc datasets will change the names according to the elements in the index vector. Can this be done?

Cheers
SAS Super FREQ
Posts: 3,390

Re: using a macro to reference a vector element

I'm not sure that I understand what you want the macro for, but I think you want to use the RENAME subroutine to rename the files in PROC IML. You can't call PROC DATASETS from your PROC IML program. (You can do it in SAS/IML Studio, but that's not what you asked.)

Without worrying about macros, here's how you can rename functions in PROC IML. Hopefully, this will give you the information you need.

proc iml;
x=1;
create d1 var {x}; append; close; /** create fakes data sets **/
create d2 var {x}; append; close;
create d3 var {x}; append; close;

oldIdx = {1 2 3}; /** old indices **/
newIdx = {4 5 6};/** new indices **/

do j = 1 to ncol(newIdx);
oldName = "d" + strip(char(oldIdx[ j ]));
newName = "d" + strip(char(newIdx[ j ]));
call rename(oldName, newName);
end;

ds = datasets(work);
print ds;


Incidentally, you can't use the indices in your example ({1,2,3} ==> {3,5,7}) because you'll end up trying to rename data1 to data3, which already exists.
Super Contributor
Posts: 281

Re: using a macro to reference a vector element

You can do this entirely with macros. You need a macro variable containing the original data set number, and another macro variable containing the number its supposed to change into. Side note: Seems kind of pointless to me to change data1 to data3, maybe its just a greatly simplified example.

However,

> Suppose I have 3 data sets: data1, data2, data3 and I
> want to rename then data3, data5, data7. I can do
> this with proc datasets:
>
> proc datasets;
> change data1=data3;
> run;

This code fails, you cannot change data1 to data3 because there is an existing data3.
Ask a Question
Discussion stats
  • 2 replies
  • 317 views
  • 0 likes
  • 3 in conversation