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 (December 2009, week 1)Back to main SAS-L pageJoin or leave SAS-L (or change settings)ReplyPost a new messageSearchProportional fontNon-proportional font
Date:         Mon, 7 Dec 2009 07:35:49 -0800
Reply-To:     Tom Abernathy <tom.abernathy@GMAIL.COM>
Sender:       "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From:         Tom Abernathy <tom.abernathy@GMAIL.COM>
Organization: http://groups.google.com
Subject:      Re: convert macro variable to numeric
Comments: To: sas-l@uga.edu
Content-Type: text/plain; charset=ISO-8859-1

Use %SYSEVALF function to compare as numbers.

%if %sysevalf(&mvar = 5.00) %then ....

On Dec 7, 9:57 am, "dc...@hotmail.com" <dc...@hotmail.com> wrote: > On Dec 6, 10:43 pm, xlr82sas <xlr82...@aol.com> wrote: > > > > > > > On Dec 6, 6:02 pm, "dc...@hotmail.com" <dc...@hotmail.com> wrote: > > > > On Dec 6, 8:39 pm, "dc...@hotmail.com" <dc...@hotmail.com> wrote: > > > > > I'm trying to compare a macro variable to a numeric in an %if and need > > > > help understanding why this won't resolve: > > > > > When %let total_quantity = 0.00 it doesn't work unless I change the if > > > > statement to = 0.00 (which for a number of reasons I don't want to do. > > > > > When %let total_quantity = 1.0 the code works fine > > > > > and when %let total_quantity = -1.00 it blows up. > > > > > is there a way to convert the macro variable &total_quantity to a > > > > numeric? I've tried the %eval function and %sysevalf but neither is > > > > having the desired impact. > > > > > %macro test; > > > > %let trade_type = C; > > > > %let total_quantity = -1.00; > > > > > %if &trade_type = C %then %do; > > > > %if &total_quantity = 0. %then %do; > > > > %put 0 = &total_quantity; > > > > %end; > > > > %else %if &total_quantity > 0.00 %then %do; > > > > %put 0 < &total_quantity; > > > > %end; > > > > %else %if &total_quantity < 0. %then %do; > > > > %put 0 > &total_quantity; > > > > %end; > > > > > %end; > > > > %mend test; > > > > > %test; > > > > I tried two more examples. What happens when we let &total_quantity = > > > 5 > > > > %macro test; > > > %let trade_type = C; > > > %let total_quantity = 5; > > > > %if &trade_type = C %then %do; > > > %if &total_quantity = 5.00 %then %do; > > > %put 0 = &total_quantity; > > > %end; > > > %else %if &total_quantity > 0.00 %then %do; > > > %put 0 < &total_quantity; > > > %end; > > > %else %if &total_quantity < 0. %then %do; > > > %put 0 > &total_quantity; > > > %end; > > > > %end; > > > %mend test; > > > > %test; > > > > If we run the above the second %put statement is triggered. If we > > > change the first %if to = 5 then the first %put is triggered. it > > > seems that the macro variable character value is not being compared > > > properly to the numeric value. Is there a way to get the right > > > comparison made??- Hide quoted text - > > > > - Show quoted text - > > > Hi dc353, > > > The macro language is basically a text subsitution language. So > > numbers are just text 5.00 and 5 are not the same text. 5.00 is four > > characters and 5 is one character. > > > If you want to comapare a the strings 5 and 5.00 then convert one of > > the other to the same string. > > > Try (untested code). The putn converts the string '5' to '5.00' > > > %macro > > test; > > %let trade_type = > > C; > > %let total_quantity = > > 5; > > > %if &trade_type = C %then > > %do; > > %if %sysfunc(putn(&total_quantity,4.2)) = 5.00 %then > > %do; > > %put 0 = > > &total_quantity; > > > %end; > > %else %if &total_quantity > 0.00 %then > > %do; > > %put 0 < > > &total_quantity; > > > %end; > > %else %if &total_quantity < 0. %then > > %do; > > %put 0 > > > &total_quantity; > > > %end; > > > %end; > > %mend > > test; > > > %test; > > Thanks, but this doesn't work. If you use -5 you get: > > ERROR: A character operand was found in the %EVAL function or %IF > condition where a numeric > operand is required. The condition was: %sysfunc(putn > (&total_quantity,5.2)) = 5.00 > ERROR: The macro TEST will stop executing > > Is there some way to convert the macro character variable to a numeric > when making the comparison in the if statement?- Hide quoted text - > > - Show quoted text -


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