DATA Step, Macro, Functions and more

computing the sensitivity for each variable

Accepted Solution Solved
Reply
Contributor
Posts: 24
Accepted Solution

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.
Capture.PNG

 

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. 


Accepted Solutions
Solution
‎11-05-2016 09:30 PM
Super User
Posts: 10,035

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;
 

View solution in original post


All Replies
Super User
Posts: 11,343

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
Posts: 10,035

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 ?

Contributor
Posts: 24

Re: computing the sensitivity for each variable

[ Edited ]

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. 

Contributor
Posts: 24

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];
   rc = read_array('@Beta', bb);
   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];
  
   rc = read_array('tb', c);
   rc = read_array('work.mean',xm );
   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
Posts: 3,753

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)?

Contributor
Posts: 24

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
Posts: 10,035

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;
 

Solution
‎11-05-2016 09:30 PM
Super User
Posts: 10,035

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
Posts: 10,035

Re: computing the sensitivity for each variable

Use the second one, the first one is not right.
Contributor
Posts: 24

Re: computing the sensitivity for each variable

Thanks @Ksharp
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 291 views
  • 2 likes
  • 4 in conversation