BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
desireatem
Pyrite | Level 9

Please can anyone help correct this code for me::

Data sim;

Input A B C Beta;

cards;

1 3  3  0.5

2 7  4  0.5

3 10 3  0.5

4 12 6  0.5

;

Run;

proc iml;

use sim;

read all  var{A B C} into DM;

close;

A = DM[,1]; B = DM[,2]; C = DM[,3];  Beta = DM[,4];

n = nrow(DM);

start Func(x);

   return(  exp(x-1)*exp(2-x +Beta*C)#(cdf("Normal", x-10+Beta*C)-cdf("Normal", x-4+C)));

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

call quad(result, "Func", A || B,);

answer = result;

end;

create kaplan1n var{A  B C Beta Answer };

append;

quit;

LOG FILE

114  proc iml;

NOTE: IML Ready

115  use sim;

116  read all  var{A B C} into DM;

117  close;

NOTE: Closing WORK.SIM

118  A = DM[,1];

118!             B = DM[,2];

118!                         C = DM[,3];

118!                                      Beta = DM[,4];

ERROR: (execution) Invalid subscript or subscript out of range.

operation : [ at line 118 column 47

operands  : DM, , *LIT1005

DM      4 rows      3 cols    (numeric)

         1         3         3

         2         7         4

         3        10         3

         4        12         6

*LIT1005      1 row       1 col     (numeric)

         4

statement : ASSIGN at line 118 column 38

119  n = nrow(DM);

120  start Func(x);

121     return(  exp(x-1)*exp(2-x +Beta*C)#(cdf("Normal", x-10+Beta*C)-cdf("Normal",

121! x-4+C)));

122  finish;

NOTE: Module FUNC defined.

123  answer = j(nrow(DM),1);

124  do i = 1 to nrow(DM);

125  call quad(result, "Func", A || B,);

126  answer = result;

127  end;

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

operation : [ at line 121 column 37

operands  : C, i

C      0 row       0 col     (type ?, size 0)

i      0 row       0 col     (type ?, size 0)

statement : RETURN at line 121 column 4

traceback : module FUNC at line 121 column 4

Convergence could not be attained over the subinterval

             (1 , 3 )

The function might have one of the following:

     1) A slowly convergent or a divergent integral.

     2) Strong oscillations.

     3) A small scale in the integrand: in this case

        you can change the independent variable

        in the integrand, or,

        provide the optional vector describing roughly

        the mean and the standard deviation of the

        integrand

     4) Points of discontinuities in the interior

        in this case, you can provide a vector containing

        the points of discontinuity and try again.

ERROR: Execution error as noted previously. (rc=100)

operation : QUAD at line 125 column 1

operands  : *LIT1014, _TEM1003,

*LIT1014      1 row       1 col     (character, size 4)

Func

_TEM1003      1 row       2 cols    (numeric)

         1         3

statement : CALL at line 125 column 1

128  create kaplan1n var{A  B C Beta Answer };

129  append;

130  quit;

NOTE: Exiting IML.

NOTE: The data set WORK.KAPLAN1N has 4 observations and 5 variables.

NOTE: The SAS System stopped processing this step because of errors.

NOTE: PROCEDURE IML used (Total process time):

      real time           0.03 seconds

      cpu time            0.03 seconds

1 ACCEPTED SOLUTION

Accepted Solutions
Rick_SAS
SAS Super FREQ

start Func(x) global( Beta_i, C_i );

   return(  exp(-x)*exp(2-x +Beta_i*C_i)#(cdf("Normal", x-2+Beta_i*C_i)-cdf("Normal", x-1+C_i)) );

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

   Beta_i = Beta; C_i = C; /* set global variables */

   call quad(result, "Func", A || B,);

   answer = result;

end;

Here is a blog post that describes the GLOBAL statement and why it is necessary:

Evaluate an iterated integral in SAS

View solution in original post

8 REPLIES 8
Rick_SAS
SAS Super FREQ

Try reading four variables into DM:

read all  var{A B C Beta} into DM;

or

read all var _NUM_ into DM;

By the way, you never use DM for anything except for finding the length of obervations. You could save yourself some trouble if you read the data like this:

use sim  nobs n;       /* n contains number of observations */

read all  var{A B C Beta};  /* A B C and Beta are defined as vectors */

close;

desireatem
Pyrite | Level 9

Hello Rick, Thanks for the help. I did that but still having issue:

I think the problem is with this line :  return(  exp(x-1)*exp(2-x +Beta*C)#(cdf("Normal", x-10+Beta*C)-cdf("Normal", x-4+C)));

Data sim;

Input A B C Beta;

cards;

1 3  3  0.5

2 7  4  0.5

3 10 3  0.5

4 12 6  0.5

;

Run;

proc iml;

use sim;

read all var _NUM_ into DM;

close;

A = DM[,1]; B = DM[,2]; C = DM[,3];  Beta = DM[,4];

n = nrow(DM);

start Func(x);

   return(  exp(x-1)*exp(2-x +Beta*C)#(cdf("Normal", x-10+Beta*C)-cdf("Normal", x-4+C)));

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

call quad(result, "Func", A || B,);

answer = result;

end;

create kaplan1n var{A  B C Beta Answer };

append;

quit;

Rick_SAS
SAS Super FREQ

Think carefully when you get an error. Does the error message give useful information that would help you debug the problem yourself?  In this case, 'i' is not defined in the function, so C is undefined.

To help you out, here are some blog posts where I have discussed error messages and how to interpret them:

How to interpret SAS/IML error messages - The DO Loop

How to find and fix programming errors - The DO Loop

Interpreting PROC IML error messages: Matrices do not conform to the operation - The DO Loop 

desireatem
Pyrite | Level 9

Hello Rick, Thanks for the help. I tried to fix it , however it runs without error but there is no answer because it never converges.

Data sim;

Input A B C Beta;

cards;

1 2  7  0.5

1 2.5  9  0.5

1 2.5 8  0.5

1 3  9  0.5

;

Run;

proc iml;

use sim;

read all var _NUM_ into DM;

close;

A = DM[,1]; B = DM[,2]; C = DM[,3];  Beta = DM[,4];

n = nrow(DM);

start Func(x);

do i = 1 to nrow(DM);

   return(  exp(-x)*exp(2-x +Beta*C)#(cdf("Normal", x-2+Beta*C)-cdf("Normal", x-1+C)));

end;

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

call quad(result, "Func", A || B,);

answer = result;

end;

create kaplan1n var{A  B C Beta Answer };

append;

quit;

LOG FILE

417  Data sim;

418  Input A B C Beta;

419  cards;

NOTE: The data set WORK.SIM has 4 observations and 4 variables.

NOTE: DATA statement used (Total process time):

      real time           0.06 seconds

      cpu time            0.00 seconds

424  ;

425  Run;

426  proc iml;

NOTE: IML Ready

427  use sim;

428  read all var _NUM_ into DM;

429  close;

NOTE: Closing WORK.SIM

430  A = DM[,1];

430!             B = DM[,2];

430!                         C = DM[,3];

430!                                      Beta = DM[,4];

431  n = nrow(DM);

432

433  start Func(x);

434  do i = 1 to nrow(DM);

435     return(  exp(-x)*exp(2-x +Beta*C)#(cdf("Normal", x-2+Beta*C)-cdf("Normal",

435! x-1+C)));

436  end;

437  finish;

NOTE: Module FUNC defined.

438  answer = j(nrow(DM),1);

439  do i = 1 to nrow(DM);

440  call quad(result, "Func", A || B,);

441  answer = result;

442  end;

Convergence could not be attained over the subinterval

             (1 , 2 )

The function might have one of the following:

     1) A slowly convergent or a divergent integral.

     2) Strong oscillations.

     3) A small scale in the integrand: in this case

        you can change the independent variable

        in the integrand, or,

        provide the optional vector describing roughly

        the mean and the standard deviation of the

        integrand

     4) Points of discontinuities in the interior

        in this case, you can provide a vector containing

        the points of discontinuity and try again.

Convergence could not be attained over the subinterval

             (1 , 2.5 )

The function might have one of the following:

     1) A slowly convergent or a divergent integral.

     2) Strong oscillations.

     3) A small scale in the integrand: in this case

        you can change the independent variable

        in the integrand, or,

        provide the optional vector describing roughly

        the mean and the standard deviation of the

        integrand

     4) Points of discontinuities in the interior

        in this case, you can provide a vector containing

        the points of discontinuity and try again.

Convergence could not be attained over the subinterval

             (1 , 2.5 )

The function might have one of the following:

     1) A slowly convergent or a divergent integral.

     2) Strong oscillations.

     3) A small scale in the integrand: in this case

        you can change the independent variable

        in the integrand, or,

        provide the optional vector describing roughly

        the mean and the standard deviation of the

        integrand

     4) Points of discontinuities in the interior

        in this case, you can provide a vector containing

        the points of discontinuity and try again.

Convergence could not be attained over the subinterval

             (1 , 3 )

The function might have one of the following:

     1) A slowly convergent or a divergent integral.

     2) Strong oscillations.

     3) A small scale in the integrand: in this case

        you can change the independent variable

        in the integrand, or,

        provide the optional vector describing roughly

        the mean and the standard deviation of the

        integrand

     4) Points of discontinuities in the interior

        in this case, you can provide a vector containing

        the points of discontinuity and try again.

443  create kaplan1n var{A  B C Beta Answer };

444  append;

445  quit;

NOTE: Exiting IML.

NOTE: The data set WORK.KAPLAN1N has 4 observations and 5 variables.

NOTE: PROCEDURE IML used (Total process time):

      real time           0.15 seconds

      cpu time            0.11 seconds

Rick_SAS
SAS Super FREQ

Your function doesn't make sense.

1) DM is not defined in the function

2) You can't loop over several RETURN statements. When IML hits the first RETURN statement, it jumps out of the loop and returns.

3) Are you trying to return a vector from the "Func" function? The QUAD function expects the function to return a scalar quantity.

It would be useful if you could explain in words what you are trying to accomplish.  Then we can create SAS/IML statements that reflect the statistical or mathematical objectives.

desireatem
Pyrite | Level 9

Hello Rick,

Thank you, I am just trying to integrate this function exp(x-1)*exp(2-x +Beta*C)#(cdf("Normal", x-10+Beta*C)-cdf("Normal", x-4+C))) numerical and have an answer for each row. The only contant is "BETA" which is 0.5.  The limit is A and B that changes for each row. Also C changes for each row. I want to return a scalar. However, once I have additional variable on each row like in the case above, it doesnt give me any answer or runs with error.

My question is how to incorporate the variable C into the integral??

I want to to something similar like the code below, just that in this case I have a additional variable C in the exponential funtion and CDF:

Data sim;

Input A B;

cards;

1 3

2 7

3 10

4 12

;

Run;

proc iml;

use sim;

read all  var{A B} into DM;

close;

A = DM[,1]; B = DM[,2];

n = nrow(DM);

start Func(x);

   return(  exp(2-x)#cdf("Normal", x) );

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

call quad(result, "Func", A || B,);

answer = result;

end;

create kaplan1n var{A  B Answer };

append;

quit;

Rick_SAS
SAS Super FREQ

start Func(x) global( Beta_i, C_i );

   return(  exp(-x)*exp(2-x +Beta_i*C_i)#(cdf("Normal", x-2+Beta_i*C_i)-cdf("Normal", x-1+C_i)) );

finish;

answer = j(nrow(DM),1);

do i = 1 to nrow(DM);

   Beta_i = Beta; C_i = C; /* set global variables */

   call quad(result, "Func", A || B,);

   answer = result;

end;

Here is a blog post that describes the GLOBAL statement and why it is necessary:

Evaluate an iterated integral in SAS

desireatem
Pyrite | Level 9

Thanks a lot Rick, I will read the blog as well

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
  • 8 replies
  • 2674 views
  • 0 likes
  • 2 in conversation