DATA Step, Macro, Functions and more

Exit macro within loop

Reply
Occasional Contributor
Posts: 16

Exit macro within loop

I am now doing the school payment project and preparing a SAS program to handle this.  As I want to filter out the observations from the dataset by a looping step with some conditional criteria in the following program, and syntax errors are found in the SAS log.  I have tried to omit the macro and the program work smoothly.  Please help to give me some hints to tackle this as I wish to exit the macro iteration if certain criteria match.

*************************************************************************

/* Dummy data.  Exit the macro if certain criteria match. */;
data output.test;
  input staff dept payment;
  datalines;
1 10   123456.123
2 15     13123.123
3  5      60001.123
;
run;

%let num=100;

%macro loop;
  %do x=1 %to #
      %let digit=10000;

      %if dept<10 and round(payment,&digit)>=50000 %then %do;
          %put staff= dept= paymnet=;
          %return;
      %end;
  %end;
%mend loop;

data _NULL_;
  set output.test;

  %loop;
run;
*************************************************************************

LOG
--------------------------------------
1    data test;
2      input staff dept payment;
3      datalines;

NOTE: The data set WORK.TEST has 3 observations and 3 variables.
NOTE: DATA statement used (Total process time):
      real time           0.01 seconds
      cpu time            0.01 seconds


7    ;
8    run;
9
10   %let num=100;
11
12   %macro loop;
13     %do x=1 %to &num;
14         %let digit=10000;
15
16         %if dept<10 and round(payment,&digit)>=50000 %then %do;
17             %put staff= dept= paymnet=;
18             %return;
19         %end;
20     %end;
21   %mend loop;
22
23   data _NULL_;
24     set test;
25
26     %loop;

ERROR: A character operand was found in the %EVAL function or %IF condition where a numeric operand is required. The condition was: dept<10 and round(payment,&digit)>=50000
ERROR: The macro LOOP will stop executing.

regards

may

Contributor dkb
Contributor
Posts: 53

Re: Exit macro within loop

This statement:

%if dept<10

instructs the macro compiler to compare the character string dept with the number 10.  This is why you got the message "A character operand was found ... where a numeric operand is required."


Is there any need to use macro language at all? The data step can probably do everything you need.

Occasional Contributor
Posts: 16

Re: Exit macro within loop

Thank you.

Actually in the dataset created, the DEPT is a numeric variable so I compare it with number 10.

Let me explain clearer what I want to do from the program.  The program will involve some procedure steps (not yet written above) which will be converged to certain value of payment during multiple iterations of loops.  So, I want to stop the macro and print the result once it is found, instead of executing maximun no. of macro loops, say 100.

Sorry for misleading readers.

Super Contributor
Posts: 336

Re: Exit macro within loop

.. but you don't need the macro. If you replace the data _NULL_ with:


data _NULL_;
  set test;
  Do x=1 To &num.;
    %Let digit=10000;
If dept < 10 and round (payment,&digit.)>=50000 Then Do;
   Put Staff= Dept= Payment=;
   Return;
End;
  End;

run;


Super User
Super User
Posts: 7,392

Re: Exit macro within loop

I agree with user24feb, a macro is not needed here.  I would suggest not creating global macros in code however as its not clear to read.  One other to mention, if you have to use macros, and as this is a learning exercise you may need to, then have a look at this paper: http://www2.sas.com/proceedings/forum2007/067-2007.pdf

which details all the different loop constructs, including the %do %while, which has the ability to loop until certain criteria are reached.  So instead of the do I=x to y, you could use a do loop as:

%let I=1;

%do  %while (&I <= 1000 and -your conditions to stop here-);

...

Occasional Contributor
Posts: 16

Re: Exit macro within loop

Many thanks to all of you.

regards

may

Contributor dkb
Contributor
Posts: 53

Re: Exit macro within loop

Actually in the dataset created, the DEPT is a numeric variable so I compare it with number 10.


That's a data step variable, not a macro variable; they are not the same thing.

But even if you do have a macro variable called DEPT that has a numeric value, your code

%if dept<10

still fails for the reason I gave above; you need to instruct the macro compiler to resolve the variable by coding

%if &DEPT < 10 .

Occasional Contributor
Posts: 16

Re: Exit macro within loop

Many thanks to all of you.

regards

may

Ask a Question
Discussion stats
  • 7 replies
  • 378 views
  • 0 likes
  • 4 in conversation