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

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

1 ACCEPTED SOLUTION

Accepted Solutions
ballardw
Super User

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

2 REPLIES 2
ballardw
Super User

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.

Cynthia_sas
SAS Super FREQ

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

cynthia

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
  • 2 replies
  • 1347 views
  • 0 likes
  • 3 in conversation