Hello,
I am trying to change a value for a macro variable decreasing it by 0.01 with each iteration and I cannot understand why this does not work. Could anyone tell me what is wrong in my code? Thank you!
%macro Low_cost_diet (K_cost_m=,
b=);
%do i=1 %to &b.;
%OptDietPattern (
DRIAG=16
,DRI=2
,DRIAG_Label='M - 2 TO 3 YRS'
,outputdata=OptimalDiet /*output data set*/
,Round=r15
,k_SOD=1
,k_IRON=1
,k_VitD=1
,k_CAL=1
,k_FI=1
,k_COST=&k_COST_m.
);
%let k_COST=%eval(&k_COST-0.01);
%end;
%mend;
%Low_cost_diet (K_cost_m=1,b=2);
The message is correct. Your exterior macro %LOW_COST_DIET does not contain a macro variable named &K_COST. Only the interior macro (%OptDietPattern) contains &K_COST.
I suspect you want to change this statement:
%let k_COST=%eval(&k_COST-0.01);
Instead, use:
%let k_COST_m = %sysevalf (&k_COST_m - 0.01);
%eval only performs integer arithmetic. You are trying to perform non-integer arithmetic using %eval.
You could use %sysevalf which will perform non-integer arithmetic.
%EVAL() does not do floating point math.
Either use %SYSEVALF() or parameterize the macro differently so that you are passing in an integer instead of a floating point value. Note it is very had to represent a floating point value exactly as a text string.
thank you but it still does not work..
WARNING: Apparent symbolic reference K_COST not resolved.
WARNING: Apparent symbolic reference K_COST not resolved.
ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:
&k_COST-0.01
ERROR: The macro LOW_COST_DIET will stop executing.
Are you calling the macro variable K_COST or K_COST_M?
I want to run %OptDietPattern macro starting with k_COST=1 and in the next iteration of that macro I want to decrease the cost by 0.01. I will add the data step for each iteration later ..now I just need to make reduction in cost work for each iteration.
I suspect in the long run you might want to consider making your macro variable integer and then doing a conversion in the step using
,k_COST=&k_COST_m.
Or not incrementing the macro variable at all and using something like
,k_COST=&k_COST_m - (0.01 * (&i - 1) )
w
The message is correct. Your exterior macro %LOW_COST_DIET does not contain a macro variable named &K_COST. Only the interior macro (%OptDietPattern) contains &K_COST.
I suspect you want to change this statement:
%let k_COST=%eval(&k_COST-0.01);
Instead, use:
%let k_COST_m = %sysevalf (&k_COST_m - 0.01);
Hurray.. it works… Thank you a lot Astounding! Much appreciated! I spent the whole morning trying to make it work...You saved me another half of the day!
Thank you very much!
It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.
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.