DATA Step, Macro, Functions and more

macro question

Accepted Solution Solved
Reply
Super Contributor
Super Contributor
Posts: 440
Accepted Solution

macro question

what is the advantage of %if... %then over if .. then..?

I mean what is it that " %" does over not having it?

In one of the problems here someone mentioned if we are to compare values within a  dataset we should not use the "%".

Can someone please elaborate this?

Thanks


Accepted Solutions
Solution
‎11-16-2013 08:46 PM
Respected Advisor
Posts: 3,908

Re: macro question

The link to the macro doc provided will give you all the information you need. Start reading "Understanding and Using the Macro Facility"

I also found this introduction from : http://support.sas.com/resources/papers/proceedings13/120-2013.pdf

On a general and oversimplified level:

- Everything "macro" gets executed (and the '%' and '&' are the tokens telling SAS that a word in the SAS code is macro language).

- After macro execution the remaining "text" is then interpreted by the SAS compiler.

So macro language is kind-of pre-processing to "normal" SAS language allowing you to dynamically generate SAS code.

Eg. below macro:

/* compile macro */

options mprint;

%macro test(parameter);

  %if &parameter=1 %then

    %do;

      data test;

        set sashelp.class;

      run;

    %end;

  %else

    %do;

      data test;

        set sashelp.cars;

      run;

    %end;

%mend;

/* call macro - so here the macro gets executed */

%test(1)

Using "options mprint" what you then can see in the log is the "result" of the macro execution, so the bit which will be executed by the "normal" SAS language.

39         %test(1)

MPRINT(TEST):   data test;

MPRINT(TEST):   set sashelp.class;

MPRINT(TEST):   run;

View solution in original post


All Replies
Respected Advisor
Posts: 3,908

Re: macro question

Super Contributor
Super Contributor
Posts: 440

Re: macro question

good articles Patrick,thanks !

Yes i do know that & is for a  macro variable and % for  a macro but within a macro code  we can have %if ...% then and  we can also have if... then...,am i correct?

So i was wondering what the difference was

PROC Star
Posts: 7,366

Re: macro question

To answer your own question you have to read the links that Patrick provided, as well as anything else you can find regarding the SAS macro language.

A SAS macro's code can contain a data step, but a data step cannot contain SAS macro code, only compiled macros.  And, within a SAS macro, a %if statement doesn't have to be within a data step, but can be.  'If' statements, themselves, can only occur within a data step.  But, to understand their utility, you first have to learn what they are, how they can be used, and their functionality within a SAS program.

Super Contributor
Super Contributor
Posts: 440

Re: macro question

Thanks Arthur. I did actually read several articles about macros and could not find the answer to my own question,maybe i did not read the right ones but thanks for the explanation.I will definitely go over these 2 articles in details

PROC Star
Posts: 7,366

Re: macro question

Solution
‎11-16-2013 08:46 PM
Respected Advisor
Posts: 3,908

Re: macro question

The link to the macro doc provided will give you all the information you need. Start reading "Understanding and Using the Macro Facility"

I also found this introduction from : http://support.sas.com/resources/papers/proceedings13/120-2013.pdf

On a general and oversimplified level:

- Everything "macro" gets executed (and the '%' and '&' are the tokens telling SAS that a word in the SAS code is macro language).

- After macro execution the remaining "text" is then interpreted by the SAS compiler.

So macro language is kind-of pre-processing to "normal" SAS language allowing you to dynamically generate SAS code.

Eg. below macro:

/* compile macro */

options mprint;

%macro test(parameter);

  %if &parameter=1 %then

    %do;

      data test;

        set sashelp.class;

      run;

    %end;

  %else

    %do;

      data test;

        set sashelp.cars;

      run;

    %end;

%mend;

/* call macro - so here the macro gets executed */

%test(1)

Using "options mprint" what you then can see in the log is the "result" of the macro execution, so the bit which will be executed by the "normal" SAS language.

39         %test(1)

MPRINT(TEST):   data test;

MPRINT(TEST):   set sashelp.class;

MPRINT(TEST):   run;

SAS Super FREQ
Posts: 8,745

Re: macro question

Hi:

I highly recommend that you read the doc and other links that have been provided. Here's a simple explanation. The "regular" IF statement "belongs" to the SAS DATA step language syntax. You can ONLY use a "regular" IF statement in a DATA step program:

data new;

set sashelp.class;

if sex = 'F' then wt_in_5 = weight*1.10;

else if sex = 'M' then wt_in_5 = weight*1.20;

run;

  The "regular" IF statement has NOTHING to do with the SAS Macro facility. A "regular" IF statement is used to alter the way a DATA step program executes...by specifying statements that work conditionally within a SAS DATA step program. As shown above, the new variable WT_IN_5 will be calculated one way for females and a different way for males.

  The Macro Facility %IF "belongs" in a SAS Macro program definition. A %IF statement is used within a macro program to conditionally generate code, as illustrated in Patrick's example macro program. You could have a macro program definition that generated complete steps, partial steps, complete statements or partial statements.

  As Art pointed out, a SAS Macro program could, but does not have to, contain DATA step code. A SAS Macro program and a SAS Macro %IF will reference macro variables, such as Patrick showed: %if &parameter=1 %then %do -- however, a DATA step program will use a "regular" IF to reference DATA step variables.


  First, you need to understand how a DATA step program works; and the utility of the "regular" IF in a DATA step program. Then, you need to understand the utility of the SAS Macro Facility to generate code for you....any kind of code. Within the SAS Macro Facility and inside a SAS Macro program definition, the %IF statement is a Macro logic statement. It has a place in a SAS Macro program. It has no place in a DATA step program. A Macro %IF can control what type of DATA step code gets generated by the Macro processor, but, as Patrick showed, your generated code does NOT have to contain any IF statements at all. On the other hand, a DATA step IF has no control over SAS macro statements.

cynthia

Super Contributor
Super Contributor
Posts: 440

Re: macro question

Cynthia ,Art and Patrick thanks for your thorough explanations. I'd probably find this info in the articles but no understand as clear as you have explained here.Million Thanks

Respected Advisor
Posts: 3,908

Re: macro question

Hi Tal

Can you please mark the answers as helpful/correct so that your question appears as answered.

Super Contributor
Super Contributor
Posts: 440

Re: macro question

hey Patrick,

Thanks again for the article by Cynthia you sent me.Just went over it and  this is  actually what i was looking for.Straight to the point,best ever introduction for Macros I've read.

Thanks Cynthia!

PROC Star
Posts: 7,366

Re: macro question

Tal,

If you REALLY want to learn the ins and outs about the SAS macro language do the following search and then read every response that Ian Whitlock made on SAS-L over the years:

Search results -- SAS-L</title><style type="text/css"><!--BODY { font-family: "Comic Sans MS",arial,...

But, beware!  There is an awfully lot of stuff there to read.  However, there is also an awfully lot of important concepts that you won't find in any of the documentation.

Super Contributor
Super Contributor
Posts: 440

Re: macro question

nice!

looks like another forum with lot of problems and solutions.

Thanks Art!

PROC Star
Posts: 7,366

Re: macro question

I wasn't advertising the forum but, rather, one person's responses which, for the most part, will be related to the SAS macro language.  However, since you mention it, that forum has been around since 1986

🔒 This topic is solved and locked.

Need further help from the community? Please ask a new question.

Discussion stats
  • 13 replies
  • 371 views
  • 12 likes
  • 4 in conversation