Hello,
Certainly a trivial question: how do I put an %if condition with numeric criteria? For example, I want the code to process if the number of obs in a dataset is greater than 6
%if &nbObs>6 %then %do;
....
%end;
The problem is that &nbObs resolve to "23" which resolve incorrectly the condition. Tried with %eval without success
Thanks
Hi,
did you try to create the macro variable using proc sql, this will create the numeric macro variable with the number of observations. please try the below code
proc sql;
select count(*) into : obs from dataset_name;
quit;
%put &obs;
Thanks,
Jag
Jag is right insofar as you should try to get an unquoted (ie. numeric) value for &nbObs.
There is a solution however even when &nbObs has the value (quotes being part of the value) "23"
%if %eval ( %sysfunc(dequote(&nbObs)) > 6 ) %then %do ;
This first removes the quotes from &nbObs and then does the %eval part.
But as mentioned initially (giving credit to Jag): try to get a pure numeric value for &nbObs first.
Regards
Robert
Thank you Robert, yes indeed your solution is also very useful.
Regards,
Jag
Although I'm not able to test it at the moment, you could probably get rid of %eval from the earlier suggestion:
%if %sysfunc(dequote(&nobs)) > 6 %then %do;
One thing you should NOT do is add double quotes to match:
%if &nobs > "6" %then %do;
It will run, but will make the wrong comparison. Because of the double quotes, it will make a character comparison. Since 2 < 6, the comparison will be false.
Better yet, listen to the suggestions to get rid of the double quotes from &nobs and you won't have to worry about complicated comparisons.
Agree with Astounding. %if will automatically conduct %eval operation at the background.
Haikuo
Join us for SAS Innovate 2025, our biggest and most exciting global event of the year, in Orlando, FL, from May 6-9.
Lock in the best rate now before the price increases on April 1.
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.
Ready to level-up your skills? Choose your own adventure.