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

I know how to assign MVar like below. 

 

%let ref_mm_ind=100; %let ref_mm_ind_stind=%sysfunc(max(10,%eval(&ref_mm_ind.-150*4)));

Now I have another macro variable, mmnote, which can be max or min.

 

Now I need if mmnote=max, assign 400 to ref_mm_ind_stind; if mmnote=min, keep untouch 

as above.

How to do with just %let lines?!

Thanks, 

1 ACCEPTED SOLUTION

Accepted Solutions
FreelanceReinh
Jade | Level 19

Hello @hellohere,

 

You can use the IFN and IFC functions in conjunction with %SYSFUNC to assign a value conditionally:

%let ref_mm_ind_stind=%sysfunc(ifn(&mmnote=max, 400, &ref_mm_ind_stind));

View solution in original post

4 REPLIES 4
FreelanceReinh
Jade | Level 19

Hello @hellohere,

 

You can use the IFN and IFC functions in conjunction with %SYSFUNC to assign a value conditionally:

%let ref_mm_ind_stind=%sysfunc(ifn(&mmnote=max, 400, &ref_mm_ind_stind));
ballardw
Super User

Related functions WHICHN and WHICHC may be useful if you need to extend this logic to selecting more  choices.

 

Be very sure of the CASE of the compared character values using IFC and WHICHC.

 

%let ref_mm_ind_stind=%sysfunc(ifn(&mmnote=max, 400, &ref_mm_ind_stind)); would not get the desired behavior if &mmnote has a value of MAX (or many others with one or more capital letters). If the resolved values of the macro variable MMNOTE might have case issues then use one of the %LOWCASE or %UPCASE in macro code such as

%let ref_mm_ind_stind=%sysfunc(ifn(%lowcase(&mmnote)=max, 400, &ref_mm_ind_stind));

This can be very important if you have to deal with user entered values such as a response to a prompt.

Tom
Super User Tom
Super User

Note sure why you think it makes sense to limit to "just %let lines".

 

So assuming that mmnote is also a macro variable then to test if its value is min you would add a simple %IF/%THEN/%DO block.

%let mmnote=max;
%let ref_mm_ind=100;
%if &mmnote=max %then %do;
  %let ref_mm_ind_stind=400;
%end;
%else %do;
  %let ref_mm_ind_stind=%sysfunc(max(10,%eval(&ref_mm_ind.-150*4)));
%end;

So no need to build complex nested function calls that will be difficult to maintain.

 

Also do you really want to calculate the formula (&ref_mm_ind - 150*4) using the INTEGER ONLY arithmetic that the macro function %EVAL() uses?  That will only work if REF_MM_IND only includes digits.  If you remove the %EVAL() then the formula will be passed to SAS itself to handle so that normal arithmetic will be used instead.

 

 

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