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 (March 2011, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
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


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