Subscribe Bookmark RSS Feed

Conditional statements, action and deleting columns

sasjmper

Community Trekker

Joined:

Jan 11, 2013

Hello Everyone,

I am having a problem with a JSL script, and I have researched the documentation, books and user forums for an answer.

I wrote a script that contained a conditional statement in JSL, and one of the “results” is to perform an action like delete a row.  Can this be done in a conditional statement?

Let’s use this sample code:

NE3=3;

NE4=5;

/* These variables can take different values. */

If (NE3==NE4,

          NewNum = NE3,

           NewNum = index (NE3, NE4)

);

If (NewNum == 0,

                  Print (“No Deletion”), 

                Current data table () << delete column (NewNum)

);

As you can see or guess, I cannot delete columns.  More importantly, I used the first conditional statement because I wanted it to create a range or single value.

However, I am not sure what to do with 0 as a value, as JMP cannot delete something that does not exist (aka, column 0).  Any suggestions will be greatly appreciated!

Many thanks,

K

1 ACCEPTED SOLUTION

Accepted Solutions
Solution

This is a little tricky.  Delete columns takes either a single column name or index, or a list of column names or indices.  This program will do the trick.  Note the AS LIST function which converts a matrix into a list:

NE3 = 3;

NE4 = 5;

/* These variables can take different values. */

dt = current data table();

nc = ncols(dt);

if (ne3 > 0 & ne3 <= nc & ne4 > 0 & ne4 <= nc,

    If( NE3 == NE4,

        dt << delete column(ne3);

        ,

        new_list = as list(Index( NE3, NE4 ));

        dt << delete column(new_list);

    );

);

6 REPLIES
Solution

This is a little tricky.  Delete columns takes either a single column name or index, or a list of column names or indices.  This program will do the trick.  Note the AS LIST function which converts a matrix into a list:

NE3 = 3;

NE4 = 5;

/* These variables can take different values. */

dt = current data table();

nc = ncols(dt);

if (ne3 > 0 & ne3 <= nc & ne4 > 0 & ne4 <= nc,

    If( NE3 == NE4,

        dt << delete column(ne3);

        ,

        new_list = as list(Index( NE3, NE4 ));

        dt << delete column(new_list);

    );

);

sasjmper

Community Trekker

Joined:

Jan 11, 2013

Thanks for your help!  I truly appreciate it.  One question that is central to conditional statement: can you perform actionable (aka, deleting and/or adding columns, rows, dt) in a conditional statement?  I don't understand why the code did not delete the specified column(s).  Was it just because I was missing the as list function from my example statement? Or was my syntax off?  I am still learning all the ins and outs of conditionals in JSL, and I see that you used semicolons to "glue" expressions together some them.  Thanks again!

pmroz

Super User

Joined:

Jun 23, 2011

You can run one statement or 1000 in a conditional statement.  You can perform actionable statements in a conditional statement, as shown by my code.

Your syntax was incorrect in a few ways.

If (NE3==NE4,

    NewNum = NE3,

    NewNum = index (NE3, NE4)

);

The result of this IF statement is that newnum is either a number or a matrix.  This is OK but perhaps a bit confusing, as newnum is being used for two different types of values (number and matrix).  But this will cause problems with the next IF statement, as shown below.

If (NewNum == 0,

    Print (“No Deletion”),

    Current data table () << delete column (NewNum)

);

This is really where the problem lies.  Newnum will only be 0 if NE3 and NE4 are 0.  If you get a matrix from the previous IF statement (when NE3 doesn't equal NE4), then the comparison of NewNum to 0 doesn't make sense.  Finally your delete column statement will only work if newnum is an integer or a list.  So the AS LIST statement is essential.

I hope this helps - this is intended as constructive criticism.

I had many difficulties when first learning JSL, especially with the IF statement!  I developed a style that includes comments showing where the THEN and ELSE parts of the statement are.  For example:

if (time_choice == 1,

// then

    date_clause = "Current";

    case_list = {1, 2, 3, 4};

// else

    ,

    date_clause = "Cumulative";

    case_list = {1, 2, 3, 4, 5, 6, 7, 8, 9};

);

sasjmper

Community Trekker

Joined:

Jan 11, 2013

No criticism at all!  I asked for it.  I see this as helping me (and the JMP community) out with pointers and working through the finer details.  I learned a great deal, and I appreciate you taking the time to answer my questions and teaching me those important finer points.

sasjmper

Community Trekker

Joined:

Jan 11, 2013

I have been working more with the conditional statements and delete columns.  I played around some more with it, and I see what you mean about delete columns taking either a single column name or index or a list of column names or indices.  However, after running through the given code and just isolating for the delete column action and creating a list of indices, I am still not able to delete a full range of indices for columns.  For example:

Let's say the dt has 13 columns, and I want to delete the first three (1-3) and the last two (12-13).

DC1 = 1;

DC2 = 3;

DC3 = 12;

DC4 = 13;

DC_List1 = As List (Index(DC1, DC2));

DC_List2 = As List (Index(DC3, DC4));

DC_List3 = DC_List1 || DC_List2;

Current data table () << delete columns (dc_list3); /* Only deletes first column b/c that's the value of the first variable. */

/* To delete even just one of the created lists, it still fails. */

current data table () << delete columns (dc_list1); /* Only deletes first column b/c that's the value of the first variable. */

Show (dc_list1);  /*dc_list1 = {{1, 2, 3}}; */

Show (dc_list3);/* dc_list3 = {{1, 2, 3}, {12, 13}}; */

I know that the "As List" function converts the matrix into a list, but I don't understand why it gives it double curly brackets.  I think that's what's throwing off my code for this.  I've tried numerious combinations to no avail.  Any thoughts/suggestions/help?  Many thanks in advance for your help!

ms

Super User

Joined:

Jun 23, 2011

Index() gives a column vector, hence the double brackets. Try to transpose the matrix.

DC_List1 = As List (transpose(Index(DC1, DC2)));

DC_List2 = As List (transpose(Index(DC3, DC4)));