Statistical programming, matrix languages, and more

Looping with SAS IML

Reply
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;
run;

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

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

proc iml;
use estt&i;
read all var _num_ into Gamma&i[colname=varNames];
rho=0.99573468;
        lambda&i=rho*Gamma&i*(inv(I(2)-rho*Gamma));
        e1={1, 0};
        c&i=t(e1)+t(e1)*(lambda&i);           
        ec&i=(t(e1)+t(e1)*lambda&i)*(Gamma&i);
        d&i=t(e1)*(lambda&i);        
        ed&i=t(e1)*(lambda&i)*(Gamma&i);    
        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;
        quit;

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

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

    data ccc&i;
        set cc&i;
        temp=1;
    run;

    data ddd&i;
        set dd&i;
        temp=1;
    run;

    data eccc&i;
        set ecc&i;
        temp=1;
    run;

    data eddd&i;
        set edd&i;
        temp=1;
    run;

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

    
*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);
    run;

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


*Appending Final Data*;

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

%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,325

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,325

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.

Attachment
SAS Super FREQ
Posts: 3,232

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: 122

Re: Looping with SAS IML

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

 

lambda&i=rho*Gamma&i*(inv(I(2)-rho*Gamma));

 

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.

Post a Question
Discussion Stats
  • 6 replies
  • 333 views
  • 2 likes
  • 4 in conversation