Subscribe Bookmark RSS Feed

Deleting multiple columns based on column name

Highlighted
masum111bd

Occasional Contributor

Joined:

Nov 11, 2017

Hi ,

 

I want to delete multiple columns based on some specific strings they contain in column name. I can do it for single name. But for multiple name it does not work.

 

col_list = dt << get column names(string);

for (i = nitems(col_list), i > 0, i--,
if (contains(col_list[i], "SAVED_IMAGES", "sw_", "HH"),
remove from(col_list, i);
);
);

Shows this error

 

dt = Current Data Table();
col_list = dt << get column names( string );
For( i = N Items( col_list ), i > 0, i--,
If( Contains( col_list[i], "SAVED_IMAGES", "SS_EID" ),

Remove From( col_list, i )
)
);

1 ACCEPTED SOLUTION

Accepted Solutions
ih

Community Trekker

Joined:

Sep 30, 2016

Solution

If I understand your question you want to iterate through both the column names and the strings to match:

 

col_list = dt << get column names(string);

remove_strings = {"SAVED_IMAGES", "sw_", "HH"};

//for each column name
for (i = nitems(col_list), i > 0, i--,
	
	//for each string to match
	for( j = 1, j <= N Items( remove_strings ), j++,
		
		//if it matches remove the item
		if( contains( col_list[i], remove_strings[j] ) > 0,
			remove from(col_list, i);
		);
	);
);
2 REPLIES
txnelson

Super User

Joined:

Jun 22, 2012

I believe the structure you need to use for your comparison is:

if (contains(col_list[i], "SAVED_IMAGES") & contains(col_list[i], "sw_")  &
contains(col_list[i], "HH"),
Jim
ih

Community Trekker

Joined:

Sep 30, 2016

Solution

If I understand your question you want to iterate through both the column names and the strings to match:

 

col_list = dt << get column names(string);

remove_strings = {"SAVED_IMAGES", "sw_", "HH"};

//for each column name
for (i = nitems(col_list), i > 0, i--,
	
	//for each string to match
	for( j = 1, j <= N Items( remove_strings ), j++,
		
		//if it matches remove the item
		if( contains( col_list[i], remove_strings[j] ) > 0,
			remove from(col_list, i);
		);
	);
);