BookmarkSubscribeRSS Feed
☑ This topic is solved. Need further help from the community? Please sign in and ask a new question.

I don't understand why the generated macro variable is not printed with decimal values, but only the integer part:

 

data test;
x=16017854.36;
run;

proc sql noprint;
select x into:x from test;
quit;
%put x=&x.;
1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26
proc sql noprint;
    select x format=12.2 into :x from test;
quit;
%put &=x;
--
Paige Miller

View solution in original post

4 REPLIES 4
PaigeMiller
Diamond | Level 26
proc sql noprint;
    select x format=12.2 into :x from test;
quit;
%put &=x;
--
Paige Miller
Sajid01
Meteorite | Level 14

In SAS SQL when no format is specified for a  variable length the format defaults to best8.
This was causing anything beyond 8 characters to be truncated. Therefore the proper approach is to use format as rightly suggested by @PaigeMiller.

ballardw
Super User

Something to always keep in mind: the SAS macro language is text. So any "number" requires you to take control to create the needed text representation of the numeric value, i.e.  a format most of the time.

 

Also use of values with decimal portions are likely to require the macro function %SYSEVALF to actually use the decimal portion. Consider this brief example of attempting to add 10 to a decimal valued macro variable:

%let value= 123.45;

%let newvalue_1 = %eval(&value. + 10);
%let newvalue_2 = %sysevalf(&value. + 10);

%put newvalue_1 is: &newvalue_1.  and  newvalue_2 is: &newvalue_2.;

Note that without %eval or %sysevalf you would get text of "123.45 + 10", not the arithmetic value.

The log:

1    %let value= 123.45;
2
3    %let newvalue_1 = %eval(&value. + 10);
ERROR: A character operand was found in the %EVAL function or %IF
       condition where a numeric operand is required. The condition was:
       123.45 + 10
4    %let newvalue_2 = %sysevalf(&value. + 10);
5
6    %put newvalue_1 is: &newvalue_1.  and  newvalue_2 is: &newvalue_2.;
newvalue_1 is:   and  newvalue_2 is: 133.45

You might ask why did I use %eval. That is what SAS will by default call internally if you attempt a number of uses.

 

You may also find this of use:

%macro dummy(value);
%if &value. > 1000 %then %put value is greater than 1000;
%else %put value is not greater than 1000;
%mend;

%dummy(123);
%dummy(123.45);

With result

40   %dummy(123);
value is not greater than 1000
41   %dummy(123.45);
value is greater than 1000

and wonder why that second result?

Consider:

42   %dummy(abc);
value is greater than 1000

Remember my comment about macros and text? When using a value that represents an integer  with the comparison you get SAS %eval result  because it works as SAS is attempting to help you. But when the value is not an integer the comparison is  CHARACTER based. Since the second character 2 is "greater than" 0 then the comparison stops and is "greater than" using character rules. And the same with abc. "a" is greater than "1".

 

This is the sort of thing that happens to generate unexpected results in the macro language when you test code and it "works" then use with other ranges of values such as testing with integers and then actual values are decimal.

A_Kh
Lapis Lazuli | Level 10

Alternatively, you can use CALL SYMPUTX in data step, which passes your decimals and long numeric values (up to 17 I believe) into macro variable without truncation. 

data test;
	x=16017854.36;
	call symputx('x', x); 
run;
%put &=x; 

log:

X=16017854.36

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

Register now!

How to Concatenate Values

Learn how use the CAT functions in SAS to join values from multiple variables into a single value.

Find more tutorials on the SAS Users YouTube channel.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

Discussion stats
  • 4 replies
  • 456 views
  • 3 likes
  • 5 in conversation