Date: Fri, 4 Mar 2011 06:27:31 -0500
Reply-To: Jim Groeneveld <jim.1stat@YAHOO.COM>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Jim Groeneveld <jim.1stat@YAHOO.COM>
Subject: Re: eval the expression in variable
Hi Clark,
And IMHO the best and (programmatically) quickest solution yet is:
DATA TestData;
A = '5+2 + 6'; * any expression to be parsed;
CALL SYMPUT ('Expr', A);
S1 = RESOLVE(&Expr);
RUN;
PROC PRINT DATA=TestData; RUN;
where A is the result of the concatenation of any character variables
together defining an arithmetic expression.
Regards - Jim.
--
Jim Groeneveld, Netherlands
Statistician/SAS consultant
http://jim.groeneveld.eu.tf
On Fri, 4 Mar 2011 06:01:48 -0500, Jim Groeneveld <jim.1stat@YAHOO.COM> wrote:
>Hi Clark,
>
>There is no character function PARSE or something equivalent in SAS (as
>there may be in other languages), yielding a numeric or character result.
>I think the solution with macro variables (CALL SYMPUT) suffices best. e.g.:
>
>DATA TestData;
> A = '5+2 + 6';
> CALL SYMPUT ('Expr', A);
>RUN;
>* step boundary here before the Expr value is available;
>DATA TestData;
> SET TestData;
> S2 = &Expr;
>RUN;
>
>PROC PRINT DATA=TestData; RUN;
>
>You may write your own parser, but it may run slower than the macro variable
>solution. A very basic example, involving addition only, is:
>
>DATA TestData (DROP=E I);
> A = '5+2 + 6';
> I = 1;
> E = INPUT(SCAN (A,I,'+'), BEST.);
> DO WHILE (NOT MISSING(E));
> S = SUM (S, E);
> I + 1;
> E = INPUT(SCAN (A,I,'+'), BEST.);
> END;
>RUN;
>
>PROC PRINT DATA=TestData; RUN;
>
>It sums numbers until a missing value occurs.
>
>Regards - Jim.
>--
>Jim Groeneveld, Netherlands
>Statistician/SAS consultant
>http://jim.groeneveld.eu.tf
>
>
>
>On Fri, 4 Mar 2011 03:55:17 -0500, Clark An <kuhasu@126.COM> wrote:
>
>>but still need to convert variables to macro variable.
>>Is there a way just using variables or array please?
>>Thx
|