turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

Find a Community

- Home
- /
- BI
- /
- Enterprise Guide
- /
- Defining macro variable by cases

Topic Options

- RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

07-13-2016 10:40 AM

I'm on SAS EG 5.1.

One possible way to define variables by cases is, for instance,

Variable=2*(Statement 1)+3*(Neg of Statement 1);

What is this called? I'd like to read about it, but I don't know what words to search for.

Now please consider the following code.

%MACRO CYCLE; %DO A=1 %TO 2; %PUT &A.; %LET B=0.636805089257504*(&A.=1)+0.656423671305948*(&A.=2); %PUT &B.; %END; %MEND; %CYCLE;

On the log I find this:

1 0.636805089257504*(1=1)+0.656423671305948*(1=2) 2 0.636805089257504*(2=1)+0.656423671305948*(2=2)

I expected it to be

1 0.636805089257504 2 0.656423671305948

What am I missing?

Finally, for my actual question, regarding the second block of code, I'm trying to sue the variable B within a Data-Step and it's obvious from some simple computations that B isn't getting the value I expected it to get. In fact, it gets a value that doesn't seem at all related with any of the coefficients used above.

I worked around this by defining B withing the Data-Step, but this is seems crass.

Can you explain to me why isn't my macro variable B working properly and how can I fix it?

Thanks.

Accepted Solutions

Solution

07-13-2016
02:10 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 10:53 AM

Hi,

If you are attempting to evaluate arithmetic expressions represented by values of macro variables, you need to use the macro function %SYSEVALF (http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000206831.htm)

Thank you,

John

All Replies

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 10:50 AM

Hi

All macro variables are character, there are no numeric variables.

If you want to perform a calculation use the %eval macro function, for example %eval(0.636805089257504*(&A.=1)+0.656423671305948*(&A.=2));

This should give you the expected results

Cheers

Chris

All macro variables are character, there are no numeric variables.

If you want to perform a calculation use the %eval macro function, for example %eval(0.636805089257504*(&A.=1)+0.656423671305948*(&A.=2));

This should give you the expected results

Cheers

Chris

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to rivieralad

07-13-2016 11:34 AM

Apparently I need to use %SYSEVALF. Using %EVAL I get the following error.

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

0.636805089257504*(1=1)+0.656423671305948*(1=2)

ERROR: The macro CYCLE will stop executing.

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was:

0.636805089257504*(1=1)+0.656423671305948*(1=2)

ERROR: The macro CYCLE will stop executing.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 10:50 AM

1. This is logic, base math, and the fact that true/false are represented as 1/0. I don't know that that's documents anywhere but you could try searching for Boolean operators.

2. If you want to evaluate an expression while assigning macro variables you need to use %eval and/or %sysevalf. One deals with integer arithmetic and the other with floating point arithmetic.

How is SAS supposed to know if you want that as text or if you want the expression resolved? By default it treats it as text and you can override this by using the macro functions mentioned above.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Reeza

07-13-2016 11:37 AM

Thanks.

I asked because I'm pretty sure I've read about it on the help documentation and I'd like to see what's on that article.

To me it's not obvious that a statement and its truth value are one and the same. In fact, being a mathematician, conceptually they are two different things and before you told me this it didn't cross my mind that it would all be the same for SAS.

I didn't know macro variables are text by default, thanks.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 11:52 AM - edited 07-13-2016 05:28 PM

Programming unfortunately is not mathematics as we get to deal with the "rules" established by the program designers.

Numeric to true and false work two slightly different ways.

When SAS assigns the value to a comparison it will always use 1 for true and 0 for false.

But if you have a numeric variable you can use it as a boolean. If the value is not missing and not 0 SAS will treat it as true, otherwise false.

Please see the results from this brief program as a demonstration.

```
data _null_;
do x= -1 to 3, ., .5;
if (x) then put x " is true";
else put x " is false";
end;
run;
```

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

07-13-2016 02:13 PM

Instructive. Thank you very much.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 02:28 PM

You should check how SAS treats positive/negative/0 values.

Which is true/false is good to know.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to ballardw

07-21-2016 06:19 AM

I thought I had replied, I just noticed I didn't.

Thank you for this lesson.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 02:26 PM

See expressions section in the documentation.

Solution

07-13-2016
02:10 PM

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 10:53 AM

Hi,

If you are attempting to evaluate arithmetic expressions represented by values of macro variables, you need to use the macro function %SYSEVALF (http://support.sas.com/documentation/cdl/en/mcrolref/61885/HTML/default/viewer.htm#a000206831.htm)

Thank you,

John

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to sleretrano

07-13-2016 11:01 AM

Even though it's good to understand the processes and how the macro processor works ...

Within a DATA step you are probably getting improper values because of the DATA step's order of operations. When you substitute the very long version of &B in a DATA step, the order of operations is likely wrong. A simple way to solve this is to add parentheses. Instead of:

%let B= ...;

Use:

%let B=(...);

Just include the same logic as you are now but add parentheses. That will force the DATA step to perform math on the longer expression first, before using it in further calculations.

- Mark as New
- Bookmark
- Subscribe
- RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content

Posted in reply to Astounding

07-13-2016 11:38 AM

Will do, thanks.