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

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

1 ACCEPTED SOLUTION

Accepted Solutions
djbateman
Lapis Lazuli | Level 10

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

2 REPLIES 2
djbateman
Lapis Lazuli | Level 10

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.

AD
Calcite | Level 5 AD
Calcite | Level 5

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

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