cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar

Using Boolean Functions

Started ‎11-08-2022 by
Modified ‎11-08-2022 by
View Fullscreen Exit Fullscreen

In this demonstration, we'll take a look at Boolean functions, as well as the If function I'm going to get a script editor window. And again, the keyboard shortcut is Ctrl+T or you can go to the File menu, use the toolbar. Many different ways to get that new Script Editor window. And let me bring the log in front of the journal. Back in the Script Editor window, I'm going to type the code where I assign the value of to the variable x. And I'll go ahead and initialize that. And then on a new line, I'm going to make a comparison and one of the things in JSL is that we can join arguments by comparison operators and have JMP treat them as a single clause. So if I typed less than x less than equal this will be evaluated all at once rather than sequentially because these comparison operators when JMP sees them strung together like this are what are called eliding operators. So I can run that line, and of course, that's a true comparison that is greater than while being less than or equal to So I get the Boolean as the result. Now, probably our most common use of the comparison functions are when we're testing particular conditions, for example, in an If function. So I'm going to change the first line and replace the with a So I'm assigning the value of to x. And then I'm going to replace the second line entirely with my If statement. So If and then in parentheses, x less than comma, the results that'll return will be the character string under the limit. So this is a simple if/then statement. If the condition evaluates as true, I return this result. If the condition evaluates is false, I don't evaluate the second argument and I return a missing value. So I'm going to run this whole script. Again, keyboard shortcut for that, Ctrl+R. So if x is storing the value then it is less than and we get this result. But if I change the value I assigned to x to be and run the whole script, I get that missing value because that condition was not true. And again, we can have an else result. So if the condition is not true, instead of a missing value, I'll return this other result. And here, I'll open up my double quotes and type Over the limit. And if I run that whole script, I get that else result. So the If function can take as many conditions and results as are necessary, and then you can always have that else result if all of the conditions evaluate to be false. So in the next example, we're going to take a table of people's age in years and classify it into one of Erik Erikson's stages of psychological development. So I'm going to go back to the journal in Section and I'll click Age Data. So we have individuals, and we want to bin them into these stages of psychological development. So again, back in the journal, I'll click Age to Stage Script. So this pre-written script from the journal is sending the New Column message to the data table Age with-- the column will have the title of Stage, and we're specifying that the data type will be character. Then once the column has been created, we're using the for each row function to populate it using an If statement. And recall, that once JMP encounters a true condition on the If statement, the only other argument that will be evaluated is the corresponding result. None of the other conditions will be evaluated. So a person who is age all of these age comparisons are true, but as soon as we evaluate, Yes, is greater than we return the Late Adult result and go on to the next row since we're doing this in the context of iteration. So in that way, we don't have to bracket each level. We can just say if it's greater than if it's greater than and so on. And one thing to think about, if you looked at the data-- in this case, they're pretty uniformly distributed. But if you looked at the data and noticed that it tended to have a lot of observations up at the high end or a lot of observations down to low end, then that's the end of your distribution that you want to start binning from because if there's lots and lots of people whose age is greater than in your data table, then in many rows, the data table will evaluate this one condition, return this result, and go on to the next step of the iteration rather than evaluating everything else. So any rows-- and again, if that's the majority where people are over you'll just speed up your script processing a little bit by moving on to the next iteration as soon as that true result is found. So let's go ahead and run the script. And you can see the column was created and populated then using the If statement. Two other types of functions that we want to talk about here are logical functions and inquiry functions. So in many cases, you need to test for more than one condition at a time. And you can achieve that by combining your conditions in a logical way. And some examples of logical functions would be the AND and OR functions. So the AND function, which has the ampersand operator, will ensure that all of the conditions are true. The OR function has the vertical pipe operator. That ensures that at least one condition is true. So I'll just use the script window where I have the Age to Stage script and clear that out. I'm going to define x as being assigned the value of semicolon, y will be assigned the value of another semicolon, and I'm going to type all on one line and let the reformat clean things up. So if in parentheses, x less than And then the operator here for OR is that vertical bar, vertical pipe, if x less than OR y greater than So that's my condition. Type a comma there. If either of those or both are true, then I'm going to return the result condition met. And if neither is true, I'll return the result as a character string condition not met. And I'll type my ending semicolon again, as a reminder, we consider that a best practice but it is not required to end your script with a semicolon. I'll go ahead and right click to reformat. And so that just clean that up a little bit after I typed it all out on one line. So if I run the script, at least one of those two conditions is true, I expect to get the first result condition met. And that is, in fact, what I get in the log. If I needed both conditions to be true, I'm going to replace the vertical pipe operator with the ampersand. In this case, only one condition is true but not both. So I expect to get the else result. And when I run it, that's what I see in the log. The other type of function that I mentioned are the inquiry functions. Sometimes you want to be able to check the nature of a value. As an example, maybe you need to know if the value is a number or a list. There's a lot of different types of values that are possible. In some cases, you might get an error if you try to run a script with the wrong kind of value stored in a variable. So you want to make sure that you inquire about that, and maybe your script goes one way or another, depending on the nature of the value in that variable. So I'm going to clear the contents of the Script Editor again. In the first line, I'm going to assign to x a matrix. So I'll type x, the assign function, and then square bracket, space comma. So that's my first row. space that's my second row. Close that square bracket, type my semicolon, and then I'll do this in the context of an if statement and I also want to show you that there is this auto complete ability for functions in JSL. And if I type the first several letters of a function. In this case, the inquiry functions all start with the letters I-S for is. And then press the Control key and the spacebar. I can arrow down through this list to find the inquiry function I want in this case, I'll use Is Matrix, and then I'll press Enter to select that and JMP completes the function for me. I, of course, still have to open the parentheses, and I'll put x in there, close the parentheses, and then before the closing parenthesis of the If statement, I'll type a comma, and I'll use the same results that are condition met, closed double quotes, comma, open double quotes condition not met. Let me arrow to make sure I've got everybody lined up, and I'll right click to reformat. So in this case, x is storing a matrix. So I expect to get this first result, and when I run the script, I do get that. Let's suppose, though, that my script is going to do something different if what x is storing is a string. Maybe this is user input, maybe it's retrieved from a data table or somewhere else. So I'm going to change the word matrix here to string, and I'm going to run the script. And so, of course, I get the else result because the condition evaluated as false. So here, you saw some examples of Boolean functions using the If function to bin data as well as using the If function in conjunction with logical functions and inquiry functions.