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.
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;
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;
Thanks, Tom.
It works perfectly!
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 is scheduled for May 6-9 in Orlando, FL. Sign up to be first to learn about the agenda and registration!
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.
Ready to level-up your skills? Choose your own adventure.