LISTSERV at the University of Georgia
Menubar Imagemap
Home Browse Manage Request Manuals Register
Previous messageNext messagePrevious in topicNext in topicPrevious by same authorNext by same authorPrevious page (April 2008, week 2)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Fri, 11 Apr 2008 07:17:41 -0500
Reply-To:     "data _null_," <datanull@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         "data _null_," <datanull@GMAIL.COM>
Subject:      Re: How to create a new variable by a formula variable
Comments: To: qkaiwei@163.com
In-Reply-To:  <853eb41a-5c3e-4eb6-97dd-9a0511387362@p25g2000pri.googlegroups.com>
Content-Type: text/plain; charset=ISO-8859-1

A function to evaluate an expression in a data step at run time would be the ideal solution. You can cheat and use %SYSEVALF (example 1) but the expression has to be rather simple. Probably the most straight forward way is to code gen the necessary statements with a data step (example 2). Then the expression can be compiled by the data step compiler and executed "properly".

*example 1; %symdel a b c d; *Notice I have changed the values of formula; data _null_; input formula:$50. a b c d; array _m[*] a b c d; do i = 1 to dim(_m); call symputx(vname(_m[i]),_m[i]); end; result = input(resolve(cats('%sysevalF(',formula,')')),8.); putlog 'NOTE: ' (_all_) (=); cards; &a*&b 1 2 3 4 &b*&c 1 2 3 4 %sysfunc(log(&a))+%sysfunc(log(&b))*100 3 4 5 6 ; run;

*Example 2; *just bite the bullet and get er done; *notice that functions not present a problem; filename ft17f001 temp; data _null_; file ft17f001; put 'data temp;'; infile cards eof=eof; do while(1); input formula:$50. a b c d; array _m[*] a b c d; put +3 'Length formula $50;'; put +3 formula=$quote52. (_m[*]) (= ';') ';'; put +3 'var=' formula ';'; put +3 'output;'; end; eof: put +3 'Run;'; stop; cards; a*b 1 2 3 4 b*c 1 2 3 4 log(a)+log(b)*100 3 4 5 6 ; run; %inc ft17f001 / source2; proc print; run;

On Fri, Apr 11, 2008 at 3:00 AM, SUBSCRIBE SAS-L qkaiwei <qkaiwei@163.com> wrote: > data tmp; > input formula:$20. a b c d; > cards; > a*b 1 2 3 4 > b*c 1 2 3 4 > ; > run; > > I want to create a new variable VAR, and the value of VAR is decided > by the formula in the formula variable. In the first row of the result > table, VAR = a*b = 2, and in the second row, VAR = b*c = 6. > > Perpaps the formulas are not so simple like those, can anyone give me > a better way? >


Back to: Top of message | Previous page | Main SAS-L page