BookmarkSubscribeRSS Feed
khill
Calcite | Level 5
Hello,

This seems like a simple problem. When uses the SELECT INTO clause I end up with a character variable than cannot be evaluated numerically. How can this be modified to resolve the evaluation correctly?

%macro t;
PROC SQL NOPRINT;
Select t1.AVG_Denom into :xAvgDenom
From WORK.testdata2 as t1;
;
%put Average is &xAvgDenom;
%if &xAvgDenom < 25 %then
%put &xAvgDenom is Less Than 25;
%else
%put &xAvgDenom is Greater Than 25;
;
%_eg_conditional_dropds(testdata2)
%mend t;
OPTIONS Nomlogic;
Data testdata2;
input AVG_Denom;
;
cards;
112.4118 ;

%t;

Average is 112.4118
112.4118 is Less Than 25 (not in my way of thinking)


Thank you in advance,
John
4 REPLIES 4
SPR
Quartz | Level 8 SPR
Quartz | Level 8
Hello Khill,

The problem with your code is that all macro %IFs are resolved at compilation phase when AVG_Denom is not yet accessible. Reducing %IFs to datastep IFs solves the problem:
[pre]
%macro t;
proc SQL noprint;
select t1.AVG_Denom into :xAvgDenom
from WORK.testdata2 as t1
;quit;
%put Average is &xAvgDenom;
data _null_;
if &xAvgDenom < 25 then put "&xAvgDenom is Less Than 25";
else put "&xAvgDenom is Greater Than 25";
run;
%mend t;
%t;
[/pre]
Sincerely,
SPR
RickM
Fluorite | Level 6
Try using

%put Average is &xAvgDenom;
%if %sysevalf(&xAvgDenom < 25) %then
%put &xAvgDenom is Less Than 25;
%else
%put &xAvgDenom is Greater Than 25;

Remember that macro variables are considered text so if you are doing any sort of numeric operation (addition, subtraction, boolean comparison) you need to use %eval (for integers) or %sysevalf (for floating point numbers).

Good luck!
ArtC
Rhodochrosite | Level 12
Just to expand on RickM's answer a bit.

The %IF statement:

[pre]%if &x lt 9 %then.....
[/pre]

will perform a numeric comparison if &x contains an integer (there is an implied %EVAL), but a alphabetic comparison if either side is a noninteger. Since the decimal point makes for a noninteger 10.0 lt 9 is true, while 10 lt 9 is false. The %sysevalf forces a numeric comparison. those darn lt signs


Message was edited by: ArtC
khill
Calcite | Level 5
Rick and Art, thanks for the insight. This is what I needed to get me back on track.

John

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 868 views
  • 0 likes
  • 4 in conversation