BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Lida
Obsidian | Level 7

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);

1 ACCEPTED SOLUTION

Accepted Solutions
Astounding
PROC Star

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);

View solution in original post

8 REPLIES 8
PaigeMiller
Diamond | Level 26

%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. 

--
Paige Miller
Tom
Super User Tom
Super User

%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.

Lida
Obsidian | Level 7

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.

Tom
Super User Tom
Super User

Are you calling the macro variable K_COST or K_COST_M?

Lida
Obsidian | Level 7

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.

ballardw
Super User

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

Astounding
PROC Star

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);

Lida
Obsidian | Level 7

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!

hackathon24-white-horiz.png

The 2025 SAS Hackathon has begun!

It's finally time to hack! Remember to visit the SAS Hacker's Hub regularly for news and updates.

Latest Updates

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 8 replies
  • 4324 views
  • 0 likes
  • 5 in conversation