DATA Step, Macro, Functions and more

Using macro variables inside an array?

Accepted Solution Solved
Reply
Contributor AD
Contributor
Posts: 31
Accepted Solution

Using macro variables inside an array?

I've got a dataset (Data1) with a bunch of variables (Var1, Var2, ..., VarN), and I want to be able to specify a number of different "sets" of variables on which I will be performing the same analysis.  I'd like to do this inside of a macro, but I need a little guidance. 

I want the user to be able to specify the number of sets and the variables associated with each set (see below for example):

%LET NumSets = 3;

%LET Set1 = Var1 Var2 Var3;

%LET Set2 = Var8 Var9 Var10;

%LET Set3 = Var12 Var13 Var14 Var15;

I also want to be able to perform a simple analysis (e.g., PROC CORR) for each "set".  The number of sets might change from user to user, however.  I've got the shell of the macro below, but something needs to be fixed.

%macro Corr;

  do i = 1 to &NumSets;

   proc corr  data=Data1;

     var Set&i;

   run;

  end;

%mend;

You can see that in the bolded line above, Proc Corr would be searching for a variable named "Set1" during the first iteration of the do loop.  But I don't want it searching for a variable named set1, I want Proc Corr to be analyzing the set of variables that were defined above in the Set1 macro variable.  I'm not sure how to get this done.  I don't know much about arrays, but maybe that's the right approach?  Or perhaps there is a simple shortcut or modification that I could make to that var statement?

Thanks,

Andy


Accepted Solutions
Solution
‎11-27-2012 02:40 PM
Regular Contributor
Posts: 220

Re: Using macro variables inside an array?

Try this:

%macro Corr;

      %do i = 1 %to &NumSets;

            proc corr data=Data1;

                  var &&Set&i;

            run;

      %end;

%mend;

You will need the double ampersands (&&) in front of your Set&i macro.  Otherwise, SAS sees the variable as Set1, but there is no variable Set1.  It is a macro variable called &Set1.

View solution in original post


All Replies
Solution
‎11-27-2012 02:40 PM
Regular Contributor
Posts: 220

Re: Using macro variables inside an array?

Try this:

%macro Corr;

      %do i = 1 %to &NumSets;

            proc corr data=Data1;

                  var &&Set&i;

            run;

      %end;

%mend;

You will need the double ampersands (&&) in front of your Set&i macro.  Otherwise, SAS sees the variable as Set1, but there is no variable Set1.  It is a macro variable called &Set1.

Contributor AD
Contributor
Posts: 31

Re: Using macro variables inside an array?

Thanks... perfect.  I love learning new tricks like this, and there are so many to learn.

☑ This topic is SOLVED.

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

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