BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
LuRo
Calcite | Level 5

I am learning SAS. I tested a function shown below, the function is to find the larger value of two integers. However, it kept reporting errors shown below. Could someone please help me with that? Thank you.

 

%macro max(dat);
data _null_;
set &dat;
if x >= y then put x =;
else put y =;
%mend max;

data temp;
input x y;
cards;
7 3
;
run;

proc print data=temp;
run;

%max(temp);
2633  %max(temp);
NOTE: Line generated by the invoked macro "MAX".
1       data _null_; set &dat; if x >= y then put x =; else put y =;
                         -
                         22
                         200
ERROR: File WORK.DAT.DATA does not exist.
ERROR 22-322: Syntax error, expecting one of the following: a name, a quoted string, ;, CUROBS,
              END, INDSNAME, KEY, KEYRESET, KEYS, NOBS, OPEN, POINT, _DATA_, _LAST_, _NULL_.

ERROR 200-322: The symbol is not recognized and will be ignored.
1 ACCEPTED SOLUTION

Accepted Solutions
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You must have some other code running or code has hung over.  Start a new session as the code runs fine for me in a new session.  Also couple of bits of advice:

%macro max(dat);
  data _null_;
    set &dat.;
    if x >= y then put x =;
    else put y =;
  run;
%mend max;

data temp;
  input x y;
cards;
7 3
;
run;

%max(temp);

Always finish macro variables with a dot - a lot of the time you will get away with it, but sometimes you will not, therefore good practive to always do it.

Always finish steps, in this case the run; was missing in the macro.  

Indent code to make it readable.

Lastly and most important, I know this one is for learning, but knowing when macro adds anything to the code is a key skill.  It is often misused.  Try to do everything in Base SAS (datastep or proc), then when you see a benefit to macro'tizing the code (e.g. for a global tool) then put the effort in to do proper SDLC on it.  The worst code out there is undocumented, obfuscated macro heavy code.

View solution in original post

8 REPLIES 8
gamotte
Rhodochrosite | Level 12

A "run" is missing at the end of the data step in the macro. Other than that, i don't see any problem with your code.

 

20   %macro max(dat);
21   data _null_;
22   set &dat;
23   if x >= y then put x =;
24   else put y =;
25   run;
26   %mend max;
27
28   data temp;
29   input x y;
30   cards;

NOTE: The data set WORK.TEMP has 1 observations and 2 variables.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds


32   ;
33   run;
34
35   proc print data=temp;
36   run;

NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: PROCEDURE PRINT used (Total process time):
      real time           0.07 seconds
      cpu time            0.00 seconds


37
38   %max(temp);

x=7
NOTE: There were 1 observations read from the data set WORK.TEMP.
NOTE: DATA statement used (Total process time):
      real time           0.00 seconds
      cpu time            0.00 seconds
LuRo
Calcite | Level 5
Thanks a lot for the reply. The bug was fixed by restarting SAS.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

You must have some other code running or code has hung over.  Start a new session as the code runs fine for me in a new session.  Also couple of bits of advice:

%macro max(dat);
  data _null_;
    set &dat.;
    if x >= y then put x =;
    else put y =;
  run;
%mend max;

data temp;
  input x y;
cards;
7 3
;
run;

%max(temp);

Always finish macro variables with a dot - a lot of the time you will get away with it, but sometimes you will not, therefore good practive to always do it.

Always finish steps, in this case the run; was missing in the macro.  

Indent code to make it readable.

Lastly and most important, I know this one is for learning, but knowing when macro adds anything to the code is a key skill.  It is often misused.  Try to do everything in Base SAS (datastep or proc), then when you see a benefit to macro'tizing the code (e.g. for a global tool) then put the effort in to do proper SDLC on it.  The worst code out there is undocumented, obfuscated macro heavy code.

LuRo
Calcite | Level 5
Thanks a lot for the detailed reply. The problem was solved by starting a
new session.
RW9
Diamond | Level 26 RW9
Diamond | Level 26

No probs.  For future note, things can get stuck in the SAS compiler.  The reason is that SAS interprets step by step, so if there is unbalanced quotes - one problem which often comes up, then until that is closed with another quote, then anything submitted from there on is just plain text until the quote is ended.  If you have oddities in your code like this, its usually simpler to start a new session.

s_lassen
Meteorite | Level 14

Very strange. When I submit your code, there is no problem (except that you miss the final RUN statement, so that nothing happens). Are you sure that you did not submit "set & dat" or something like that?

 

LuRo
Calcite | Level 5
Thanks a lot for replying. By restarting SAS the problem was solved.
ballardw
Super User

And in a more esoteric mode: SAS has a data step function called MAX.

 

data _null_;

   x= 4;

   y= 3;

   z= 18;

   a= max(x,y,z);

   put a=;

run;

 

It is not a good idea to create code with the name of an existing SAS function, especially one that behaves differently than the SAS supplied version as your code may be misinterpreted at a later date.

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 8 replies
  • 872 views
  • 4 likes
  • 5 in conversation