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
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.
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.
I put a brief discussion of the difference in this paper: http://support.sas.com/resources/papers/proceedings13/120-2013.pdf
cynthia
Are you ready for the spotlight? We're accepting content ideas for SAS Innovate 2025 to be held May 6-9 in Orlando, FL. The call is open until September 25. Read more here about why you should contribute and what is in it for you!
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.