BookmarkSubscribeRSS Feed
🔒 This topic is solved and locked. Need further help from the community? Please sign in and ask a new question.
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

1 ACCEPTED SOLUTION

Accepted Solutions
Patrick
Opal | Level 21

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

13 REPLIES 13
Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

art297
Opal | Level 21

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

Patrick
Opal | Level 21

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;

Cynthia_sas
SAS Super FREQ

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

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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

Patrick
Opal | Level 21

Hi Tal

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

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

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!

art297
Opal | Level 21

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.

Tal
Pyrite | Level 9 Tal
Pyrite | Level 9

nice!

looks like another forum with lot of problems and solutions.

Thanks Art!

art297
Opal | Level 21

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

sas-innovate-2024.png

Join us for SAS Innovate April 16-19 at the Aria in Las Vegas. Bring the team and save big with our group pricing for a limited time only.

Pre-conference courses and tutorials are filling up fast and are always a sellout. Register today to reserve your seat.

 

Register now!

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
  • 13 replies
  • 1180 views
  • 12 likes
  • 4 in conversation