Choose Language Hide Translation Bar
Highlighted
seburke89
Level III

Summarize Function with multiple by columns

All,

 

I'm trying to write a script that gets a count of observations, potentially by 1 or more columns (as determined by input from the user). I can get the script to work for one column, but not for two or more. How do I adjust the code in the by step of the summarize function to handle 1+ columns?

 

Below is a sample of script:

 

 

varName = VarList <<Get selected();
numCols = nitems(varName);
n = nrow(dt);

if(numCols == 0,
	//do one thing
,numCols>0,
	summarize(dt, cat = by(Column(dt,varName)),c=count);
	//do other things
);
	

 

 

 

Thanks!

Sarah

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Summarize Function with multiple by columns

There's not quite enough information in your post to be sure I know what the problem is. It would be helpful to know what VarList is, and what the result of the <<Get Selected() message is. However, I suspect that VarList is a list of columns, liket this:

 

dt=open("$SAMPLE_DATA\Big Class.jmp");
varname={:age, :sex};

If so, then you need to put use Eval() around varname in the Summarize() function, like this:

 

dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

varname = {:age, :sex};

//varName = VarList <<Get selected();
numCols = N Items( varName );
n = N Row( dt );

If(
	numCols == 0, 
	//do one thing
,
	numCols > 0,
		Summarize( dt, cat = by( Eval( varname ) ), c = count );
	//do other things
);
-Jeff

View solution in original post

2 REPLIES 2
Highlighted
Jeff_Perkinson
Community Manager Community Manager

Re: Summarize Function with multiple by columns

There's not quite enough information in your post to be sure I know what the problem is. It would be helpful to know what VarList is, and what the result of the <<Get Selected() message is. However, I suspect that VarList is a list of columns, liket this:

 

dt=open("$SAMPLE_DATA\Big Class.jmp");
varname={:age, :sex};

If so, then you need to put use Eval() around varname in the Summarize() function, like this:

 

dt = Open( "$SAMPLE_DATA\Big Class.jmp" );

varname = {:age, :sex};

//varName = VarList <<Get selected();
numCols = N Items( varName );
n = N Row( dt );

If(
	numCols == 0, 
	//do one thing
,
	numCols > 0,
		Summarize( dt, cat = by( Eval( varname ) ), c = count );
	//do other things
);
-Jeff

View solution in original post

Highlighted
seburke89
Level III

Re: Summarize Function with multiple by columns

Sorry for the confusion on the varname variable. You've got it right - it's a list of column names that depend on input from the user from a GUI (that's the varList - a column list box). And you've solved my problem. Love it when it's a simple fix.

 

For my future knowledge then, anytime I want to reference data table column names in a function in JSL, things should work well with the eval() command around it? I always seem to have problems with that - especially when coding graphs.

 

Thanks much!

Sarah

Article Labels

    There are no labels assigned to this post.