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

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

1 ACCEPTED SOLUTION

Accepted Solutions
xiangpang
Quartz | Level 8

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

10 REPLIES 10
Tom
Super User Tom
Super User

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

ballardw
Super User

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.

xiangpang
Quartz | Level 8

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);
Tom
Super User Tom
Super User

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=.;
xiangpang
Quartz | Level 8

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

Tom
Super User Tom
Super User

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

xiangpang
Quartz | Level 8

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. 

 

 

 

 

 

Kurt_Bremser
Super User

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

xiangpang
Quartz | Level 8

Thanks for the suggestion.

Kurt_Bremser
Super User

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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.

Click image to register for webinarClick image to register for webinar

Classroom Training Available!

Select SAS Training centers are offering in-person courses. View upcoming courses for:

View all other training opportunities.

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