Obsidian | Level 7

## computing the sensitivity for each variable

I would like to compute the sensitivity base on the following formula for each variable, however, I have difficulty to use proc inside a MACRO. the other problem is about the number of variables which are not fixed always.

I try to write my code,

%macro do_loop (data=, coef=);
data &data;
set &data;
set &coef;
array cp {*)_numeric_;
array nvar {*} _numeric_;
%do  i = 1 to dim(nvar);
Sens_&i. = 1+ (cp_&i *mean(nvar_&i))/(mult(cp , nvar));
%end;
run;
%mend do_loop;

this code definitely returns the error.
but I would very appreciate it if you help me how I can improve my code.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

## Re: computing the sensitivity for each variable

DATA x;
input x1 x2 x3 x4;
datalines ;
1 2 3 5
3 4 4 6
5 6 9 7
8 5 6 7
;
run;

DATA b;
input b1 b2 b3 b4;
datalines ;
0.3 0.4 0.5 0.1
;
run;
%let dsid=%sysfunc(open(x));
%let nvar=%sysfunc(attrn(&dsid,nvars));
%let dsid=%sysfunc(close(&dsid));
proc summary data = x;
var x: ;
output out=mean (drop=_:) mean= ;
run;
data want;
merge b mean;
array b{*} b:;
array x{*} x:;
array s{*} sens1-sens&nvar;
do i=1 to &nvar;
sum+b{i}*x{i};
end;

do i=1 to &nvar;
temp=0;
do j=1 to &nvar;
if j=i then temp+b{j}*x{j}*2;
else temp+b{j}*x{j};
end;
s{i}=temp/sum;
end;
drop i j temp sum;
run;

10 REPLIES 10
Super User

## Re: computing the sensitivity for each variable

If your %do i loop is supposed to address array members then do not use %do. The %do is not going to have any idea what the value of the dim of an array is. Macros generate code that is compiled and not much on the way of examining data step variable values at execution.

Instead of Sens_&i create another array to hold the results.

Array references are:   arrayname {index} where the index can be inside parantheses, braces or [ ]

So CP [i]

However datastep handles one row of data at a time and you are apparently attempting a MATRIX operation which means you either need to go to Proc IML

For the future it really helps us if you post Log results with error messages. If running macros use OPTIONS MPRINT SYMBOLGEN; run the code and post the log complete with error messages in the code box that opens when clicking on the "run" icon in the message menu list to preserve layout.

Super User

## Re: computing the sensitivity for each variable

Yeah. It is better for IML. Post is at IML forum.
and where is your data? and can you explain these symbols in formula .
b hat, x bar  stand for what ?

Obsidian | Level 7

## Re: computing the sensitivity for each variable

I have some restriction, only I can use SAS Base.

Bs are estimated coefficient & x_bar is the sample mean.
BTW, we have these value, don't need to compute them again.

Obsidian | Level 7

## Re: computing the sensitivity for each variable

this is what I write so far, but the final result is NULL

DATA x;
input x1 x2 x3 x4;
datalines ;
1 2 3 5
3 4 4 6
5 6 9 7
8 5 6 7
;
run;

DATA b;
input b1 b2 b3 b4;
datalines ;
0.3 0.4 0.5 0.1
;
run;

%macro trans (Beta , n);
proc fcmp;
array bb [1,&n] / nosymbols;
array tb [&n,1];
call transpose(bb, tb);
put tb =;
rc = write_array('tb', tb);
quit;
%mend;

%macro multi (datax, Beta,n);
%trans(&Beta, &n);
proc summary data = &datax nway;
var x: ;
output out=mean (drop=_:) mean= ;
run;
proc fcmp;
array xm [1,&n]/ nosymbols;
array c [&n, 1]/ nosymbols;
array result[1,1];

call mult (xm, c,result);
put result=;
rc = write_array('bx', result);
quit;
%mend;

%multi(x,b,4);

Data Sens;
set work.mean;
array y[*]_numeric_;
set  work.TB;
array D[*]_numeric_;
array Sens[*]_numeric_;
set work.BX;
array BetaX[*]_numeric_;

do i = 1 to dim(Sens);
Sens[i] = 1 + y[i]*D[i]/BetaX[1];
end;
run;
SAS Super FREQ

## Re: computing the sensitivity for each variable

Do you have the parameter estimates  (b hats) and the sample means (x bars)  already stored in a data set, or is that part of the problem? In otherwords, are we starting with the raw variables or with the estimates?  Also, does your formula assume anything about the variables, such as they are centered (have mean zero)?

Obsidian | Level 7

## Re: computing the sensitivity for each variable

I have estimated B and sample mean (x bar).
Yes, the data are centered, means we can assume that b0 is equal to zero.
Super User

## Re: computing the sensitivity for each variable

DATA x;
input x1 x2 x3 x4;
datalines ;
1 2 3 5
3 4 4 6
5 6 9 7
8 5 6 7
;
run;

DATA b;
input b1 b2 b3 b4;
datalines ;
0.3 0.4 0.5 0.1
;
run;
%let dsid=%sysfunc(open(x));
%let nvar=%sysfunc(attrn(&dsid,nvars));
%let dsid=%sysfunc(close(&dsid));
proc summary data = x;
var x: ;
output out=mean (drop=_:) mean= ;
run;
data want;
merge b mean;
array b{*} b:;
array x{*} x:;
array s{*} sens1-sens&nvar;
do i=1 to &nvar;
sum+b{i}*x{i};
end;

do i=1 to &nvar;
do j=1 to &nvar;
if j=i then temp+b{j}*x{j}*2;
else temp+b{j}*x{j};
end;
s{i}=temp/sum;
end;
drop i j temp sum;
run;

Super User

## Re: computing the sensitivity for each variable

DATA x;
input x1 x2 x3 x4;
datalines ;
1 2 3 5
3 4 4 6
5 6 9 7
8 5 6 7
;
run;

DATA b;
input b1 b2 b3 b4;
datalines ;
0.3 0.4 0.5 0.1
;
run;
%let dsid=%sysfunc(open(x));
%let nvar=%sysfunc(attrn(&dsid,nvars));
%let dsid=%sysfunc(close(&dsid));
proc summary data = x;
var x: ;
output out=mean (drop=_:) mean= ;
run;
data want;
merge b mean;
array b{*} b:;
array x{*} x:;
array s{*} sens1-sens&nvar;
do i=1 to &nvar;
sum+b{i}*x{i};
end;

do i=1 to &nvar;
temp=0;
do j=1 to &nvar;
if j=i then temp+b{j}*x{j}*2;
else temp+b{j}*x{j};
end;
s{i}=temp/sum;
end;
drop i j temp sum;
run;

Super User

## Re: computing the sensitivity for each variable

Use the second one, the first one is not right.
Obsidian | Level 7

## Re: computing the sensitivity for each variable

Thanks @Ksharp
Discussion stats
• 10 replies
• 1533 views
• 2 likes
• 4 in conversation