Date: Sat, 4 Apr 2009 19:09:17 +0200
Reply-To: Rune Runnestø <rune@FASTLANE.NO>
Sender: "SAS(r) Discussion" <SAS-L@LISTSERV.UGA.EDU>
From: Rune Runnestø <rune@FASTLANE.NO>
Subject: Re: A problem with macro logic
Thanks for the answer about the missing comma.
Now another problem has encountered. Below is a macro definition and a call
for that macro. It can be pasted right into the editor and submitted. There
are two inparameters, ALL_RECORDS and COUNT_RECORDS. They refer to records
in a dataset, but the dataset is irrelevant to understand my problem, so I
have cut off much of the code.
The problem is how to make the code bullet-proof for unintended values of
COUNT_RECORDS. I don't consider ALL_RECORDS not to be bullet-proof, because
it you neither write 'no' or 'yes', you will not end in a wrong conditional
place in the code, but you will end up in LOC 1.
But with COUNT_REOCORDS it is different. The user is just inended to type
numeric counts here. They may as well be negative with a minus-sign in front
of them or left out. But letters like 'problem' or leading zeros like in
'07' gives me a problem. How can I strip off eventually leading zeros, and
how can I make a conditonal logic that will lead values like 'problem' or
'rr' or 'whatever' in their own logical category (LOC 7, for instance)?
Rune
%macro printing_macro_logic( all_records = ,
count_records =
);
%global numobs;
%let numobs = 27;
%PUT &numobs;
/* Check if the inparameter 'all_records' is filled out with YES or NO */
%if %upcase(&all_records) ^= YES and %upcase(&all_records) ^= NO %then
%do;
%put LOC 1: COUNT_RECORDS = &count_records ALL_RECORDS = &all_records;
%end;
/* If 'all_records' is filled out with 'yes' */
%else %if %upcase(&all_records) = YES %then %do;
%put LOC 2: COUNT_RECORDS = &count_records ALL_RECORDS = &all_records;
%end;
/* Otherwise, if 'all_records' is filled out with 'no' */
%else %if %upcase(&all_records) = NO %then %do;
%if &count_records = &numobs %then %do;
%put LOC 3: COUNT_RECORDS = &count_records ALL_RECORDS =
&all_records;
%end;
%if (&count_records > &numobs) %then %do;
%put LOC 4: COUNT_RECORDS = &count_records ALL_RECORDS =
&all_records NUMOBS = &numobs;
%return;
%end;
%else %if (&count_records <= 0 or &count_records = ) %then %do;
%put LOC 5: COUNT_RECORDS = &count_records ALL_RECORDS =
&all_records;
%return;
%end;
%else %if (&count_records >= 1 and &count_records < &numobs) %then
%do;
%put LOC 6: COUNT_RECORDS = &count_records ALL_RECORDS =
&all_records;
%end;
%end;
%mend;
%printing_macro_logic( all_records = no,
count_records = problem
);