When to use % in if then Macro.

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

When to use % in if then Macro.

Any one can help explain when to put % in if then statement , for example %if *** %then %do. In the example below, should I use % or not in if then statement.
One more question, can I use "Do i = 1 to n while (condition)" statement here like this? It’s not array, they are all observations in the data sets.

%MACRO FUNDSOURCE(I);
DATA SASDATA.STUDENT&I;
SET SASDATA.STUDENTLIST
DO M = 1 TO 310 WHILE(&&BUDG&I > 0); /*loop through all observations_ALL STUDENTS*/
IF &&BUDG&I LE 3000- FA_TOT THEN do;
DISBURSE = &&BUDG&I;
FA_TOT +DISBURE;
&&BUDG&I - DISBURSE;
end;
ELSE IF &&BUDG&I GT (3000- FA_TOT) THEN DO;
DISBURSE = 3000-FA_TOT;
FA_TOT + DISBURSE;
&&BUDG&I - DISBURSE;
END;
END;

IF _n_ > M THEN DELETE; /*if budget are all gone, delete other observations, keep observations only for the student who get funds*/
RUN;
%MEND FUNDSOURCE;

Accepted Solutions
Solution
‎06-06-2018 12:32 AM
Trusted Advisor
Posts: 1,837

Re: When to use % in if then Macro.

Your question: when to use %IF and what is the difference between %IF and IF:

 

IF is used in a data step to execute a stetement or group of stetements (using DO ...END) conditionally based on data.

 

%IF is used in a macro program. Macro program is used to generate sas code. 

Thus, %IF will select the code to be generated according to the condition, based on given arguments to the macro program.

It doesn't check data.

 

%IF is executed once at time of "compilation" of the macro program.

IF is executed in a data step for every input row.

View solution in original post


All Replies
Valued Guide
Posts: 597

Re: When to use % in if then Macro.

Super User
Posts: 23,778

Re: When to use % in if then Macro.

Posted in reply to SuryaKiran

@SuryaKiran SAS V8 is pretty old (17ish years), the SAS 9.4 reference is different and %IN has changed enough between 8 and 9 that its best to use the correct version of the documentation. 

 

http://documentation.sas.com/?docsetId=mcrolref&docsetTarget=n1alyfc9f4qrten10sd5qz5e1w5q.htm&docset...

 

 

Super User
Posts: 23,778

Re: When to use % in if then Macro.



Any one can help explain when to put % in if then statement , for example %if *** %then %do. In the example below, should I use % or not in if then statement.

 

You would not use %IN here. You would use it if you need to have macro logic and say loop a data step, not within a data step. There are cases of using %IN within a data step, for example if you're creating a series of variables where the index is unknown. In general, keeping your macro logic simple is your best bet.

 



One more question, can I use "Do i = 1 to n while (condition)" statement here like this? It’s not array, they are all observations in the data sets.



There's no requirement that do loops use an array, this is perfectly valid. As long as you realize it's looping for every line and that's what you intend. Basically, it's valid syntax, but that doesn't mean its correct for your logic (I don't know what your logic is so just trying to be clear here). 

 


@Chaichai wrote:
Any one can help explain when to put % in if then statement , for example %if *** %then %do. In the example below, should I use % or not in if then statement.
One more question, can I use "Do i = 1 to n while (condition)" statement here like this? It’s not array, they are all observations in the data sets.

%MACRO FUNDSOURCE(I);
DATA SASDATA.STUDENT&I;
SET SASDATA.STUDENTLIST
DO M = 1 TO 310 WHILE(&&BUDG&I > 0); /*loop through all observations_ALL STUDENTS*/
IF &&BUDG&I LE 3000- FA_TOT THEN do;
DISBURSE = &&BUDG&I;
FA_TOT +DISBURE;
&&BUDG&I - DISBURSE;
end;
ELSE IF &&BUDG&I GT (3000- FA_TOT) THEN DO;
DISBURSE = 3000-FA_TOT;
FA_TOT + DISBURSE;
&&BUDG&I - DISBURSE;
END;
END;

IF _n_ > M THEN DELETE; /*if budget are all gone, delete other observations, keep observations only for the student who get funds*/
RUN;
%MEND FUNDSOURCE;

 

Occasional Contributor
Posts: 5

Re: When to use % in if then Macro.

Thank you. You might misunderstand my first question, while you did solve my problem and let me know when to use %, also I know how to use do while condition.

Thanks

 

Super User
Posts: 10,283

Re: When to use % in if then Macro.

[ Edited ]

You've approached the issue from the wrong direction by starting out with macro programming.

Instead follow the proven path:

  1. Create working Base SAS code and test it
  2. Identify parts that need to be made dynamic
  3. Replace those with macro variables, and test again after manually setting those macrovars
  4. NOW think of a macro mechanism that creates the dynamic parts; these might come from macro parameters or be created in a macro loop.
  5. Only now implement your findings from step 4 in macro code

When you follow this guideline, the question for when to use if or %if will not even arise. Everything that needs a "normal" if (or do while) will be already there in step 1, while %if (or %do %while) will not need to be used before step 5(!)

 

Edit: addendum to step 4: sometimes you might find you don't need any macro elements at all by using call execute from a data step that does the logic for the dynamic parts, or works off a defined list kept in a dataset.

 

---------------------------------------------------------------------------------------------
Maxims of Maximally Efficient SAS Programmers
How to convert datasets to data steps
How to post code
Solution
‎06-06-2018 12:32 AM
Trusted Advisor
Posts: 1,837

Re: When to use % in if then Macro.

Your question: when to use %IF and what is the difference between %IF and IF:

 

IF is used in a data step to execute a stetement or group of stetements (using DO ...END) conditionally based on data.

 

%IF is used in a macro program. Macro program is used to generate sas code. 

Thus, %IF will select the code to be generated according to the condition, based on given arguments to the macro program.

It doesn't check data.

 

%IF is executed once at time of "compilation" of the macro program.

IF is executed in a data step for every input row.

☑ This topic is solved.

Need further help from the community? Please ask a new question.

Discussion stats
  • 6 replies
  • 168 views
  • 3 likes
  • 5 in conversation