BookmarkSubscribeRSS Feed
samirt
Fluorite | Level 6

Hi All,

 

I have missing values in one of the macro variable. While performing arithmetic operation the result shows missing value. Is there any way to handle the missing macro value. For Example in Subtract macro value there is one missing value.

I want the Subtract to show 

Subtract= . - 10 - 20  i.e.   -10

 

%let a=.;

%let b=10;

%let c=20;

 

data _null_;

    %let Add=%sysevalf(&a+&b+&c);

   %let Subtract=%sysevalf(&a-&b-&c);

   %let Div=%sysevalf(&a/&b);

run;

    

%put &Add is .

%put &Subtract is .

%put &Div is .

 

 

 

3 REPLIES 3
Kurt_Bremser
Super User

1. A mathematical operation invovlving missing values will always yield missing values. In Base SAS and in the macro engine.

 

2. Your math is skewed. 0 - 10 - 20 = -30 and not -10.

 

3. You will be better off doing calculations in a data _null_ step and setting macro variables with call symput. This allows the easy use of functions that are robust with regard to missing values (eg the sum() function).

 

4. Your construct:

data _null_;
    %let Add=%sysevalf(&a+&b+&c);
   %let Subtract=%sysevalf(&a-&b-&c);
   %let Div=%sysevalf(&a/&b);
run;

is bogus. This creates an empty data _null_ step that does nothing (besides annoying the SAS interpreter); the macro statements are dealt with by the macro engine before the data step is compiled and executed. Macro statements do not need data or proc steps around them. They might need a macro definition (%if and %do, which are not allowed in open code).

mohamed_zaki
Barite | Level 11

If you want to use %sysevalf, then use only nonmissing values within the first argument to the %SYSEVAL function.

 

As one work around make sure your macro variables is not equal to missing values like 

 

%let a=%sysfunc(sum(.,0));
%let b=%sysfunc(sum(10,0));
%let c=%sysfunc(sum(20,0));
 

 By that any missing values will be equal to zero.

RW9
Diamond | Level 26 RW9
Diamond | Level 26

No, the real question here is why on earth are you doing numerical imputations in a textual code generation system?  I mean do you use Word as a calculator?  Let me explain:

Macro language is a technique of generating text, it has some conditionals and what not, but it not itself compilable code, it is merely there to help generate compilable code to save you having to type things.  Base SAS is the compilable code, it is there you would do all your computations, data manipulations etc.  If for some unkown reason you do need a result in a macro variable, then @Kurt_Bremser has provided you with an answer, use Base SAS data _null_ and call symput to generate the macro variable.

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

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
  • 3 replies
  • 1374 views
  • 1 like
  • 4 in conversation