BookmarkSubscribeRSS Feed
HeatherNewton
Quartz | Level 8

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;
7 REPLIES 7
MarkusWeick
Barite | Level 11

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

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
HeatherNewton
Quartz | Level 8

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?

Kurt_Bremser
Super User

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

MarkusWeick
Barite | Level 11

Hi @HeatherNewton,

 

p.8. of the docu:

Bruehl_0-1647507344522.png

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

Please keep the community friendly.
Like posts you agree with or like. Mark helpful answers as “accepted solutions”. Generally have a look at https://communities.sas.com/t5/Getting-Started/tkb-p/community_articles
HeatherNewton
Quartz | Level 8

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

Kurt_Bremser
Super User

"&" 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.

Ready to join fellow brilliant minds for the SAS Hackathon?

Build your skills. Make connections. Enjoy creative freedom. Maybe change the world. Registration is now open through August 30th. Visit the SAS Hackathon homepage.

Register today!
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
  • 7 replies
  • 846 views
  • 5 likes
  • 3 in conversation