cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Browse apps to extend the software in the new JMP Marketplace
Choose Language Hide Translation Bar
terapin
Level VI

Formula to use previous value if not missing, otherwise use value from last occupied cell

I'm trying to write a formula in Col3 that adds the current value of Col1 with the lagged value of Col2 but I'm running into a pickle trying to figure out how to carry forward the last observation from Col2 when missing data is encountered and to use this value at the first instance of non-missing data.  That is,

 

 

If( !IsMissing( Col1)
     Col1 + Lag(Col2,1);
     //create variable that contains current Col2 value;

//Else
     Col1 + previously stored value from Col2;

);

 

I would appreciate any suggestions on how best to accomplish this.  Thanks.

 

Row# Col1 Col2 Col3
1 2 1  
2 1 3 2
3 1 5 4
4 2 7 7
5 . .  
6 . .  
7 . .  
8 2 8 9
9 2 9 10
10 1 11 10
11 . .  
13 . .  
14 2 12 13
15 2 13 14
16 1 14 14
17 2 15 16
1 ACCEPTED SOLUTION

Accepted Solutions
jerry_cooper
Staff (Retired)

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I think this formula will give you what you're after:

If( Is Missing( :Column 1 ),
	:Column 1,
	Summation( i = 1, Row(), :Column 1 )
)

Hope this helps.

-Jerry

View solution in original post

15 REPLIES 15
txnelson
Super User

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

Here is one version of the formula that works:

 

 

If( Row() == 1, lagval = . ); 
x = Sum( :col 1, lagval ); 
If( Is Missing( col 2 ) == 0, 
       lagval = :col 2 
);
x;

 

 

Jim
Jeff_Perkinson
Community Manager Community Manager

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I think there's an error in your description that we'll have to clear up before we can give you a precise solution. Your conditional says if that if Col1 is NOT MISSING then add it to the lagged value of Col2, and if Col1 IS MISSING then add that to a previously stored value of Col2.

You can't add a missing Col1 to anything and get anything other than missing.

Having said that, I would build this table out one column at a time. Specifically, I would create a "non-missing Col2" column which had no missing values using the formula:

11375_JMPScreenSnapz015.png

This formula sets "non-missing Col2" to Col2 when Col2 isn't missing and sets it to the Lag of "non-missing Col2" when Col2 is missing.

11376_JMPScreenSnapz016.png

You can then use a Lag(non-missing Col2, 1) in your final formula.

I hope that helps.

-Jeff

-Jeff
terapin
Level VI

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

Thanks Jim and Jeff,

I think I made the issue more complicated than it needs to be in my description and table layout and can see how that may have led you to your suggestions.

While I understand your suggestion Jeff about creating a new column that holds the non-missing data I'm hoping to accomplish my work without creating new columns since I'll be using this formula for many different columns.  Let me try to be more clear about what I'm trying to accomplish by way of the table below.

I'm trying to create a running sum of Column 1 in Column 2.  Without creating any additional columns, I was hoping to create a variable that contains the current running sum value in Column 2 and adds that to the value of Column 1.

My issue is not knowing the best way to create and use a variable that contains the present value for Column 2 so that this value can be "carried" across rows of missing Column 1 data.  Column "Result I'm Looking For" shows what I would like the formula in Column 2 to produce.  Hopefully this description is a bit more clear about what I'm looking to accomplish.

11378_pastedImage_1.png

jerry_cooper
Staff (Retired)

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I think this formula will give you what you're after:

If( Is Missing( :Column 1 ),
	:Column 1,
	Summation( i = 1, Row(), :Column 1 )
)

Hope this helps.

-Jerry

markschahl
Level V

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I run into this problem all of the time and use the solution give by Jerry. I analyze process measurements (temperature, pressure, flow, etc.) that are retrieved from a compressed-data-historian. Every few thousand points, there can be a missing value.

 

Example below calculates if reactor is running or not based on 3 point moving average of catalyst flow, F.Cat >= 11 (anonymized yes, you get the reference...). If F.Cat is missing, calc uses previous row's value of "Running?".

If(
	Row() < 3, 0,
	Is Missing( :F.Cat ), Lag( :Name( "Running?" ), 1 ),
	(Lag( :F.Cat, 2 ) + Lag( :F.Cat, 1 ) + :F.Cat) / 3 >= 11, 1,
	0
)
cjw0
Level II

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I think this is exactly what I need as well, but I don't fully understand the script....is it possible to show what this would look like in the formula editor... 

 

thanks so much

jerry_cooper
Staff (Retired)

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

I believe this response was written prior to the release of JMP 13 which introduced the Col Cumulative Sum() function. If you are using v13 or higher, all you need do to create a cumulative sum column is right-click the column header for the column you wish to sum and select New Formula Column=>Row=>Cumulative Sum. This will create a new column with the appropriate formula. To see the formula in the editor, right-click the new column and select Formula.

If you are using a prior version, copy and paste the following statement into the formula editor to see what it looks like:

Summation(i=1, Row(), Your Column[i])

Hope this helps.


 

cjw0
Level II

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

What I am tyring to do is ask the question....what is the date difference between two different biopsies (column labelled TCMR) for a given patient (pt number in first column) in my database. The biopsies occur at irregular intervales and the outcome of the biopsy is shown in the third column below.

Although I know how to retrieve this information if I simply create a subset of only the rows which contain a biopsy result and then ask for the date difference....what I would likel to do is figure out a way to determine the date difference between any to rows that have a biopsy result while ignoring the empty rows.


@jerry_cooperwrote:

I think this formula will give you what you're after:

 

If(Is Missing(:Column 1), :Column 1, Summation(i = 1, Row(), :Column 1))

 

Hope this helps.

-Jerry


 

Can you help with this.  Ideally with a solution that I can see in the formula editor or can paste into it.

 

jerry_cooper
Staff (Retired)

Re: Formula to use previous value if not missing, otherwise use value from last occupied cell

Unfortunately, I can't see the example table you provided, however, the enclosed file might give you an idea for an approach to take the difference between the current and previous non-missing dates. 

The formula in the second column first establishes a matrix of rows for the non-missing cells in the Date column. This provides a way to reference the previous, non-missing row which is used as an index in the Date Difference function. 

I'm sure there are other ideas for doing this...