BookmarkSubscribe
Choose Language Hide Translation Bar
lilysecret
Community Trekker

Referring to Contiguous Columns in JSL

Hi,

 

I often have to delete a number of contiguous columns in datasets I work with. I've searched through the documentation and can't find the answer to referring to a series of contiguous columns in JSL. For example, I might use the following code to delete a set of columns:

 

Current Data Table()<< delete columns(QA_1,QA_2,QA_3,QA_4,QA_5,QA_6,QA_7,QA_8,QA_9,QA_10);

 

The list can get long and cumbersome and they aren't always in any kind of numeric order as above. I'd like a way to do the following (except this doesn't work):

 

Current Data Table()<< delete columns(QA_1 to QA_10);

 

What do I use for the "to" in the above line? Thanks very much.

0 Kudos
1 ACCEPTED SOLUTION

Accepted Solutions
mikedriscoll
Community Trekker

Re: Referring to Contiguous Columns in JSL

That function will accept a list but I don't think it will do what you want so directly. Possibly grouping them and deleting the group will work but that's a few steps too. This script seems to work. You could turn it into a function.  Run it as is, and it will stop at the stop() command. Then run the code after the stop command after the file is loaded to see the before / after. To do this you can just highlight the code after the stop() and hit ctrl+r or run.

 

Names Default To Here( 1 );

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

firstCol = "age";
lastCol = "height";
firstColNum = -1;
lastColNum = -1;

for(i = 1, i <= ncol(dt), i++,
    if(column(dt, i)<<get name() == firstCol,
        firstColNum = i;
    )
);
for(i = 1, i <= ncol(dt), i++,
    if(column(dt, i)<<get name() == lastCol,
        lastColNum = i;
    )
);

stop();
if(lastColNum != -1 & firstColNum != -1 & lastColNum >= firstColNum,
    for(i = lastColNum, i >= firstColNum, i--,
        dt << delete columns(i)
    )
);
0 Kudos
2 REPLIES 2
mikedriscoll
Community Trekker

Re: Referring to Contiguous Columns in JSL

That function will accept a list but I don't think it will do what you want so directly. Possibly grouping them and deleting the group will work but that's a few steps too. This script seems to work. You could turn it into a function.  Run it as is, and it will stop at the stop() command. Then run the code after the stop command after the file is loaded to see the before / after. To do this you can just highlight the code after the stop() and hit ctrl+r or run.

 

Names Default To Here( 1 );

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

firstCol = "age";
lastCol = "height";
firstColNum = -1;
lastColNum = -1;

for(i = 1, i <= ncol(dt), i++,
    if(column(dt, i)<<get name() == firstCol,
        firstColNum = i;
    )
);
for(i = 1, i <= ncol(dt), i++,
    if(column(dt, i)<<get name() == lastCol,
        lastColNum = i;
    )
);

stop();
if(lastColNum != -1 & firstColNum != -1 & lastColNum >= firstColNum,
    for(i = lastColNum, i >= firstColNum, i--,
        dt << delete columns(i)
    )
);
0 Kudos
lilysecret
Community Trekker

Re: Referring to Contiguous Columns in JSL

Thanks, I'll try it out and let you know!
0 Kudos