Statistical programming, matrix languages, and more

Looping with SAS IML

Occasional Contributor
Posts: 5

Looping with SAS IML

[ Edited ]

I am running a program that involves looping around some proc iml commands. The index variable I use is country code ('code'). The code is looking like this:


%MACRO Aggregate1;

%DO i= 1 %TO 46;

data Vart&i;
set Var1;
if code=&i;

proc varmax data=Vart&i outest=est&i noprint;
model return TS / p=1
print=(estimates diagnose);
output out=VARR&i;

data estt&i (drop = const);
set est&i;
if type="EST" then output;

proc iml;
use estt&i;
read all var _num_ into Gamma&i[colname=varNames];
        e1={1, 0};
        create cc&i from c&i[colname={"h1&i" "h2&i"}];
        append from c&i;
        create dd&i from d&i[colname={"h11&i" "h12&i"}];
        append from d&i;
        create ecc&i from ec&i[colname={"h21&i" "h22&i"}];
        append from ec&i;
        create edd&i from ed&i[colname={"h31&i" "h32&i"}];
        append from ed&i;

data hi&i;
        merge Vart&i VARR&i(keep = res1 res2);

*make temp variable to merge first part of NCF and NDR *;
    data hi1&i;
        set hi&i;
        if ind2 =. then delete;

    data ccc&i;
        set cc&i;

    data ddd&i;
        set dd&i;

    data eccc&i;
        set ecc&i;

    data eddd&i;
        set edd&i;

    data final&i;
        merge hi1&i ccc&i ddd&i eccc&i eddd&i;
        by temp;

*estimate nc_indep_nosvol and nd_indep_nosvol;
    data Final1&i;
        set Final&i;
        nc_ind_nosvol&i=h1&i*res1 + h2&i*res2;
        nd_ind_nosvol&i=h11&i*res1 + h22&i*res2 ;
        ec_ind_nosvol&i=h21&i*lag(return) + h22&i*lag(TS);
        ed_ind_nosvol&i=h31&i*lag(return) + h32&i*lag(TS);

    data Final1A&i;
    set Final1&i;
    if res1=. or res2=. then delete;

*Appending Final Data*;

proc datasets nolist;
append base=CV force data=Final1A&i;

%mend; %Aggregate1;


The original data (VAR1) I have attached to this message. The code works for one iteration, but it doesn't work if I put it in a looping format. I get the following error message:


ERROR: (execution) Matrix has not been set to a value.




The SAS log indicates everything before the proc iml portion of the code works, so the mistake must be in the proc iml section. Is there anything wrong there?



Grand Advisor
Posts: 9,578

Re: Looping with SAS IML

It is hard to give you some advice if you don't give us a TEST dataset.
Occasional Contributor
Posts: 5

Re: Looping with SAS IML

Here is the dataset I am using.

Grand Advisor
Posts: 9,578

Re: Looping with SAS IML

Sorry. I couldn't run PROC VARMAX .
Occasional Contributor
Posts: 5

Re: Looping with SAS IML

HEre is the log file that I generated running the code. Clearly there is something wrong with the proc iml portion of the code. It looks like however the proc varmax is fine.

Posts: 3,409

Re: Looping with SAS IML

I don't have time to look at this right now, but your program will be greatly simplified if you get rid of the macro and use the 

BY code;

statement in PROC VARMAX.


As a general rule it is hard to debug a complex macro. A useful tip is to develop your program as open code and set 

%LET i = 1;

%LET i = 2;

%LET i = 3;


to manually debug each step of an iterative process.

Frequent Contributor
Posts: 131

Re: Looping with SAS IML

I haven't tried to run your code, but I think the line:




might be the cause of the problem.  Without the &i suffix near the end, I would expect a message about the matrix Gamma not being set.

Ask a Question
Discussion stats
  • 6 replies
  • 4 in conversation