cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
ileshem
Level III

JSL Compare value in row to the next row.

Hello,

I am trying to use "FOR" and "IF" to create a condition column. 

I have this table and I want the "Con" column to indicate if the age in the row after is lower then the current row. if yes "1" if not "0"

NameAgeCon
David230
Bari261
James120
Lora140

 

This is what I have: (doesn't work)

dt<< New Column( "Con", Formula( For( i = 1,i <= N Row( dt ), i++, If(:Age[i] >= :Age[i+1] , "0", "1", ))));

 

Your help will be appreciated. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: JSL Compare value in row to the next row.

Here is two ways to do this (Row() and Lag():

Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(4),
	Compress File When Saved(1),
	New Column("Name", Character, "Nominal", Set Values({"David", "Bari", "James", "Lora"})),
	New Column("Age", Numeric, "Continuous", Format("Best", 12), Set Values([23, 26, 12, 14])),
	New Column("Con", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 0, 0]))
);

dt << New Column("Younger than Next_Row", Character, "Nominal", 
Formula(If(:Age > :Age[Row() + 1], "Yes", "No")));


dt << New Column("Younger than Next_Lag", Character, "Nominal", 
	Formula(If(:Age > Lag(:Age, -1), "Yes", "No"))
);
-Jarmo

View solution in original post

3 REPLIES 3
ian_jmp
Staff

Re: JSL Compare value in row to the next row.

Is this what you are after?

New Table( "Ages",
	Add Rows( 4 ),
	New Column( "Age",
		Numeric,
		"Ordinal",
		Format( "Best", 12 ),
		Set Values( [23, 26, 12, 14] )
	),
	New Column( "Younger than Previous",
		Numeric,
		"Nominal",
		Format( "Best", 12 ),
		Formula( If( :Age < Lag( :Age, 1 ), 1, 0 ) ),
		Value Labels( {0 = "No", 1 = "Yes"} ),
		Use Value Labels( 1 ),
		Set Display Width( 125 )
	)
)
ileshem
Level III

Re: JSL Compare value in row to the next row.

Thanks, this could work but it will mark as "Yes" the row 3 (James) and i am trying to mark row 2 (Bari) with a "Yes"
Any other solution? 

jthi
Super User

Re: JSL Compare value in row to the next row.

Here is two ways to do this (Row() and Lag():

Names Default To Here(1);

dt = New Table("Untitled",
	Add Rows(4),
	Compress File When Saved(1),
	New Column("Name", Character, "Nominal", Set Values({"David", "Bari", "James", "Lora"})),
	New Column("Age", Numeric, "Continuous", Format("Best", 12), Set Values([23, 26, 12, 14])),
	New Column("Con", Numeric, "Continuous", Format("Best", 12), Set Values([0, 1, 0, 0]))
);

dt << New Column("Younger than Next_Row", Character, "Nominal", 
Formula(If(:Age > :Age[Row() + 1], "Yes", "No")));


dt << New Column("Younger than Next_Lag", Character, "Nominal", 
	Formula(If(:Age > Lag(:Age, -1), "Yes", "No"))
);
-Jarmo