## Help to understand If-then-do-end vs. %if %then %do %end

Solved
Super Contributor
Posts: 338

# Help to understand If-then-do-end vs. %if %then %do %end

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

Accepted Solutions
Solution
‎03-12-2014 03:20 PM
Super User
Posts: 23,700

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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.

All Replies
Solution
‎03-12-2014 03:20 PM
Super User
Posts: 23,700

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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.

Posts: 3,215

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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.

---->-- ja karman --<-----
Super User
Posts: 8,090

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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.

SAS Super FREQ
Posts: 9,367

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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

Super Contributor
Posts: 338

## Re: Help to understand If-then-do-end vs. %if %then %do %end

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

🔒 This topic is solved and locked.