Hi:
While the other examples are truly magnificent examples, here's something a bit simpler (I hope):
[pre]
** define a generic macro program for sashelp.class;
%macro getobs;
** get rid of periods in WANTVAL for filename;
%let x = %sysfunc(translate(&wantval,'_','.'));
%let outfile = Apr08_&wantvar.&x;
%let wantvar = %upcase(&wantvar);
title "Report for &wantvar = &wantval";
title2 "Result File is: &outfile..html";
%if &wantvar = AGE or &wantvar = HEIGHT or
&wantvar = WEIGHT %then %do;
%let whrcls = where &wantvar EQ &wantval;
%end;
%else %if &wantvar = NAME or &wantvar = SEX %then %do;
%let whrcls = where &wantvar EQ "&wantval";
%end;
%else %do;
%let whrcls = %str(where 1);
title 'You did not specify name, age, height, weight or sex';
title2 'for &wantvar. check your program and submit again';
title3 'Printing all OBS for SASHELP.CLASS';
%end;
options mprint symbolgen;
ods html file="c:\temp\&outfile..html" style=sasweb;
proc print data=sashelp.class;
&whrcls;
run;
ods html close;
title; footnote;
options nomprint nosymbolgen;
%mend getobs;
[/pre]
Basically, this is a definition for a macro program. I am using it to build code for me so that, when the macro program is invoked, the generated code (when executed) will select observations out of SASHELP.CLASS. The macro variables that I will provide are &WANTVAR (for which variable they want -- name, age, height, weight, sex) and &WANTVAL will be the VALUE that somebody is asking for. My condition is always an EQ condition in the WHERE clause. But for every time I run the Macro program (%getobs), I want a separate file name to be generated that looks like this: Apr08_xxxxxxxyyyyy.html where xxxxx is the value for &WANTVAR and yyyyy is the value for &WANTVAL. AND, in order to build the correct WHERE clause, I have to know whether they asked for AGE, HEIGHT and WEIGHT for &WANTVAR or whether they asked for NAME or SEX -- because the WHERE clause would look different for character variables versus numeric variables. AND, since height and weight could have decimal points in the number, I need the decimal in the &WANTVAL macro variable, but I do NOT want the . in my file name.
So, the %let for X is getting rid of the . out of &WANTVAL and the %let for OUTFILE is building the special file name that I want. And, in addition to that, the %let for WHRCLS is being set conditionally depending on what they asked for. So, from 2 pieces of information provided to my macro program, I have done these OTHER things, just by creating new macro variables:
1) generated a file name for my HTML file based on the values they sent me (including stripping out the . and replacing it with _)
2) generated the correct where clause for character vs numeric variables
Then when I invoke that macro program, I will get the desired results for the first 4 invocations and all the obs in SASHELP.CLASS as a result of the last invocation. I'm not going to show all the output below, but I will show all of the TITLES from each run:
[pre]
** now use the macro program multiple times;
** note in the log how the where statement and output file;
** name get changed appropriately depending on wantvar and wantval;
%let wantvar = age;
%let wantval = 13;
%getobs;
Title Results:
Report for AGE = 13
Result File is: Apr08_age13.html
*************************************
%let wantvar = height;
%let wantval = 62.5;
%getobs;
Title Results:
Report for HEIGHT = 62.5
Result File is: Apr08_height62_5.html
*************************************
%let wantvar = name;
%let wantval = Alfred;
%getobs;
Title Results:
Report for NAME = Alfred
Result File is: Apr08_nameAlfred.html
*************************************
%let wantvar = sex;
%let wantval = F;
%getobs;
Title Results:
Report for SEX = F
Result File is: Apr08_sexF.html
*************************************
%let wantvar = Region;
%let wantval = Asia;
%getobs;
Title Results:
You did not specify name, age, height, weight or sex
for &wantvar. check your program and submit again
Printing all OBS for SASHELP.CLASS
*************************************
[/pre]
This is just ONE example of how you can build new macro variables from existing macro variables. There are many, many more. Generally, you use this feature of the macro facility because you need something in your generated code that can be derived from an existing macro variable.
cynthia