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

Hi there,

 

I have several variables called V1 V2 ... Vn, how can I put this sequence in by statement to make the sample group by every variable in this sequence?

 

Just like this:

 

proc ttest data=test3;
by V1 V2 ... Vn;
var ret;
run;

 

The n is one of my macro variables, and sometimes I need to change the value of n.

1 ACCEPTED SOLUTION

Accepted Solutions
Tom
Super User Tom
Super User

First thing is figure out the SAS code you want.

If your variables are really named V1, V2, V3, V4 and V5 then the BY statement is simply:

by v1-v5 ;

Now if the want to use a macro variable to help generate that statement you could put 5 into the macro variable.

%let n=5;

Then use it to create the BY statement:

by v1-v&n;

View solution in original post

3 REPLIES 3
Tom
Super User Tom
Super User

First thing is figure out the SAS code you want.

If your variables are really named V1, V2, V3, V4 and V5 then the BY statement is simply:

by v1-v5 ;

Now if the want to use a macro variable to help generate that statement you could put 5 into the macro variable.

%let n=5;

Then use it to create the BY statement:

by v1-v&n;
canvaschen
Calcite | Level 5

Thanks, Tom.

It works perfectly!

PhilC
Rhodochrosite | Level 12

Tom always gets the solves.

 

If anyone else is interested and find this, this alternative way is available.  If all one wishes to use all variables in the data set that start with same prefix ( say v in this case) then 

/*no let statement */
proc ttest data=test3;
  by V: ; /*colon operator*/
  var ret;
run;

SAS Innovate 2025: Save the Date

 SAS Innovate 2025 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!

Save the date!

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.

SAS Training: Just a Click Away

 Ready to level-up your skills? Choose your own adventure.

Browse our catalog!

Discussion stats
  • 3 replies
  • 768 views
  • 1 like
  • 3 in conversation