Question about %Let

Accepted Solution Solved
Reply
Occasional Contributor
Posts: 5
Accepted Solution

Question about %Let

** 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 Smiley Frustrated   and for (B) (c) (d) i am completely lost Smiley Sad

been stock on this questions for days Smiley Sad

 

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

 

Attachment

Accepted Solutions
Solution
‎01-07-2017 03:19 AM
Trusted Advisor
Posts: 1,479

Re: Question about %Let

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


All Replies
Solution
‎01-07-2017 03:19 AM
Trusted Advisor
Posts: 1,479

Re: Question about %Let

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.

Occasional Contributor
Posts: 5

Re: Question about %Let

Thank you so much Smiley Happy Now its making more sense...i didnt what i was writing Smiley Tongue Now i am able to understand the logic behind it !
☑ This topic is solved.

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

Discussion stats
  • 2 replies
  • 185 views
  • 0 likes
  • 2 in conversation