|
On 20/06/06, Jagadish <jagadishkr@gmail.com> wrote:
> Hi All,
> I have a dataset which has a character variable called var1. Below are
> the sample records of this dataset.
>
> Var1
> 1+2+3
> 3+4+5
> 10+20+100
>
>
> I want to create a new numeric variable say Var2 which is the result
> the string expressions. The final dataset should look like,
>
> Var1 Var2
> 1+2+3 6
> (3+4)*5 35
> (30-20)+100 110
>
> I tried with the below code and it is working fine,
> data x;
> length var1 $100;
> input var1 & $;
> call symput('x', var1);
> call execute('%let x=%Eval(&x);');
> var2=symget('x');
> cards;
> 1+ 2 + 3
> (3 + 4 ) * 5
> (30 - 20) + 100
> ;
> run;
>
> I just wanted to know is there any better way to do this. I mean
> without usinng macro variables or is there any SAS function to handle
> string math expressions?
>
> Thanks,
> Jagadish
>
Hi,
I don't know if there is a BETTER way, but using the RESOLVE function
seems more straitforward.
data x;
length var1 $100 y $20;
input var1 & $;
y = resolve('%eval('||var1||')');
cards;
1+ 2 + 3
(3 + 4 ) * 5
(30 - 20) + 100
;
run;
Regards
++ Guido
|