Hi SAS Forum,
I did a literature survey to understand the difference between “If-then-do-end” vs. %if %then %do %end.
Below conditional logic code (SUGI29 paper 243-29 by Slaughter and Delwiche acknowledged) prints data set called “orders” if the current day is Monday. And creates a table using proc tabulate if the current day is Friday.
%MACRO reports;
%IF &SYSDAY = Monday %THEN %DO;
PROC PRINT DATA = orders NOOBS;
FORMAT OrderDate DATE7.;
TITLE "&SYSDAY Report: Current Orders";
%END;
%ELSE %IF &SYSDAY = Friday %THEN %DO;
PROC TABULATE DATA = orders;
CLASS CustomerID;
VAR Quantity;
TABLE CustomerID ALL, Quantity;
TITLE "&SYSDAY Report: Summary of Orders";
%END;
%MEND reports;
RUN;
%reports
RUN;
Q: Why cannot we simply replace all the places having %IF %THEN %DO %END and %ELSE %IF %THEN %DO %END with standard IF THEN DO END and ELSE IF THEN DO END ?
Sorry if my question looks like a stupid one.
Miris
IF/THEN/DO/etc work inside a data step and or procs.
%IF/%THEN/%DO/%etc work in macro code, for example not in a data step. So they are used to conditionally execute a data step/proc.
IF/THEN/DO/etc work inside a data step and or procs.
%IF/%THEN/%DO/%etc work in macro code, for example not in a data step. So they are used to conditionally execute a data step/proc.
There are many languages being mixed in your daily SAS usage.
SAS macro language is different from the data-step and that is different to using a proc and there are more.
See: SAS(R) 9.3 Macro Language: Reference how it works.
As being different languages being processed at different moments the interactions can be sometimes a challenge.
It is the programmers task to understand that.
Same intention, different words as Reeza.
Macro logic is used to conditionally generate code that is then passed onto regular SAS to interpret and execute. In another sense you can think of them as two separate processes. The different syntax (% versus no %) are what keeps the interpreter (and the programmer) from getting confused.
Hi:
I explained a lot of these concepts for the beginner in this paper:
http://support.sas.com/resources/papers/proceedings13/120-2013.pdf
cynthia
Hi Reeza, Jaap, Tom and Cynthia,
Many thanks to each one of you for this help.
Hi Cynthia,
Your paper is very helpful for me. Thank you very much for it.
Regards
Miris
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
Learn the difference between classical and Bayesian statistical approaches and see a few PROC examples to perform Bayesian analysis in this video.
Find more tutorials on the SAS Users YouTube channel.