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

** 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;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Shmuel
Garnet | Level 18

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.

View solution in original post

2 REPLIES 2
Shmuel
Garnet | Level 18

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.

weiwei2392
Fluorite | Level 6
Thank you so much 🙂 Now its making more sense...i didnt what i was writing 😛 Now i am able to understand the logic behind it !

sas-innovate-2024.png

Don't miss out on SAS Innovate - Register now for the FREE Livestream!

Can't make it to Vegas? No problem! Watch our general sessions LIVE or on-demand starting April 17th. Hear from SAS execs, best-selling author Adam Grant, Hot Ones host Sean Evans, top tech journalist Kara Swisher, AI expert Cassie Kozyrkov, and the mind-blowing dance crew iLuminate! Plus, get access to over 20 breakout sessions.

 

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
  • 1134 views
  • 2 likes
  • 2 in conversation