BookmarkSubscribeRSS Feed
Onizuka
Pyrite | Level 9

Hello everyone,

 

I'm working on SAS Macros and I saw that sometimes, we have to put

%THEN %DO.....

but sometimes we don't have to.

 

For example :

DATA mdv.&typequest._finale (where = (test <> 0)) ;
	SET mdv.&typequest._finale;
	ARRAY tquestcourt[&nbvar.] &list.;
	test=0;
	%LOCAL i ;
	%DO i=1 %TO &nbvar.;
		IF tquestcourt[&i] ne . THEN do ; 
		test=test+1; 
		end ;
	%END;
RUN;

On this example, there is a %do i ... %to but just below there isn't a %if %then %do.

 

I didn't find specific rules on the web so I would like to ask the question here.

 

Thank you very much 🙂

 

Onizuka

29 REPLIES 29
PaigeMiller
Diamond | Level 26

%DO and %IF are independent, one does not have to follow the other, you use them as needed.

 

In the code you show, there is no need for a macro %DO here, and it won't work as you have coded it anyway. You can use a data step DO to loop over the array.

 

IMPORTANT CONCEPT: get code to work properly first, one one iteration through a small data set, without macros; and then and only then add in macros and macro variables. Since you can get this code to work without %DO by using the data step DO, no need for macros.

--
Paige Miller
Onizuka
Pyrite | Level 9

Hello Miller, thank you for you response.

 

Before start doing "macro programm" i'm always doing without macro (like you said).

 

I'm a little surprise by what you say because the programm works well !

 

This one allows to keep the observations for which at least one variable in the list is not null.

 

Amir
PROC Star

Would you please provide an example of working code where a %do is followed by a %then.

 

Thanks,

Amir.

Quentin
Super User

You typically wouldn't have %do followed by %then.  You will often see %then followed by %do, e.g.

  

%if <condition> %then %do;
  *something;
%end;

 

As @PaigeMiller wrote, the %DO statement is a separate statement than the %IF/%THEN/%ELSE statement.

 

There are plenty of examples in the documentation, see e.g. 

 

https://documentation.sas.com/?docsetId=mcrolref&docsetTarget=p1xhuzfc68jkhcn1xb0c4d5atq5e.htm&docse...

 

The Boston Area SAS Users Group is hosting free webinars!
Next up: Joe Madden & Joseph Henry present Putting Power into the Hands of the Programmer with SAS Viya Workbench on Wednesday Nov 6.
Register now at https://www.basug.org/events.
Amir
PROC Star

@Quentin not sure if you meant to respond to me or the OP(?)

 

If it was me then agreed, which is why I asked the OP for a working example, as that was their opening assertion, so either they find an example, which someone on this forum should be able to explain to the OP, or they don't, in which case part of the premise of the question does not hold true and so the OP realises their assumption. Win-Win!

 

Kind regards,

Amir.

PaigeMiller
Diamond | Level 26

@Amir wrote:

Would you please provide an example of working code where a %do is followed by a %then.

 


I don't think code can have %DO followed by %THEN.

--
Paige Miller
Amir
PROC Star

@PaigeMiller agreed. Please see my initial response to @Quentin.

 

Kind regards,

Amir.

Onizuka
Pyrite | Level 9

I agree, the %DO comes always after the %THEN

 

EDIT : my bad, i write it bad on my exemple, i wanted to say %THEN ... %DO and not say %DO ... %THEN

Onizuka
Pyrite | Level 9

I don't have an example of that, the example i have is which one I post on the topic :

	%DO i=1 %TO &nbvar.;
		IF tquestcourt[&i] ne . THEN do ; 
		test=test+1; 
		end ;
	%END;

You can see the %DO ... %TO and just below an IF (without percent before) ... THEN (without percent before) do (without percent before) so I was wondering why sometimes we put the %(IF, THEN, DO) and why sometimes not. Just after the %DO i = 1 %to &nbvar. ; there is a IF THEN DO without percent but as I am in macro language i found it weird.

 

 

EDIT : This example is from a "long" macro programm

PaigeMiller
Diamond | Level 26

I was wondering why sometimes we put the %(IF, THEN, DO) and why sometimes not

 

Because sometimes the logic of the specific program requires it, and other times the logic does not require it.

--
Paige Miller
Onizuka
Pyrite | Level 9

Yes, I guess it depends.. buy why in my example (it's just an example of a code I have and run well) if i replace

 

IF tquestcourt[&i] ne . THEN do ;

by

 

%IF tquestcourt[&i] ne . %THEN %DO ;

it doesn't work while i'm in a macro programm. I don't understand very well the logic you are talking about.

PaigeMiller
Diamond | Level 26

Okay, @Onizuka we are going around in circles here. At one point, you seem to be asking questions about macro code, and then at other times you seem to be asking questions about the logic. Which is it?

 

If your code doesn't work, then show us the SAS log, using options mprint; before you run the program. Look at the log yourself and see if you can figure out what is the problem. If not, then post the log here by clicking on the {i} icon and pasting it into the window that appears. Do not -- I repeat DO NOT -- post the log without clicking on the {i} icon and pasting it into the window that appears.

--
Paige Miller
Amir
PROC Star

Typically, macro code is executed before any data step code is executed.

 

1) The %do - %end loop will cause the data step code to be repeated &nbvar times.

2) Once the macro code has completed then the data step code will start, which include &nbvar instances of the if-then-do-end code.

3) Using options mprint; before you run the code should show more information in the log.

 

Amir.

Onizuka
Pyrite | Level 9

Thank you Amir,

 

I believe that I have understood. When using a variable macro the % must be used and if not removed.

 

I think I could have written this :

 

/* if i take off the macro variable */
%macro test ;
..
Data blabla;
set blabla1 ;
..      
	DO i=1 TO 10;
		IF tquestcourt[&i] ne . THEN do ; 
		test=test+1; 
		end ;
	END;
..
run ;

%mend test ;

/*if i add the macro variable */ 

%macro test ;
..
Data blabla ;
set blabla1 ;
..

	%DO i=1 %TO &nbvar.; 
		IF tquestcourt[&i] ne . THEN do ; 
		test=test+1; 
		end ; 
	%END;
..
run ;

%mend test ;

 

Am I right ? haha

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 29 replies
  • 1622 views
  • 11 likes
  • 6 in conversation