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
%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.
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.
Would you please provide an example of working code where a %do is followed by a %then.
Thanks,
Amir.
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.
@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.
@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.
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
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
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.
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.
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.
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.
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.