** Original code without macro variables;
PROC FREQ DATA = sasdata.cats;
TABLES Origin;
TITLE 'Cat Breeds by = Origin’;
RUN;
PROC PRINT DATA = sasdata.cats;
WHERE Origin = 'Thailand';
TITLE 'Cat Breeds with Origin = Thailand';
RUN;
a.Use %LET statements to create two macro variables: one to replace the variable name Origin, and another to replace the
data value ‘Thailand’.Use an option that will enable you to see the standard SAS statements generated by the macro processor.
b. Use the macro variables to produce counts for the variable Derivation, and to list data for
breeds that were derived by mutation.
c. Convert the code to a macro. Pass the values for the two macro variables into the macro as
parameters. Call the macro to produce counts for the variable Hair, and to list data for breeds
with long hair.
d. Add programming to your macro from part c) that will save your output in a PDF file. Name
this file CatRpt, and append the filename with a suffix that is the name of the variable used in
the TABLES statement.
------------------------------------------------------------------------------------------
The following is my code for (a), but i can't print it successfully :S and for (B) (c) (d) i am completely lost 😞
been stock on this questions for days 😞
%let Origin=from;
%let Thailand=Island;
%macro coco;
PROC FREQ DATA = sec.cats;
TABLES Origin;
TITLE 'Cat Breeds by = Origin';
RUN;
PROC PRINT DATA = se.cats;
WHERE Origin = 'Thailand';
TITLE 'Cat Breeds with Origin = Thailand';
RUN;
%mend coco;
A)
%let varname = Origin;
%let country = Thailand;
PROC FREQ DATA = sasdata.cats;
TABLES &varname;
TITLE "Cat Breeds by = &country";
RUN;
PROC PRINT DATA = sasdata.cats;
WHERE &varname = "&country";
TITLE "Cat Breeds with &varname = &country ";
RUN;
pay attention: &origin will insert your assignment by %let to this macro variable
use double quotes in order that macro variable will be resolved to its content
when it is used as litteral
C) same as (A) can be done without %LET but by macro program with arguments:
%macro coco(varname,country);
PROC FREQ DATA = sasdata.cats;
TABLES &varname;
TITLE 'Cat Breeds by = Origin’;
RUN;
PROC PRINT DATA = sasdata.cats;
WHERE &varname = "&country";
TITLE "Cat Breeds with &varname = &country ";
RUN;
%mend coco;
%coco(origin,Thailand);
D) In order to get a PDF report use ODS PDF file="<path and file name with suffix>";
in front your code and add ODS CLOSE; at end of the code.
Try it and check results.
A)
%let varname = Origin;
%let country = Thailand;
PROC FREQ DATA = sasdata.cats;
TABLES &varname;
TITLE "Cat Breeds by = &country";
RUN;
PROC PRINT DATA = sasdata.cats;
WHERE &varname = "&country";
TITLE "Cat Breeds with &varname = &country ";
RUN;
pay attention: &origin will insert your assignment by %let to this macro variable
use double quotes in order that macro variable will be resolved to its content
when it is used as litteral
C) same as (A) can be done without %LET but by macro program with arguments:
%macro coco(varname,country);
PROC FREQ DATA = sasdata.cats;
TABLES &varname;
TITLE 'Cat Breeds by = Origin’;
RUN;
PROC PRINT DATA = sasdata.cats;
WHERE &varname = "&country";
TITLE "Cat Breeds with &varname = &country ";
RUN;
%mend coco;
%coco(origin,Thailand);
D) In order to get a PDF report use ODS PDF file="<path and file name with suffix>";
in front your code and add ODS CLOSE; at end of the code.
Try it and check results.
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 16. 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.