Discussion:
Macro variable value rounded when assigned
(too old to reply)
a***@hushmail.com
2006-09-14 15:20:34 UTC
Permalink
I'm creating a macro variable that has the total for a given
variable /column using proc sql . . .

proc sql noprint; select sum(Amount) into :total from mydata;

. . . the value is 102105478, after assigning it to
a variable such as . . .

base = &total ;

. . . the value is 102110000, which is a rounded value (not what I
want).

I do use the total macro value in other calculations and they are
correct.

I've tried several different formats, but no luck.

Can someone explain this.

Thanks
Jim Groeneveld
2006-09-14 15:32:27 UTC
Permalink
Hi Auto

Show us the code, the log and the output.
A macro value is text, not a numerical value, so there must be something
else going on.

Regards - Jim.
--
Jim Groeneveld, Netherlands
Statistician, SAS consultant
home.hccnet.nl/jim.groeneveld

My computer's keyboard has quirks, I'm not making the typing errors.
Post by a***@hushmail.com
I'm creating a macro variable that has the total for a given
variable /column using proc sql . . .
proc sql noprint; select sum(Amount) into :total from mydata;
. . . the value is 102105478, after assigning it to
a variable such as . . .
base = &total ;
. . . the value is 102110000, which is a rounded value (not what I
want).
I do use the total macro value in other calculations and they are
correct.
I've tried several different formats, but no luck.
Can someone explain this.
Thanks
toby dunn
2006-09-14 15:33:10 UTC
Permalink
Auto ,

Explicitly put the value into a macro var instead of letting SAS do the
implicit conversion.


proc sql noprint ;
select Put( sum(Amount) , 10. -L ) Into :total
from mydata;
Quit ;



Toby Dunn

When everything is coming at you all at once, your in the wrong lane.

A truly happy person is someone who can smile and enjoy the scenery on a
detour.





From: ***@HUSHMAIL.COM
Reply-To: ***@HUSHMAIL.COM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Macro variable value rounded when assigned
Date: Thu, 14 Sep 2006 08:20:34 -0700

I'm creating a macro variable that has the total for a given
variable /column using proc sql . . .

proc sql noprint; select sum(Amount) into :total from mydata;

. . . the value is 102105478, after assigning it to
a variable such as . . .

base = &total ;

.. . . the value is 102110000, which is a rounded value (not what I
want).

I do use the total macro value in other calculations and they are
correct.

I've tried several different formats, but no luck.

Can someone explain this.

Thanks
Terjeson, Mark
2006-09-14 15:43:57 UTC
Permalink
Yes, a good suggestion, because remember that
macro variables are character variables. And
when re-using it later in a numeric context,
remember to convert it back to numeric:

basenum = input("&total",9.);


put() -- convert numeric to character
input() -- convert character to numeric



Hope this is helpful.


Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group


Russell
Global Leaders in Multi-Manager Investing






-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
toby dunn
Sent: Thursday, September 14, 2006 8:33 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: Macro variable value rounded when assigned

Auto ,

Explicitly put the value into a macro var instead of letting SAS do the
implicit conversion.


proc sql noprint ;
select Put( sum(Amount) , 10. -L ) Into :total
from mydata;
Quit ;



Toby Dunn

When everything is coming at you all at once, your in the wrong lane.

A truly happy person is someone who can smile and enjoy the scenery on a
detour.





From: ***@HUSHMAIL.COM
Reply-To: ***@HUSHMAIL.COM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Macro variable value rounded when assigned
Date: Thu, 14 Sep 2006 08:20:34 -0700

I'm creating a macro variable that has the total for a given variable
/column using proc sql . . .

proc sql noprint; select sum(Amount) into :total from mydata;

. . . the value is 102105478, after assigning it to
a variable such as . . .

base = &total ;

.. . . the value is 102110000, which is a rounded value (not what I
want).

I do use the total macro value in other calculations and they are
correct.

I've tried several different formats, but no luck.

Can someone explain this.

Thanks
Howard Schreier <hs AT dc-sug DOT org>
2006-09-14 15:45:41 UTC
Permalink
Post by a***@hushmail.com
I'm creating a macro variable that has the total for a given
variable /column using proc sql . . .
proc sql noprint; select sum(Amount) into :total from mydata;
SQL uses BEST8. for the numeric-to-character conversion, so you get

1.0211E8

Toby's code uses a wider format and so retains all of the significant digits.
Post by a***@hushmail.com
. . . the value is 102105478, after assigning it to
a variable such as . . .
base = &total ;
. . . the value is 102110000, which is a rounded value (not what I
want).
I do use the total macro value in other calculations and they are
correct.
I doubt that. The lower significant digits are lost to the macro variable
created by the SQL code.
Post by a***@hushmail.com
I've tried several different formats, but no luck.
Can someone explain this.
Thanks
a***@hushmail.com
2006-09-14 16:55:30 UTC
Permalink
Thank you all for your help:

. . . formating it within the proc sql statement does the job:

proc sql noprint; select sum(Amount) format=15.0 into :total from
mydata;

Thanks again !
Post by Howard Schreier <hs AT dc-sug DOT org>
Post by a***@hushmail.com
I'm creating a macro variable that has the total for a given
variable /column using proc sql . . .
proc sql noprint; select sum(Amount) into :total from mydata;
SQL uses BEST8. for the numeric-to-character conversion, so you get
1.0211E8
Toby's code uses a wider format and so retains all of the significant digits.
Post by a***@hushmail.com
. . . the value is 102105478, after assigning it to
a variable such as . . .
base = &total ;
. . . the value is 102110000, which is a rounded value (not what I
want).
I do use the total macro value in other calculations and they are
correct.
I doubt that. The lower significant digits are lost to the macro variable
created by the SQL code.
Post by a***@hushmail.com
I've tried several different formats, but no luck.
Can someone explain this.
Thanks
Howard Schreier <hs AT dc-sug DOT org>
2006-09-14 16:13:17 UTC
Permalink
Post by Terjeson, Mark
Yes, a good suggestion, because remember that
macro variables are character variables. And
when re-using it later in a numeric context,
basenum = input("&total",9.);
I have to disagree with Mark here.

If &TOTAL is initialized in the manner of the original poster's SQL code,
the right hand side of the assignment statement

basenum = &total;

is not a character value. It's numeric. Using the INPUT function can cause
problems if there are more than 9 characters in the string or if there are
leading spaces in the macro variable.

Of course there are other situations where a special informat (eg, DATE or
HEX) is required instead of the 9. Then Mark's construct is the way to go.
Post by Terjeson, Mark
put() -- convert numeric to character
input() -- convert character to numeric
Hope this is helpful.
Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group
Russell
Global Leaders in Multi-Manager Investing
-----Original Message-----
toby dunn
Sent: Thursday, September 14, 2006 8:33 AM
Subject: Re: Macro variable value rounded when assigned
Auto ,
Explicitly put the value into a macro var instead of letting SAS do the
implicit conversion.
proc sql noprint ;
select Put( sum(Amount) , 10. -L ) Into :total
from mydata;
Quit ;
Toby Dunn
When everything is coming at you all at once, your in the wrong lane.
A truly happy person is someone who can smile and enjoy the scenery on a
detour.
Subject: Macro variable value rounded when assigned
Date: Thu, 14 Sep 2006 08:20:34 -0700
I'm creating a macro variable that has the total for a given variable
/column using proc sql . . .
proc sql noprint; select sum(Amount) into :total from mydata;
. . . the value is 102105478, after assigning it to
a variable such as . . .
base = &total ;
.. . . the value is 102110000, which is a rounded value (not what I
want).
I do use the total macro value in other calculations and they are
correct.
I've tried several different formats, but no luck.
Can someone explain this.
Thanks
Terjeson, Mark
2006-09-14 16:26:51 UTC
Permalink
Ahhh, yes, thanks Howard, I did forget to
state that it the stored digits can be used directly.

Yes indeed the basenum = &total;
gets replaced with the digits as they
are stored in the macro variable, so
as long as the digits were stored in
the format you want, the syntax is just
as if you hardcoded a literal. e.g.
basenum = &total;
turns into
basenum = 102105478;
if those were the characters stored.
Yeah, the INPUT() will also work if you
wanted to alter the format of the stored
characters.

Thanks for catching the straightforward omission.

Mark

-----Original Message-----
From: SAS(r) Discussion [mailto:SAS-***@LISTSERV.UGA.EDU] On Behalf Of
Howard Schreier <hs AT dc-sug DOT org>
Sent: Thursday, September 14, 2006 9:13 AM
To: SAS-***@LISTSERV.UGA.EDU
Subject: Re: Macro variable value rounded when assigned

On Thu, 14 Sep 2006 08:43:57 -0700, Terjeson, Mark
Yes, a good suggestion, because remember that macro variables are
character variables. And when re-using it later in a numeric context,
basenum = input("&total",9.);
I have to disagree with Mark here.

If &TOTAL is initialized in the manner of the original poster's SQL
code, the right hand side of the assignment statement

basenum = &total;

is not a character value. It's numeric. Using the INPUT function can
cause problems if there are more than 9 characters in the string or if
there are leading spaces in the macro variable.

Of course there are other situations where a special informat (eg, DATE
or
HEX) is required instead of the 9. Then Mark's construct is the way to
go.
put() -- convert numeric to character
input() -- convert character to numeric
Hope this is helpful.
Mark Terjeson
Senior Programmer Analyst, IM&R
Russell Investment Group
Russell
Global Leaders in Multi-Manager Investing
-----Original Message-----
toby dunn
Sent: Thursday, September 14, 2006 8:33 AM
Subject: Re: Macro variable value rounded when assigned
Auto ,
Explicitly put the value into a macro var instead of letting SAS do the
implicit conversion.
proc sql noprint ;
select Put( sum(Amount) , 10. -L ) Into :total
from mydata;
Quit ;
Toby Dunn
When everything is coming at you all at once, your in the wrong lane.
A truly happy person is someone who can smile and enjoy the scenery on
a detour.
Subject: Macro variable value rounded when assigned
Date: Thu, 14 Sep 2006 08:20:34 -0700
I'm creating a macro variable that has the total for a given variable
/column using proc sql . . .
proc sql noprint; select sum(Amount) into :total from mydata;
. . . the value is 102105478, after assigning it to a variable such
as . . .
base = &total ;
.. . . the value is 102110000, which is a rounded value (not what I
want).
I do use the total macro value in other calculations and they are
correct.
I've tried several different formats, but no luck.
Can someone explain this.
Thanks
Loading...