cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Check out the JMP® Marketplace featured Capability Explorer add-in
Choose Language Hide Translation Bar
mrosenthal
Level II

Help with an if statement

Hi,

I am probably missing something basic here. I am trying to change a column's values by using a combination of two other column values. I type in the following if statement and the column values do not change. What am I missing here? Thanks for any tips and advice. 

 

Names Default To Here( 1 );
dt = Data Table( "20210811Miflezet_CAPCMP.jmp" );
dt << Begin Data Update;
If( :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "center"
,
	 :Rile_Calc3 == "center"
	 ,
	:justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "center"
	,
	 :Rile_Calc3 == "center"
	 ,
	:justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "left"
	,
	 :Rile_Calc3 == "right"
	 ,
	:justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "left"
	,
	:Rile_Calc3 == "left"
	,
	:justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "right"
	,
	:Rile_Calc3 == "left"
	,
	:justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "right"
	,
	:Rile_Calc3 == "right"
);
dt << End Data Update;

 

1 ACCEPTED SOLUTION

Accepted Solutions
Craige_Hales
Super User

Re: Help with an if statement

In your original JSL you probably ( edit: also ) want to change

Rile_Calc3 == "center" // compare. the result of the compare (0 or 1) will be the result of the if(), see below

to

Rile_Calc3 = "center" // assign value

JSL if statements return a value; you could write it as

:Rile_Calc3 = If( 
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "center",  "center",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "center",  "center",
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "left",  "right",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "left", "left",
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "right", "left",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "right", "right",
   :Rile_Calc3 // or "Unknown" ? depends how you want to handle the unexpected case.
);

If you know there are no unexpected cases, you can omit the final otherwise clause. But the nature of unexpected cases is to show up when least expected.

 

If Secrets 

 

Craige

View solution in original post

4 REPLIES 4
jthi
Super User

Re: Help with an if statement

You could use either For Each Row or Set Each Value. Below is an example:

Names Default To Here(1);
dt = Open("$SAMPLE_DATA/Big Class.jmp");
For Each Row(
	If(:sex == "F",
		:age = .
	);
);

dt:weight << Set Each Value(
	If(:sex == "M",
		.,
		:height
	);
);
-Jarmo
mrosenthal
Level II

Re: Help with an if statement

Thanks for the answer! As the various scripting guides show, it should be the right thing yet it did not work here. I assume something was wrong with the data table and all the processing I already did on these variables. Eventually, the workaround was to concatenate the two nominal columns to one and then recode the expressions I got there to the desired outcomes. Thanks again.

Craige_Hales
Super User

Re: Help with an if statement

In your original JSL you probably ( edit: also ) want to change

Rile_Calc3 == "center" // compare. the result of the compare (0 or 1) will be the result of the if(), see below

to

Rile_Calc3 = "center" // assign value

JSL if statements return a value; you could write it as

:Rile_Calc3 = If( 
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "center",  "center",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "center",  "center",
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "left",  "right",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "left", "left",
   :justice_decision_REC_RILE2 == "reject" & :RILE_Calc2a == "right", "left",
   :justice_decision_REC_RILE2 == "accept" & :RILE_Calc2a == "right", "right",
   :Rile_Calc3 // or "Unknown" ? depends how you want to handle the unexpected case.
);

If you know there are no unexpected cases, you can omit the final otherwise clause. But the nature of unexpected cases is to show up when least expected.

 

If Secrets 

 

Craige
mrosenthal
Level II

Re: Help with an if statement

That actually worked. I did one small tweak using @jthi 's post and did small changes in @Craige_Hales's code. The final format that worked for me was this: 

dt = Data Table( "20210812Miflezet_CAPCMP.jmp" );
For Each Row(
 If( 
  :just_dec_rec == "reject" & :RILE_Rec == "center",  :RILE4="center",
   :just_dec_rec == "accept" & :RILE_Rec == "center", :RILE4= "center",
   :just_dec_rec == "reject" & :RILE_Rec == "left", :RILE4= "right",
   :just_dec_rec == "accept" & :RILE_Rec == "left", :RILE4="left",
   :just_dec_rec == "reject" & :RILE_Rec == "right", :RILE4="left",
   :just_dec_rec == "accept" & :RILE_Rec == "right", :RILE4="right"
));