Turn on suggestions

Auto-suggest helps you quickly narrow down your search results by suggesting possible matches as you type.

Showing results for

- JMP User Community
- :
- Discussions
- :
- Re: Recode a series of columns with a loop

News

On June 1, we’re asking you to select a content label when starting a new topic in the Discussions area. Read more to find out why.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Jul 17, 2018 10:58 AM
(3499 views)

Hello,

I frequently receive multi-punch data "exploded" into separate columns, where a positive response for option 1 = 1, option 2 = 2, etc.. I have to recode these to binary and I'd like to have this in a reproducable script. I think I'm on the right track with the following script, but I get the error "object not subscriptable{1} in access or evaluation of 'Subscript'" when I run it. I've attached a sample data set. Thanks in advance.

```
dt = Current Data Table();
lstColNames = {"14_1","14_2","14_3","14_4","14_5","14_6","14_7","14_8","14_9","14_10","14_11","14_12","14_13"};
For Each Row( For( i = 1, i <= N Items( lstColNames ), i++, If( lstColNames[i][]== i, lstColNames[i][] = 1 ) ) );
```

1 ACCEPTED SOLUTION

Accepted Solutions

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

See if this is what you want

```
Names Default To Here( 1 );
dt = Current Data Table();
lstColNames = dt << get column names( string, nominal );
For( i = 1, i <= N Items( lstColNames ), i++,
theRows = dt << get rows where( As Column( dt, lstColNames[i] ) >= 1 );
If( N Rows( theRows ) > 0,
Column( dt, lstColNames[i] )[theRows] = 1
);
theRows = dt << get rows where( Is Missing( As Column( dt, lstColNames[i] ) ) == 1 );
If( N Rows( theRows ) > 0,
Column( dt, lstColNames[i] )[theRows] = 0
);
Column( dt, lstColNames[i] ) << delete property( "Value Labels" );
```

Jim

4 REPLIES 4

Highlighted

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

See if this is what you want

```
Names Default To Here( 1 );
dt = Current Data Table();
lstColNames = dt << get column names( string, nominal );
For( i = 1, i <= N Items( lstColNames ), i++,
theRows = dt << get rows where( As Column( dt, lstColNames[i] ) >= 1 );
If( N Rows( theRows ) > 0,
Column( dt, lstColNames[i] )[theRows] = 1
);
theRows = dt << get rows where( Is Missing( As Column( dt, lstColNames[i] ) ) == 1 );
If( N Rows( theRows ) > 0,
Column( dt, lstColNames[i] )[theRows] = 0
);
Column( dt, lstColNames[i] ) << delete property( "Value Labels" );
```

Jim

Highlighted
##

Thanks, TX, that works for the sample I posted perfectly. How would I restrict this to only columns 14_1 to 14_13 when they are in a larger dataset that would include many more columns? That's why I was trying to use a specific list.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Recode a series of columns with a loop

Highlighted
##

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Recode a series of columns with a loop

`For( i = 1, i <= 13, i++,`

Just reduce the For() loop

Jim

Highlighted
##

Thanks! I was able to use this to create a reproducable script for my projects. I appreciate the assist!

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Email to a Friend
- Report Inappropriate Content

Re: Recode a series of columns with a loop