Date: Tue, 19 Dec 2006 15:34:53 +0000
Reply-To: iw1junk@COMCAST.NET
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Ian Whitlock <iw1junk@COMCAST.NET>
Subject: Re: Setting a macro variable inside a macro.
Summary: Possible errors and design considerations.
#iw-value=3
Mohit,
Many things can go wrong.
Perhaps you wanted to refer to SUBJ outside the macro. By default
SAS makes it local to the macro unless declared GLOBAL or previously
declared in a still executing macro. For example:
%let subj = OK ;
%Checkerrors
Perhaps you set the test variables to Y or N and are just testing
the difference between quotemarks and letters. Or maybe you are testing
the difference between lowercase letters and uppcase letters.
Perhaps the only test variable that is 'Y' is PRESAC_INVALID and the
error is in the logic of your macro.
Now if you take Richard's advice (mine is always take Richard's advice),
then you might have (assuming the test for PRESAC_INVALID being N was a
mistake)
%macro chksubj;
%* return OK or NOK * depending on value of CHKLIST;
%local chklist ;
%let chklist = &prod_class_invalid&prod_type_invalid&source_invalid
&contact_invalid&presac_invalid&acct12_missing
&sac_date_missing ;
%scan( OK NOK, (%index(&chklist, Y)>0) + 1 )
%mend chksubj ;
%let subj = %chksubj ;
However, with this technique, you might not even need the variable, since
you have the value.
It is still questionable where the values of the test variables are coming
from so one might get better control by adding the appropriate parameter.
%macro chk
(chklist=prod_class_invalid prod_type_invalid source_invalid
contact_invalid presac_invalid acct12_missing
sac_date_missing);
%* return OK or NOK * depending on value of modified CHKLIST;
%let chklist = %str( )%sysfunc(compbl(&chklist)) ;
%let chklist = %sysfunc(tranwrd(&chklist,%str( ),%nrstr(&))) ;
%let chklist = %upcase(&chklist) ;
%scan( OK NOK, (%index(&chklist, Y)>0) + 1 )
%mend chk ;
%let subj = %chk() ;
Now you can even vary which variables go into the test and the parameter
makes it clear that you are giving the macro explicit permission to access
the test variables. Moreover, either suggestion allows the test variables
to have the empty value.
The remaining question is - why should SUBJ have the values OK or NOK
instead of Y or N. Standardization helps. Personally I like 1 and 0.
Then the final line simplifies to
%eval(&chklist>0)
and positive integers can be used to indicate various types of errors ( 1
invalid because not asked, 2 invalid because missassigned, 3 invalid
because not known, 4 other, etc.).
Ian Whitlock
=================
Date: Tue, 19 Dec 2006 16:03:01 +0530
Reply-To: mohit.b.bhatia@AEXP.COM
Sender: "SAS(r) Discussion"
From: Mohit Bhatia <mohit.b.bhatia@AEXP.COM>
Subject: Setting a macro variable inside a macro.
Content-Type: text/plain; charset=us-ascii
Hi,
I want to set a macro variable subj inside a macro based on a condition.
But this is not working. Kindly
suggest why. Is the statement inside IF ok?
%macro checkerrors;
%if (&prod_class_invalid='Y' or &prod_type_invalid='Y'or
&source_invalid='Y' or &contact_invalid='Y' or &presac_invalid='N' or
&acct12_missing ='Y' or &sac_date_missing='Y') %then %do;
%let subj=NOK;
%end;
%else %do;
%let subj=OK;
%end;
%mend checkerrors;
%checkerrors;
American Express made the following
annotations on 12/19/06, 03:33:08
---------------------------------------------------------------------------
---
***************************************************************************
***
"This message and any attachments are solely for the intended recipient and
may contain confidential or privileged information. If you are not the
intended recipient, any disclosure, copying, use, or distribution of the
information included in this message and any attachments is prohibited. If
you have received this communication in error, please notify us by reply e-
mail and immediately and permanently delete this message and any
attachments. Thank you."
American Express a ajout?e commentaire suivant le 12/19/06, 03:33:08
Ce courrier et toute pi? jointe qu'il contient sont r?rv?au seul
destinataire indiqu?t peuvent renfermer des renseignements confidentiels et
privil?? Si vous n'?s pas le destinataire pr?, toute divulgation,
duplication, utilisation ou distribution du courrier ou de toute pi? jointe
est interdite. Si vous avez re?cette communication par erreur, veuillez
nous en aviser par courrier et d?uire imm?atement le courrier et les pi?s
jointes. Merci.
***************************************************************************
***
==========================================================================
===