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