BookmarkSubscribeRSS Feed
trekvana
Calcite | Level 5
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
2 REPLIES 2
Rick_SAS
SAS Super FREQ
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.
Paige
Quartz | Level 8
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.

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!

Multiple Linear Regression in SAS

Learn how to run multiple linear regression models with and without interactions, presented by SAS user Alex Chaplin.

Find more tutorials on the SAS Users YouTube channel.

From The DO Loop
Want more? Visit our blog for more articles like these.
Discussion stats
  • 2 replies
  • 924 views
  • 0 likes
  • 3 in conversation