BookmarkSubscribeRSS Feed
wongmay
Calcite | Level 5

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

7 REPLIES 7
dkb
Quartz | Level 8 dkb
Quartz | Level 8

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.

wongmay
Calcite | Level 5

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.

user24feb
Barite | Level 11

.. 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;


RW9
Diamond | Level 26 RW9
Diamond | Level 26

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-);

...

wongmay
Calcite | Level 5

Many thanks to all of you.

regards

may

dkb
Quartz | Level 8 dkb
Quartz | Level 8

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 .

wongmay
Calcite | Level 5

Many thanks to all of you.

regards

may

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
  • 7 replies
  • 2583 views
  • 0 likes
  • 4 in conversation