Solved
Contributor
Posts: 66

# Calculations using Macros

Please help me understand what is wrong with my code.

I have a macro variable in the following format

%Put*****************mm=&mm;

which gives mm=12,345.678  .....has a comma due to formatting, if not acceptable , I can get rid of it.

I want to use this variable to divide a  variable in a data set.  This is what I have tried.

Data have;

input  aa @@;

Datalines;

10000   20000  30000

;

Data want;

set have;

NewVar=aa/%sysevalf(&mm);

run;

I have also tried doing

Data want;

set have;

NewVar=aa/%sysevalf(&nn);   /*   Where &nn=12345.678   */

run;

None of them seem to work!!!

Thanks for your attention.

Accepted Solutions
Solution
‎07-25-2013 02:53 PM
Super Contributor
Posts: 339

## Re: Calculations using Macros

You simply can't use %sysevalf outside a macro block. However, you don't need it for what you ought to do. Consider SAS programs to be compiled then executed. Macros and macro variables revolve during the first (compile) phase and then whatever they resolved to act as "parsed code text" or syntax if you will.

Thus, something as simple as

%let mm=12,345.678;

data want;

set have;

newvar=aa/&mm;

run;

would be exactly identical to

data want;

set have;

newvar=aa/12,345.678;

run;

As such, you can't have a macro variable representing a number have a format that differs from its coding syntax representation. If you need further help to solve the , issue (assuming you import multiple macro variables from a dataset, for example, reply for more help with more details on how you retrieve your macro variables.

So the working code would be as follow:

%let nn=12345.678;

data want;

set have;

newvar=aa/&nn;

run;

Vincent

All Replies
Solution
‎07-25-2013 02:53 PM
Super Contributor
Posts: 339

## Re: Calculations using Macros

You simply can't use %sysevalf outside a macro block. However, you don't need it for what you ought to do. Consider SAS programs to be compiled then executed. Macros and macro variables revolve during the first (compile) phase and then whatever they resolved to act as "parsed code text" or syntax if you will.

Thus, something as simple as

%let mm=12,345.678;

data want;

set have;

newvar=aa/&mm;

run;

would be exactly identical to

data want;

set have;

newvar=aa/12,345.678;

run;

As such, you can't have a macro variable representing a number have a format that differs from its coding syntax representation. If you need further help to solve the , issue (assuming you import multiple macro variables from a dataset, for example, reply for more help with more details on how you retrieve your macro variables.

So the working code would be as follow:

%let nn=12345.678;

data want;

set have;

newvar=aa/&nn;

run;

Vincent

Super User
Posts: 8,111

## Re: Calculations using Macros

Posted in reply to Vince28_Statcan

You can use COMPRESS() function to remove the commas.

%let mm=12,345.678;

%let n=%sysfunc(compress(%superq(mm),%str(,)));

%put mm=&mm n=&n;

Or you could use the INPUT() function in the SAS code.

data x;

y=input("&mm",comma32.);

put y=;

run;

Contributor
Posts: 66

## Re: Calculations using Macros

Posted in reply to Vince28_Statcan

Hi Vince28,

Thanks for the help. As you pointed our it was unnecessary use of   %sysevalf function out side a macro block...something I was not aware of.

Have a good day.

Raghu.

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
• 3 replies
• 230 views
• 3 likes
• 3 in conversation