BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Chaichai
Fluorite | Level 6
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;
1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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

6 REPLIES 6
Reeza
Super User

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

 

 

Reeza
Super User


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;

 

Chaichai
Fluorite | Level 6

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

 

Kurt_Bremser
Super User

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.

 

Shmuel
Garnet | Level 18

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.

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
  • 6 replies
  • 661 views
  • 3 likes
  • 5 in conversation