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

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. 

1 ACCEPTED SOLUTION

Accepted Solutions
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller

View solution in original post

11 REPLIES 11
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SasStatistics
Pyrite | Level 9

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;
PaigeMiller
Diamond | Level 26

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.

--
Paige Miller
SasStatistics
Pyrite | Level 9

 

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. 

Quentin
Super User

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?

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
SasStatistics
Pyrite | Level 9

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. 

Reeza
Super User
Notice the examples do not have INPUT() but your code does. You do not require the input function here as macro variables function as literal text replacement, think copy/paste.

They're text, but not character variables if that makes sense.

Anyways, remove the INPUT() from your code and it will work.
Quentin
Super User

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.

BASUG is hosting free webinars Next up: Don Henderson presenting on using hash functions (not hash tables!) to segment data on June 12. Register now at the Boston Area SAS Users Group event page: https://www.basug.org/events.
Tom
Super User Tom
Super User

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
Pyrite | Level 9
I did an other error (used &=MacroVariable) which as pointed out the in the comments below should only be used in the put statement. Anyway, this worked. Thanks Paige!
ballardw
Super User

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

sas-innovate-2024.png

Available on demand!

Missed SAS Innovate Las Vegas? Watch all the action for free! View the keynotes, general sessions and 22 breakouts on demand.

 

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
  • 11 replies
  • 2552 views
  • 8 likes
  • 6 in conversation