Hello Experts,
Do you know, please, how to to sum 2 macro variables with € format ? For exemple, if I have in &TOTAL_S 1 € and in &TOTAL2_Another_2 3 €, the sum in &TOTAL_SUP2 must be 4 €.
In case when the table is empty (no observation), how to create the macro variable with 0 value ?
My code is :
options locale=FR;
data matable_bis;
set matable (obs=1);
TOTAL2=propcase((put(TOTAL,NLMNLEUR12.0)));
keep TOTAL TOTAL2;
run;
/***if my table matableenPlus2 is empty (no observation),
how to create the macrovariable ? ***/
data matableenPlus2;
set matableenPlus;
TOTAL2_Another=propcase((put(Another_total,NLMNLEUR12.0)));
keep TOTAL2_Another;
run;
proc sql noprint;
select TOTAL2 into : TOTAL_S from matable_bis;
quit;
proc sql noprint;
select TOTAL2_Another into : TOTAL2_Another_S from matableenPlus2;
quit;
/****How to sum 2 macro variables with format ? ***/
%let TOTAL_SUPP2 = %sysfunc(&TOTAL_S., &TOTAL2_Another_S.);
Thank you for your help !
You run into 2 problems with that format. One is that the COMMA that may get placed in the value (national language settings) means that would be treated as a delimiter by the macro language. Then just having the currency symbol means that the result is not treated as a number even with the proper function calls.
Example using US currency as but Euros will do the same. Note the conversion note only shows PART of the VAL1 in the error message. That is because of the comma in the value.
80 %let val1 = $123,456.77; 81 %let val2 = $45.12; 82 83 %let sum= %sysevalf(&val1. + &val2.); ERROR: Unknown %SYSEVALF conversion operand '456.77 + $45.12' specified; conversion is terminated. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: $123 84 /* error so no value*/ 85 86 87 %let val11 = 123456.77; 88 %let val22 = 45.12; 89 90 %let sum2= %sysevalf(&val11. + &val22.); 91 %put sum is: &sum2.; sum is: 123501.89
%sysfunc is going to require at least one data step function, such as SUM, but still will fail because of the currency symbols.
Can you post an example of what the two macro variables look like?
Why do you go to such lengths to store the values as character with a currency format when you later need the raw numeric values?
Anyway, you can always use the original variables in the SELECT INTO without a format.
Which value do you want in case there's no observations? Zero or missing?
Thank you for your answer.
I specify : I would like to improve my code : I need to sum another variable.
So, I have the code that calculate the total, after I put the total in € format.
Now, I need to add to this total another total, I'm wondering if I can sum 2 macro variables.
Another problem : when I calculat another total, sometimes I can have no values in the tables (empty table), in this case I woul like to add 0.
Of course you can sum macro variables, if they contain numbers in a programming sense (only digits, a decimal dot, and possibly a leading sign). That's why you MUST use the unformatted values.
Repeating what has already been said:
You have:
data matable_bis;
set matable (obs=1);
TOTAL2=propcase((put(TOTAL,NLMNLEUR12.0)));
keep TOTAL TOTAL2;
run;
/***if my table matableenPlus2 is empty (no observation),
how to create the macrovariable ? ***/
data matableenPlus2;
set matableenPlus;
TOTAL2_Another=propcase((put(Another_total,NLMNLEUR12.0)));
keep TOTAL2_Another;
run;
Do the sum on data set variables TOTAL and ANOTHER_TOTAL, do not sum macro variables that have characters in them.
You run into 2 problems with that format. One is that the COMMA that may get placed in the value (national language settings) means that would be treated as a delimiter by the macro language. Then just having the currency symbol means that the result is not treated as a number even with the proper function calls.
Example using US currency as but Euros will do the same. Note the conversion note only shows PART of the VAL1 in the error message. That is because of the comma in the value.
80 %let val1 = $123,456.77; 81 %let val2 = $45.12; 82 83 %let sum= %sysevalf(&val1. + &val2.); ERROR: Unknown %SYSEVALF conversion operand '456.77 + $45.12' specified; conversion is terminated. ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: $123 84 /* error so no value*/ 85 86 87 %let val11 = 123456.77; 88 %let val22 = 45.12; 89 90 %let sum2= %sysevalf(&val11. + &val22.); 91 %put sum is: &sum2.; sum is: 123501.89
%sysfunc is going to require at least one data step function, such as SUM, but still will fail because of the currency symbols.
SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.