BookmarkSubscribeRSS Feed
himalayancat
Fluorite | Level 6

Hello all,

i am trying to create a macro variable based of the value in table, but my macros are wrongly assigned.

 

table noerr
alert                  count
Note                      3
Warni                    7

table err
alert                 count
Note                    4
Error                    6
Warni                  2


%macro gen;
data _null_;
set noerr;

 

      if substr(alert,1,4)='NOTE' then do;
           %let tomail=No Errors No Warning;
     end;

     if substr(alert,1,5)='WARNI' then do;
            %let tomail=Warning found;
     end;


    if substr(alert,1,5)='ERROR' then do;
            %let tomail=Error found ;
    end;

 

run;
%mend;
%gen;
%put &=tomail;

 

1.what i want?

   a.        if  alert=error     then a           macro variable   'tomail'   should be created resolving to     'error found'.

               ( if there is 'Error' ,there is no need to assign or resolve for latter two)

 

   b.else if  alert=warni    then a           macro variable   'tomail'   should be created resolving to     'Warning found'.

   c.else if  alert=note     then a            macro variable   'tomail'   should be created resolving to     'No error found'.

 

 

Please suggest.

Thank You,

1 REPLY 1
Tom
Super User Tom
Super User

First you need to understand the relationship between the macro pre-processor and actual SAS code.

You use macro code to generate SAS code. Once SAS sees a full step it runs it.

So you ran these statements:

%let tomail=No Errors No Warning;
%let tomail=Warning found;
%let tomail=Error found ;

data _null_;
   set noerr;
   if substr(alert,1,4)='NOTE' then do;
   end;
   if substr(alert,1,5)='WARNI' then do;
   end;
   if substr(alert,1,5)='ERROR' then do;
   end;
run;

Second it looks like your sample data has multiple observations, but you are generating only one macro variable.  So you need to have logic that reflects that.

data _null_;
  set noerr end=eof;
  if alert=:'WARNI' then warnings+1;
  if alert=:'ERROR' then errors+1;
  if eof then do;
    if errors then call symputx('tomail','Errors found');
    else if warnings then call symputx('tomail','Warnings found');
    else call symputx('tomail','No Errors or Warnings');
  end;
run;

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
Mastering the WHERE Clause in PROC SQL

SAS' Charu Shankar shares her PROC SQL expertise by showing you how to master the WHERE clause using real winter weather data.

Find more tutorials on the SAS Users YouTube channel.

Discussion stats
  • 1 reply
  • 1118 views
  • 1 like
  • 2 in conversation