Hello,
I tried a code with "if a>&missp then b=.; " it works.
But when I tried "if a>&missp/100 then b=.; " It did not work.
Why? What is the right way to condition with Macro calculation?
Thanks
Thanks Tom, I found "%sysevalf" working for my code, but not %eval. I think eval for integer while sysevalf not.
@xiangpang wrote:
Hello,
I tried a code with "if a>&missp then b=.; " it works.
But when I tried "if a>&missp/100 then b=.; " It did not work.
Why? What is the right way to condition with Macro calculation?
Thanks
Neither of those statements are macro code so they should work just fine in your data step.
Now if the macro variable MISSP does not contain either a numeric literal value (like 45) or the name of a numeric variable (like C) then you might generate a syntax error.
What is the value of &missp? What type of variable is A, numeric or character? And what do some of the values look like?
Doesn't work is awful vague.
Are there errors in the log?: Post the code and log in a code box opened with the {i} to maintain formatting of error messages.
No output? Post any log in a code box.
Unexpected output? Provide input data in the form of a dataset, the actual results and the expected results. Data should be in the form of a data step. Instructions here: https://communities.sas.com/t5/SAS-Communities-Library/How-to-create-a-data-step-version-of-your-dat... will show how to turn an existing SAS data set into data step code that can be pasted into a forum code box using the {i} icon or attached as text to show exactly what you have and that we can test code against.
Sorry for the confuse.
The code like this. all the data is number. But I want to use 10 as missp, so I want create something like &missp/100 instead &missp.
Thanks
data a;
input ID y d z w;
cards;
1 1 38 38 23
2 1 30 45 19
4 1 56 45 23
4 0 67 13 67
4 1 48 35 56
;
run;
%Macro miss(missp);
data b;
set a;
c=ranuni(0);
if c>&missp then d=.;
run;
%mend;
%miss(0.9);
I think you are saying you want the user to pass in an integer and have that integer be divided by 100?
There is no reason that would not work.
Since you are using the value in a data step statement just add the '/100' into the constant part of statement.
if c>(&missp/100) then d=.;
So if you make that change and call the macro like this:
%miss(90)
Then the macro will generate this statement.
if c>(90/100) then d=.;
Which does the same thing as the line that the old macro would have generated it you passed in 0.9.
if c>0.9 then d=.;
Thanks Tom, I found "%sysevalf" working for my code, but not %eval. I think eval for integer while sysevalf not.
You must use %sysevalf() if you want to do non integer arithmetic in macro code. This include comparisons. %EVAL() is what %if , %while and %until use by default.
You compare two strings that look like integers with %EVAL() and it will do a numeric comparison. But if either includes a decimal point then it compare them as strings.
You also need to use %sysevalf() if you want to use date literals like '01JAN2017'd in your comparisons or calculations.
When you ask a question make sure to include important details. The example code you posted in your original question and in your follow-up response never showed any place where SAS would have used %eval().
Hello, Tom, thanks for your explanation and suggestion.
I tried "if a>(&missp/100) then b=.; " It did not work last night. There is no error in log, but the number produced is not right. So I tried to find other ways to solve the problem. That is why I used %eval and %sysevalf later.
The weird thing is I just run "if a>(&missp/100) then b=.; " again. It works, It happened before when I close SAS program and restart computer. I don't know why.
Again I appreciate your help and the suggestion from other people.
@xiangpang wrote:
Hello, Tom, thanks for your explanation and suggestion.
I tried "if a>(&missp/100) then b=.; " It did not work last night. There is no error in log, but the number produced is not right. So I tried to find other ways to solve the problem. That is why I used %eval and %sysevalf later.
The weird thing is I just run "if a>(&missp/100) then b=.; " again. It works, It happened before when I close SAS program and restart computer. I don't know why.
Again I appreciate your help and the suggestion from other people.
Anytime this happens, inspect all your programs that ran in this session for an options statement that sets obs=. See Maxim 9.
Thanks for the suggestion.
"It did not work" is one of the most UNHELPFUL statements a person can make. It tells us exactly nothing about the problem, and is worthy of the proverbial blonde secretary (no offense to people with fair hair here).
Show code, show log, show example data (see data2datastep and how to post code).
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.