Choose Language Hide Translation Bar
JSL tip: finding unique values in a list

One of the great things about attending the JMP Discovery Summit as a developer is hearing questions and suggestions from people with so many different applications of JMP. One JMP Scripting Language (JSL) question came up three times, so it's worth sharing. The question is, "How do I remove duplicate items from a list?"

That is, given the list {"like", "dislike", "like", "neutral", "dislike", "like"} the result should be {"dislike", "like", "neutral"} (order not important).

There are probably lots of reasonable ways to do that in JSL (other ideas are welcome), but the simplest I could think of is to use an associative array. An associative array is a collection of key-value pairs where the keys are unique. If you add more than one key-value pair with the same key, only one of them is kept (the last one). If you don't care about the values, then the associative array is effectively a set of unique keys.

In JSL, you can create an associative array in several ways, but the only one of interest here is to create it from a list. The resulting associative array will treat the list values as keys and only store the unique keys. Then we can ask the associative array for a list of its keys.

answers = {"like", "dislike", "like", "neutral", "dislike", "like"};
answers set = Associative Array( answers );
unique answers = answers set << Get Keys;

Result: {"dislike", "like", "neutral"}

If you haven't discovered associative arrays in JMP, explore the docs and the examples in the JSL Scripting Index.

Now, it's your turn: What other ways can you get unique values from a list? I can think of one using the Summarize() function...

Article Labels

    There are no labels assigned to this post.


Michael Crotty wrote:

Another built-in JSL function that can help with this problem is the Design() function:

answers = {"like", "dislike", "like", "neutral", "dislike", "like"};

design(answers, < < Levels)[2]


Guillaume wrote:

Another way is to use loop statement and Loc() function to find unique values

answers = {"like", "dislike", "like", "neutral", "dislike", "like"};

unique_answers = List();

For (i=1,i<=Nitems(answers),i++,

If (Nrows(Loc(unique_answers,answers[i])) == 0,

InsertInto (unique_answers,answers[i])




Pmroz wrote:

Finding unique values in a list came up

in the JMP discussion forum a while back.  Here's the function

that I use, which is based on the technique Xan showed.:


Function Name: get_unique_values

Description: Find the unique values in a list. 

Uses an associative array


in_list     List to get unique values for


Get_unique_values = Function( {in_list}, {Default Local},

   tmp = [=> 0];

   Insert Into( tmp, in_list );

   tmp << get keys;



Leighton Spadone wrote:

//JSL Matrix functions used to reduce a List to Unique Values;

//A List with duplicate values;

answers = {"like", "dislike", "like", "neutral", "dislike", "like"};

//Colapse a design matrix to single row with 1 for 1st occurrence and 0 for duplicates;

DesignMatrix =VMax(design(answers, answers));

//Result: DesignMatrix = [1 1 0 1 0 0];

//Subset List of answers to unique values;


//Result: UniqueAnsers = {"dislike", "like", "neutral"};


Eric Wittry wrote:

If I have a key=>value pair, and I want to sort the values and return the keys in that order (*based on sorted value), how would I do that in JMP. Thanks Eric


Xan Gregg wrote:

If you iterate through an associative array (with the First and Next messages), the entries are returned in order of the sorted keys. If the values are unique, you can use them as keys, but if not, I think it will require explicit sorting. You might try the JMP Community Forum for ideas on the latter.


James Cultra wrote:

This method works better for matrices as the associative array function appears to round the values in the matrix (thus the unique list doesn't match the actual values in the matrix)