DATA Step, Macro, Functions and more

The difference between Macro Loop and a normal loop

Accepted Solution Solved
Reply
New Contributor
Posts: 2
Accepted Solution

The difference between Macro Loop and a normal loop

Dear all,

I am working on macro facility and when I writes the codes,

sometimes it is difficult for me to determine when to use a macro loop  and when not to.

Below is an example of my code:

%macro test1b(type);

     data advance.test1b;

     set advance.test1a;

          %if &type=Orio %then %do;

                 where test2='abc';

          %end;

    run;

%mend test1b;

The function of the code is to first check whether user inputs the parameter 'Orio', and if it is true then executes the 'where' clause.

I found that whether or not I add a % in the loop, the outputs are very similar except a variable named "Orio' is created when % is not used.

I do not quite understand the difference in the use of %, and may someone please explain with respect to the above example?

Thank you so much!

Casey


Accepted Solutions
Solution
‎05-26-2015 12:17 PM
Super User
Posts: 11,343

Re: The difference between Macro Loop and a normal loop

Without seeing input or expected output details may be rough. but the %do/%end controls the creation of code by the SAS Macro processor and Do /end is a data step code construct.

Your example macro if invoked as

%test1b (Orio);

Will generate the following data step code:

     data advance.test1b;

     set advance.test1a;

                 where test2='abc';

    run;

%test1b (Fred); would generate

     data advance.test1b;

     set advance.test1a;

    run;

NOTE that the comparison is going to be case sensitive: ORIO is not equal to Orio or oRio.

You might want to add:

     %let &type=%upcase(&type); before the Data statement and use

     %if &type=ORIO %then %do;

to reduce case issues.

if you use

if &type=Orio %then %do;

                 where test2='abc';

end;

then the data step generated looks like:

     data advance.test1b;

     set advance.test1a;

     if Orio=Orio then do;

                 where test2='abc';

     end;

    run;

Which assumes ORIO is a variable and SAS will create any variable references with missing values.

HINT: Use the SAS macro options to look at what code is created by your macro:

options mprint symbolgen mlogic; will show the lines of code generated in the LOG, the resolution of macro variables as they are used and the results of logic involving macro comparisons.

Use options nomprint nosymbolgen nomlogic; to turn the resolution off. You may use any or all of these as need. I generally start with MPRINT and add symbolgen if some variable looks like it isn't resolving correctly.

View solution in original post


All Replies
Solution
‎05-26-2015 12:17 PM
Super User
Posts: 11,343

Re: The difference between Macro Loop and a normal loop

Without seeing input or expected output details may be rough. but the %do/%end controls the creation of code by the SAS Macro processor and Do /end is a data step code construct.

Your example macro if invoked as

%test1b (Orio);

Will generate the following data step code:

     data advance.test1b;

     set advance.test1a;

                 where test2='abc';

    run;

%test1b (Fred); would generate

     data advance.test1b;

     set advance.test1a;

    run;

NOTE that the comparison is going to be case sensitive: ORIO is not equal to Orio or oRio.

You might want to add:

     %let &type=%upcase(&type); before the Data statement and use

     %if &type=ORIO %then %do;

to reduce case issues.

if you use

if &type=Orio %then %do;

                 where test2='abc';

end;

then the data step generated looks like:

     data advance.test1b;

     set advance.test1a;

     if Orio=Orio then do;

                 where test2='abc';

     end;

    run;

Which assumes ORIO is a variable and SAS will create any variable references with missing values.

HINT: Use the SAS macro options to look at what code is created by your macro:

options mprint symbolgen mlogic; will show the lines of code generated in the LOG, the resolution of macro variables as they are used and the results of logic involving macro comparisons.

Use options nomprint nosymbolgen nomlogic; to turn the resolution off. You may use any or all of these as need. I generally start with MPRINT and add symbolgen if some variable looks like it isn't resolving correctly.

SAS Super FREQ
Posts: 8,866

Re: The difference between Macro Loop and a normal loop

I put a brief discussion of the difference in this paper: http://support.sas.com/resources/papers/proceedings13/120-2013.pdf

cynthia

🔒 This topic is solved and locked.

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

Discussion stats
  • 2 replies
  • 297 views
  • 0 likes
  • 3 in conversation