use field value as a variable in sas -


wonder if can me

i’ve got dataset value in column field name of column. want able use value of column call applicable field in formula.

for instance … have columns…

merch_no v01 m02 v08 m08 amount plan 

a record …and want calc field do…

merch_no      v01   m02  v08  m08  amount    plan    calc 123456        2     2    1    1    100.00    v01     value of v01 * amount 456789        4     4    4    4    250.00    m08     value of m08 * amount 

if plan field record says v01, value of v01 column must used in calc field. if plan field says, m08, m08 value should used. there 40 plans.

a static example of how use vvaluex() function that.

data result; v01 = 2; amount=100; calc = 'value of v01 * amount'; length arg1 arg2 $32; arg1 = scan(compress(calc, 'value of'), 1); arg2 = scan(compress(calc, 'value of'), 2); put arg1 arg2; result = input(vvaluex(arg1), 16.) * input(vvaluex(arg2), 16.); run; 

for situation, you'd have create logic recognize know patters of calc, types , formats of variables (since vvaluex() returns formatted values).

a dynamic approach not suitable lots of data generate code each row (see below). assumes simple expression usable in if .. then.

data input; length calc  $50; input v01 m08 amount  calc 9-58; cards; 2 1 100 value of v01 * amount 2 4 100 value of m08 * amount ; run;  /* code generation */ data _null_; file 'mycalc.sas'; set input end=last;  length line $150; if _n_=1 do;     put 'data result;';     put '   set input;'; end; line = 'if _n_ = ' || put(_n_, 8. -l) ||           ' result = ' || compress(calc, 'value of') || ';'; put line; if last put 'run;'; run;  %include 'mycalc.sas'; /* run code */ 

ok, if see didn't notice note plan field - please adjust need.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -