Search code examples
sassas-macro

SAS: How to repeat calculation and change values?


For the following data, I want to create several new variables that combine Australia and Canada with different weights. In total, I would like to examine 10 different weight combinations.

Is there a way to do this where I can use the one formula and just change the weight values?

For example, rather than calculate Weight_1 to Weight_etc, can I just list the weights I want and then create the variables based on this list?

data Weighted_returns; set returns;
    Weight_1 = (Australia*0.6)+(Canada*0.4);
    Weight_2 = (Australia*0.5)+(Canada*0.5);
    Weight_3 = (Australia*0.4)+(Canada*0.6);
run;

enter image description here


Solution

  • DATA Step does not have any sort of vector math syntax. You can use one array to arrange and reference the target variables and another to hold the weights.

    Your result variables weight* would be a little conflicting with an array of weights, so I named the result variables result*

    data have;
      input australia canada;
      datalines;
      0.07 0.08
      0.02 -0.001
      0.05 0.01
    run;
    
    data want;
      set have;
      array results result_1-result_3;
      array weights (3) _temporary_ (0.6 0.5 0.4);
      do _n_ = 1 to dim(results);
        results(_n_) = australia * weights(_n_) + canada * (1 - weights(_n_));
      end;
    run;
    

    Use two weight arrays if the transformation is such that the sum of the weights to apply are not unity.