Hi I am not familiar with the kind of macro where there is a % before a lot of words, what do they mean?
%macro gen_ind(num); %do i=1 %to &num %by 1; proc sql; create table rate_MAC as select a.*, . . . Quit; %end;
Hi @HeatherNewton,
I recommend Chapter 1 "Introduction to the Macro Facility" in SAS 9.1 Macro Language.
I Think it's to difficult to explain yout example in two sentences.
Best, Markus
before I saw example where % are only added to %macro and %mend...
why suddenly they are added to do if then this kind of word?
@HeatherNewton wrote:
before I saw example where % are only added to %macro and %mend...
why suddenly they are added to do if then this kind of word?
Because these are macro statements. Please read the documentation.
In the link I gave you, go to Understanding and Using the Macro Facility. This will help you understand what the macro facility does.
Hi @HeatherNewton,
p.8. of the docu:
Conditionally creating SAS code gets to the point! It's an advanced concept. For me it helped to take the course SAS Training in the United States -- SAS® Macro Language 1: Essentials
am I right to say if these if then else loops in within a macro program, it needs % added to if then else?
if the if then else condition is not inside a macro program, then no % is required in from of if then else...
"&" and "%" are macro triggers which invoke the macro processor, or tell the macro processor that the following is (&) a macro variable/parameter reference , or (%) a macro statement or macro/macro function call. Everything else is just text to the macro processor.
You must first understand what the macro processor does. It is a code generator that allows you to create dynamic code. For easier understanding, its elements look a lot like "standard" SAS elements, but they are (in fact) completely different things.
IF and %IF are completely different things, although they have similarities, but THEY ARE DIFFERENT.
Same for (e.g.) SUBSTR vs. %SUBSTR.
Imagine you want to run a code for datasets that span back before Austria changed to the Euro, but, in case an "old" dataset is encountered, add a conversion to your code:
%if &year. lt 2002
%then %do;
amount = amount / 13.7604;
%end;
The first line compares the macro variable year to a value (mind - NOT a data step variable!!). If that comparison results in TRUE, the following %DO-%END block is executed by the macro processor, so it creates the code
amount = amount / 13.7604;
and feeds it back to the SAS data step compiler. In case you run the analysis for a later year, the statement is not created, and the data step compiler never sees it.
if year lt 2002
then do;
amount = amount / 13.7604;
end;
This code does not invoke the macro processor, is "seen" by the data step compiler, and uses the data step variable year to make the comparison; the IF is always executed during data step runtime.
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.