BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Maimai
Calcite | Level 5
Hello. I’m trying to run a sas macro that contains PROC IML procedure. And it says the procedure is not found.
So I want to look for an alternative to PROC IML. One part of the macro is as below.
Really appreciate if someone could help me on this.

“ data data1;
set &data (keep=&yvar &mvar &avar &cvar &cens);
run;
%if &interaction=true %then %do;
data data1;
set data1;
int=&avar*&mvar;
run;
%end;
%if (&cvar^= & &casecontrol=false) | (&cvar^= &
&casecontrol=) %then %do;
%LET cvars= &cvar;
%LET i =1;
%DO %UNTIL(NOT %LENGTH(%SCAN(&cvars,&i))) ;
proc means noprint data=data1;
var %SCAN(&cvars,&i);
output out=data2&i mean=/autoname;
run;
data data2&i;
set data2&i;
drop _TYPE_ _FREQ_;
run;
proc iml;
use data2&i;
read all into vb;
mean=vb[1,1];
cname1 = {"mean"};
create data2new&i from mean [colname=cname1];
append from mean;
quit;
proc append base=data3 data=data2new&i;
run;
proc sql;
%LET i=%EVAL(&i+1);
%END;
proc iml;
use data3;
read all into vb;
data3=t(vb);
create data2 from data3;
append from data3;
quit;”
1 ACCEPTED SOLUTION

Accepted Solutions
Reeza
Super User

Here's the part of the code transposed that you originally posted. I'll leave the rest up to you. 

 

data data1;
set &data (keep=&yvar &mvar &avar &cvar &cens);
run;
%if &interaction=true %then %do;
data data1;
set data1;
int=&avar*&mvar;
run;
%end;
%if (&cvar^= & &casecontrol=false) | (&cvar^= &
&casecontrol=) %then %do;
%LET cvars= &cvar;
%LET i =1;
%DO %UNTIL(NOT %LENGTH(%SCAN(&cvars,&i))) ;
proc means noprint data=data1;
var %SCAN(&cvars,&i);
output out=data2&i mean=/autoname;
run;

data data2new&i;
set data2&i;
rename %SCAN(&cvars,&i)_mean = mean;
drop _TYPE_ _FREQ_;
run;

proc append base=data3 data=data2new&i;
run;
proc sql;
%LET i=%EVAL(&i+1);
%END;

proc transpose data=data3 out=data2(drop = _name_);
run;

Honestly, that entire process could be removed from the macro loop and done in a single proc means. 

The single proc means below replaces most of the code you originally posted. 

 

%let cvars = weight height;
%let n_vars = %sysfunc(countw(&cvars));

%put &cvars.;
%put &n_vars.;

proc means data=sashelp.class noprint;
var &cvars;
output out=data2 (drop= _type_ _freq_) mean=col1-col&n_vars.;
run;

Good Luck!

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

In general, there is no SAS alternative to PROC IML, but you can do similar calculations in R or Python.

 

Perhaps some parts of IML code can be "migrated" to other parts of SAS. Please explain what your code is doing, and maybe there are ways to do this without IML.

--
Paige Miller
Maimai
Calcite | Level 5
It’s a macro to do the mediation analysis in epidemiology area. It’s provided on the webpage. https://www.hsph.harvard.edu/linda-valeri/computational-tools/

I also attached it here.
PaigeMiller
Diamond | Level 26

I don't know anything about causal mediation analysis, but I do know that SAS has PROC CAUSALMED, which you should investigate.

--
Paige Miller
Maimai
Calcite | Level 5

Thank you for your suggestion. I'm aware of the PROC CAUSALMED, but this procedure is not supported with my current SAS version. So I cannot use it. 

Reeza
Super User
PROC IML there is pretty basic, looks like some means or data tables so it should be pretty straightforward to transpose if you can describe what's happening in those steps. Or provide some data that this code would run on. Someone who knows IML better may be able to convert it easily as well.

I *think* the first one is just isolating some stat from the PROC MEANS output which should be easy to do in a data step as well. You could probably replace that entire loop with a single proc means in fact.

The second is a transpose so maybe proc transpose instead or a data step?
Maimai
Calcite | Level 5

I attached the data sample and the specific macro I want to modify here. 

Hope that someone who knows well PROC IML can help me to change the code to a different procedure. 

Many thanks in advance. 

Reeza
Super User
Took a look at the code and its quite a bit of code conversion but it all does seem like transpose and basic data processing so I think if you tried it you would be able to figure it out, just do each one at a time. Or if you can post your data here (like you have already) why not just run it on Academics on Demand which does have IML?
Reeza
Super User

Here's the part of the code transposed that you originally posted. I'll leave the rest up to you. 

 

data data1;
set &data (keep=&yvar &mvar &avar &cvar &cens);
run;
%if &interaction=true %then %do;
data data1;
set data1;
int=&avar*&mvar;
run;
%end;
%if (&cvar^= & &casecontrol=false) | (&cvar^= &
&casecontrol=) %then %do;
%LET cvars= &cvar;
%LET i =1;
%DO %UNTIL(NOT %LENGTH(%SCAN(&cvars,&i))) ;
proc means noprint data=data1;
var %SCAN(&cvars,&i);
output out=data2&i mean=/autoname;
run;

data data2new&i;
set data2&i;
rename %SCAN(&cvars,&i)_mean = mean;
drop _TYPE_ _FREQ_;
run;

proc append base=data3 data=data2new&i;
run;
proc sql;
%LET i=%EVAL(&i+1);
%END;

proc transpose data=data3 out=data2(drop = _name_);
run;

Honestly, that entire process could be removed from the macro loop and done in a single proc means. 

The single proc means below replaces most of the code you originally posted. 

 

%let cvars = weight height;
%let n_vars = %sysfunc(countw(&cvars));

%put &cvars.;
%put &n_vars.;

proc means data=sashelp.class noprint;
var &cvars;
output out=data2 (drop= _type_ _freq_) mean=col1-col&n_vars.;
run;

Good Luck!

SAS Innovate 2025: Register Now

Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!

What is ANOVA?

ANOVA, or Analysis Of Variance, is used to compare the averages or means of two or more populations to better understand how they differ. Watch this tutorial for more.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 8 replies
  • 1354 views
  • 2 likes
  • 3 in conversation