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.
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
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.
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.
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;
Instructive. Thank you very much.
You should check how SAS treats positive/negative/0 values.
Which is true/false is good to know.
I thought I had replied, I just noticed I didn't.
Thank you for this lesson.
See expressions section in the documentation.
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
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.
Will do, thanks.
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!
What’s the difference between SAS Enterprise Guide and SAS Studio? How are they similar? Just ask SAS’ Danny Modlin.
Find more tutorials on the SAS Users YouTube channel.