cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
The Discovery Summit 2025 Call for Content is open! Submit an abstract today to present at our premier analytics conference.
Get the free JMP Student Edition for qualified students and instructors at degree granting institutions.
Choose Language Hide Translation Bar
View Original Published Thread

Delete columns with for loop

yanee
Level III

Hi,

 

I create a for loop script to delete empty column (column contains only N/A).

It works ok except on one file, one column get deleted when it's not suppose to.

Not sure what's wrong

 

for (i= ncols (dt[d]),i>=1,i--,
	summarize(data table(dt[d]),temp = by(column(i)));
	if(nitems(temp) ==1 & temp[1]=="N/A",
		data table(dt[d])<< delete columns(column(i));
	);
);



1 REPLY 1
jthi
Super User


Re: Delete columns with for loop

Add supporting prints inside your for loop to see what is going on with the one specific column. I usually like using Show()

Names Default To Here(1);

dt = {};

dt_u = New Table("Untitled",
	Add Rows(3),
	New Column("Column 1", Character, "Nominal", Set Values({"N/A", "N/A", "N/A"})),
	New Column("Column 2", Character, "Nominal", Set Values({"a", "", ""})),
	New Column("Column 3", Character, "Nominal", Set Values({"b", "", ""})),
	New Column("Column 4", Character, "Nominal", Set Values({"N/A", "N/A", ""}))
);
dt[1] = dt_u << get name;
d = 1;

For(i = N Cols(Datatable(dt[d])), i >= 1, i--,
	Summarize(Data Table(dt[d]), temp = by(Column(i)));
	If(i == 3,
		show(temp);
	);
	If(N Items(temp) == 1 & temp[1] == "N/A",
		show(temp);
		Data Table(dt[d]) << delete columns(Column(i))
	);
);
-Jarmo