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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
Ksharp
Super User

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

10 REPLIES 10
ballardw
Super User

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.

Ksharp
Super User
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 ?

Marzi
Obsidian | Level 7

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. 

Marzi
Obsidian | Level 7

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;
Rick_SAS
SAS Super FREQ

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

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

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;
 

Ksharp
Super User

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;
 

Ksharp
Super User
Use the second one, the first one is not right.

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!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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