Monday, May 02, 2005

Some SAS/MACRO techniques

SAS/Macro is a very powerful tool in programming. I would like to summarize some techniques often used in SAS/MACRO:
1. orderring heterogenously named variables:
1.first store the variable names in a data set called nameset;
2.second use following macro:
%macro getname;
data _null_;
set lib.nameset;
call symput('varname'left(_n_), name);
run;
%mend;

2.resolving macro name and underlying value:
suppose one variable is named Division and has value 'ARMY6'. We put it in a ordered sequential Macro variable names name1-name5, where Division is &name3;
using &&name&i to resolve the name itself: Division;
using &&&name&i to resolve the underlying value: ARMY6;

3.using macro to generate statement, note the double semicolons;
suppose we want to generate a 'keep' statement in a DATA STEP within a macro. We want to keep variables with heterogeneous names, but each is associated with a corresponding ordered macro variable name&i. We can generate the keep statement like this:
keep
%do i=1 %to 5;
&&name&i
%end;;
at the end of macro variable, there is no semicolon while at the end of %do loop, there are double semicolons. The first semicolon is for %do loop while the second one is for 'keep' statement.

**********END***********

0 Comments:

Post a Comment

<< Home