I have saved two macro variables which I know is saved as text. But there are "numbers" and I would like to use them in a calculation. Any suggestions on how to do this nicely?
How I thought, roughly, that I could solve it:
data MyCalculation;
MyAnswer = input(&=Numerator) / input(&=Denominator);
run;
Any suggestions on how to do this nicely?
Thanks.
You can do this:
data MyCalculation;
MyAnswer = &Numerator / &Denominator;
run;
It is NOT necessary to save numbers in macro variables in most cases, and you could find the macro variables truncate the number to a certain number of decimal places (depending on how you do it). Yes, there are unusual cases where that is necessary, but for simple division of two variables, use DATA step variables, where the full number of digits is available.
You can do this:
data MyCalculation;
MyAnswer = &Numerator / &Denominator;
run;
It is NOT necessary to save numbers in macro variables in most cases, and you could find the macro variables truncate the number to a certain number of decimal places (depending on how you do it). Yes, there are unusual cases where that is necessary, but for simple division of two variables, use DATA step variables, where the full number of digits is available.
Doing that gives the error:
ERROR 386-185: Expecting an arithmetic expression.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, a numeric constant, a datetime constant,
a missing value, arrayname, (, +, -, INPUT, NOT, PUT, ^, _NEW_, ~.
ERROR 200-322: The symbol is not recognized and will be ignored.
30 run;
To debug macro code and code with macro variables, add this command as the first line of your program and run it again.
options mprint symbolgen mlogic;
Then show us the entire log for this DATA step, including code as it appears in the log, plus all ERRORs, NOTEs and WARNINGs.
PS: you should always show us the entire log for the step that has the errors, not just the error messages.
I calculate the number of rows in a data set (DataA) and store it in a Macro variable (wether this is a good way of doing it or not I don't know but it works):
data _null_; if 0 then set DataA nobs=n; call symputx('Numerator',n); stop; run; %put &=Numerator;
Then I do the same for another data set (DataB) and store it in a Macro variable:
data _null_;
if 0 then set DataB nobs=n;
call symputx('Denominator',n);
stop;
run;
%put &=Denominator;
Then I want to calculate the fraction (Numerator/Denominator) by using the macro variables.
What I tried was then:
data MyCalculation;
MyAnswer = input(&=Numerator) / input(&=Denominator);
run;
Unfortunately I cannot give the complete log and original variable names now since it is company data I am using. But the principle can be seen by the code here i hope.
That suggests your macro variables may have something besides a number.
1 %let numerator=1 ; 2 %let denominator=2 ; 3 4 data MyCalculation; 5 MyAnswer = &Numerator / &Denominator; 6 put MyAnswer= ; 7 run; MyAnswer=0.5 NOTE: The data set WORK.MYCALCULATION has 1 observations and 1 variables.
Can you show your full code, and full log?
I calculate the number of rows in a data set (DataA) and store it in a Macro variable (wether this is a good way of doing it or not I don't know but it works):
data _null_; if 0 then set DataA nobs=n; call symputx('Numerator',n); stop; run; %put &=Numerator;
Then I do the same for another data set (DataB) and store it in a Macro variable:
data _null_;
if 0 then set DataB nobs=n;
call symputx('Denominator',n);
stop;
run;
%put &=Denominator;
Then I want to calculate the fraction (Numerator/Denominator) by using the macro variables.
What I tried was then:
data MyCalculation;
MyAnswer = input(&=Numerator) / input(&=Denominator);
run;
Unfortunately I cannot give the complete log and original variable names now since it is company data I am using. But the principle can be seen by the code here i hope.
As @Reeza, the value of a macro variable is text. Or you can think of it as code. But it's not a "character value" like the data step language character value. The (main) job of the macro language is to generate SAS code. You can think of it as code substitution.
So if you have working SAS code:
data want ;
set sashelp.class ;
MyAnswer = 1 / 2;
run ;
You could also code:
%let numerator=1 ;
%let denominator=2 ;
data want ;
set sashelp.class ;
MyAnswer = &Numerator / &Denominator;
run ;
Because the below step works:
data want ;
set sashelp.class ;
MyAnswer = height / weight;
run ;
You could also code:
%let numerator=height ;
%let denominator=weight ;
data want ;
set sashelp.class ;
MyAnswer = &Numerator / &Denominator;
run ;
When you think about macro variables resolving, think about generating code. You're not dealing with a data step character value. All the macro variables resolve before the step even compiles.
After your macro variables resolve, SAS will compile the step, then execute the step, and you will see if there were any errors.
First problem: &=mvar is for %PUT statement only. To reference the macro variable normally just use &mvar.
Second problem: There is no need for the INPUT() function call. The macro variable value becomes the text of the SAS program. You don't need to call INPUT() to type a numeric constant into your SAS program.
Minor issue: You can simplify the logic of getting NOBS= value into a macro variable.
data _null_;
call symputx('Numerator',n);
stop;
set DataA nobs=n;
run;
data _null_;
call symputx('Denominator',n);
stop;
set DataB nobs=n;
run;
data MyCalculation;
MyAnswer = &Numerator / &Denominator ;
run;
@SasStatistics wrote:
I have saved two macro variables which I know is saved as text. But there are "numbers" and I would like to use them in a calculation. Any suggestions on how to do this nicely?
How I thought, roughly, that I could solve it:
data MyCalculation; MyAnswer = input(&=Numerator) / input(&=Denominator); run;
Any suggestions on how to do this nicely?
Thanks.
I don't know where you got the idea to use &=Numerator but that is not the way to reference a macro variable.
Macro variable reference would be &numerator.
INPUT requires a CHARACTER value so you have yet another problem. If you used Numerator properly and was actually text that resolves to a numeric value then the INPUT would fail because input would expect unquoted text to be a variable name and numbers wouldn't be valid or the name of a variable. Third the Input function requires the Informat to be used for converting text to numeric.
SHOW us the values of the macro variables concerned.
Registration is now open for SAS Innovate 2025 , our biggest and most exciting global event of the year! Join us in Orlando, FL, May 6-9.
Sign up by Dec. 31 to get the 2024 rate of just $495.
Register now!
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.
Ready to level-up your skills? Choose your own adventure.