DATA Step, Macro, Functions and more

How to condition with Macro calculation?

Accepted Solution Solved
Reply
Contributor
Posts: 60
Accepted Solution

How to condition with Macro calculation?

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


Accepted Solutions
Solution
‎03-16-2018 08:07 AM
Contributor
Posts: 60

Re: How to condition with Macro calculation?

Thanks Tom, I found "%sysevalf" working for my code, but not %eval. I think eval for integer while sysevalf not. 

View solution in original post


All Replies
Super User
Super User
Posts: 7,934

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

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

Super User
Posts: 13,321

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

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.

Contributor
Posts: 60

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

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);
Super User
Super User
Posts: 7,934

Re: How to condition with Macro calculation?

[ Edited ]
Posted in reply to xiangpang

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=.;
Solution
‎03-16-2018 08:07 AM
Contributor
Posts: 60

Re: How to condition with Macro calculation?

Thanks Tom, I found "%sysevalf" working for my code, but not %eval. I think eval for integer while sysevalf not. 

Super User
Super User
Posts: 7,934

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

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().

Contributor
Posts: 60

Re: How to condition with Macro calculation?

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. 

 

 

 

 

 

Super User
Posts: 9,890

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

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

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Contributor
Posts: 60

Re: How to condition with Macro calculation?

Posted in reply to KurtBremser

Thanks for the suggestion.

Super User
Posts: 9,890

Re: How to condition with Macro calculation?

Posted in reply to xiangpang

"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).

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 10 replies
  • 188 views
  • 0 likes
  • 4 in conversation