cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP 19 is here! See the new features at jmp.com/new.
  • Register to attend Discovery Summit 2025 Online: Early Users Edition, Sept. 24-25.
Choose Language Hide Translation Bar
bub
bub
Level II

Beginner trying to Write Script / How to iterate within the formula function?

HI, I am a beginner at script writing in any language.  I am currently meandering through what I thought would be a simple practice task.  I am using JMP18.  My goal is to automate a routine operation that I do with growing datasets over time.  The routine operation is to first determine the time scale, and then show the change in data over that time scale for large datasets. The basic way I am approaching this is creating new columns to fill in all the initial data and using formulas on more columns to get differences.  If someone has a better suggestion, I'd appreciate it.  Below is where I am at:

 

Essentially, I have a set of data and I want to achieve the following. 

1) I am looking to take the date and time columns, bring them together into a single column, and then I find the minimum, and use that to create a starting date.  I can then subtract the times to see the difference in time between data.  This was all simple enough with the formula() functions.  Everything here works great.

2) I want to do something similar to several of the data columns, but I cannot do what I did before to find the initial value.  The initial data is determined by the minimum time found in what I did before for every unique item in a third column (called Unique_Fruit in the code below).  For the sake of this forum, for each unique_fruit, i want to create a column that contains the initial values of data for that fruit so i can then quickly calculate the changes from the actual data column.

My current JSL script is below.  I have a dummy code formula where I haven't figured out what to do.  It is the steps where I used the If function, found the times equal each other and set the value for only those cells.  My original thought was to try using a for loop and iterate in the formula, but that did not seem to work.

DataTable = Current Data Table();  

New Column( "Date Number Format", Numeric, "Continuous" );

For( i = 1, i <= N Rows( DataTable ), i++,
    DataTable:Date Number Format[i] = Num( DataTable:Key_Date[i] );
);

New Column( "Time Number Format", Numeric, "Continuous" );

For( i = 1, i <= N Rows( DataTable ), i++,
    DataTable:Time Number Format[i] = Num( DataTable:Key_Time[i] ); 
);

New Column( "Current_Time", Numeric, "Continuous" );

For( i = 1, i <= N Rows( DataTable ), i++,
    DataTable:Current_Time[i] = DataTable:Date Number Format[i] + DataTable:Time Number Format[i]; 
);

New Column( "Unique_Fruit", Character, "Nominal" );

For( i = 1, i <= N Rows( DataTable ), i++,
    DataTable:Unique_Fruit[i] = DataTable:Fruit[i] || "-" || DataTable:Type[i];  
);

New Column( "Starting_Date_Number_Format", Numeric, "Continuous", 
    Formula( Col Minimum( :Current_Time, :Unique_Fruit ) )
);

New Column( "Minutes", Numeric, "Continuous", 
    Formula( Date Difference( :Starting_Date_Number_Format, :Current_Time, "Minute", "fractional" ) )
);

New Column( "Days", Numeric, "Continuous", 
    Formula( Date Difference( :Starting_Date_Number_Format, :Current_Time, "Day", "fractional" ) )
);

New Column( "Weeks", Numeric, "Continuous", 
    Formula( Date Difference( :Starting_Date_Number_Format, :Current_Time, "Week", "fractional" ) )
);

New Column( "Months", Numeric, "Continuous", 
    Formula( Date Difference( :Starting_Date_Number_Format, :Current_Time, "Month", "fractional" ) )
);

New Column( "Initial_Taste", Numeric, "Continuous", 
    Formula( 
        If( :Current_Time == :Starting_Date_Number_Format, :Key_Taste, . ) 
    )
);

For( i = 1, i <= N Rows( DataTable ), i++,
    If( DataTable:Current_Time[i] == DataTable:Starting_Date_Number_Format[i], 
        DataTable:Initial_Taste[i] = DataTable:Key_Taste[i];
    );
);

New Column( "Delta_Taste", Numeric, "Continuous", 
    Formula( :Key_Taste - :Initial_Taste )
);

New Column( "%_Change_Taste", Numeric, "Continuous", 
    Formula( ( :Delta_Taste / :Initial_Taste ) * 100 + 100 )
);

 

11 REPLIES 11
bub
bub
Level II

Re: Beginner trying to Write Script / How to iterate within the formula function?

Hey, I had an additional thought as I am playing around with this.  If I wanted to do something closer to what I originally thought I had to do when I posted this, how is it done?  Where I am at is writing a variable that stores a list and then trying to have the code iterate through that list to populate the formula.  Instead of populating the variable remains inside the formula rather than updating to one of the list values. Is that something that can be done within the formula function?

jthi
Super User

Re: Beginner trying to Write Script / How to iterate within the formula function?

You can get a list of values/collect a list of values inside a formula but you have to be just careful how you update the values (using As Constant or If(Row() == 1,...) can help). Also you might want to avoid loops inside formulas as they do trigger quite often.

 

You could use Lag() but this would require your data to be sorted (can be done without sorting, but it can quickly get complicated) but usually the idea is something a bit like this

 

If(Row() == 1,
	init_taste = :Key_Taste
);

If(:Unique_Fruit != Lag(:Unique_Fruit),
	init_taste = :Key_Taste;
);

init_taste;

Do note that I did sort the data by Date Number Format and Unique_Fruit.

 

-Jarmo

Recommended Articles