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

Showing results for

Find a Community

- Home
- /
- SAS Programming
- /
- Base SAS Programming
- /
- arithmetic operation using macro variables

- 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-31-2015 11:35 AM

I have two macro variables that correctly resolved from the following PROC SQL functions.

proc sql;

select count(*) as Total into :totaln from test1;

quit;

proc sql;

select count(*) as Total into artn from test2;

quit;

Although they are both macro (character) variables, in essence they are numeric constants from which I am interested in calculating a percentage: pc = (partn/totaln )* 100.

%macro pc;

%let totaln=%left(&totaln);

%let partn=%left(&partn);

t=inputn(&totaln,8.);

p=inputn(&partn,8.);

%let pc=%syseval((p/t)*100);

title "Percent represented: &pc.% ";

%mend pc;

%pc;

The above attempt did not work. The macro does not let me create a constant t or p and gives me an error message: ERROR 180-322: Statement is not valid or it is used out of proper order.

How do I turn macro character to numeric variable to perform an arithmetic operation?

Accepted Solutions

Solution

07-31-2015
01:23 PM

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

Posted in reply to sonicpoem

07-31-2015 01:23 PM

I think you're looking for something like:

%let pc = %syseval**f **( (&partn / &totaln)*100);

You may want to round the result to desired number of decimals such as

%let pc = %sysfunc(round(&pc, 0.01));

All Replies

Solution

07-31-2015
01:23 PM

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

Posted in reply to sonicpoem

07-31-2015 01:23 PM

I think you're looking for something like:

%let pc = %syseval**f **( (&partn / &totaln)*100);

You may want to round the result to desired number of decimals such as

%let pc = %sysfunc(round(&pc, 0.01));

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

Posted in reply to sonicpoem

07-31-2015 02:16 PM

Thank you. It worked!