Use JavaScript to process data

Updated at November 24th, 2023

You can make programmatic changes to your survey data by adding code to your project to process data. 

There are two ways to add code to your project, either in pre-calculate which run each time you open the project or in a static data process that you run manually.

For small modifications, pre-calculate is a good place to start. It's suitable for small amounts of code.

Pre-calculate

Go to project settings and click on the "Pre-calculate" tab.

You should see a page with one line of code: "return rows;". This is the most basic data process. Rows represent the primary source of a project's data. This line of code simply returns the data for the project.

Protobi will execute code in pre-calculate prior to loading your project. That's why pre-calculate is best for simple and short data processes that will not take too much time to load.

Try this

This is a process that creates a pop up message. Copy the code below, put it in pre-calculate and "Save". 

var message =  "This dataset has " + rows.length + "respondents "
alert(message)

return rows;

The message will pop up each time the project is opened, or when the browser page is reloaded. 

Add up serval values on the same row

Project pre-calculate is useful for lightweight data processing. A common example is creating a new variable that sums several data columns in a survey. 

For example, let's say we want to create a variable that sums number of patients across all products for Q2.

We can put code in pre-calculate that creates a new variable Q2_sum. 

We'll need to run the code through a loop in order to calculate the new value for each respondent. JavaScript has a method for this referred to as iterating over rows. It takes code nested within the function, and loops through every respondent in the data.  

rows.forEach(function(row) {

    row.Q2_sum = (+row.Q2_1) + (+row.Q2_2) + (+row.Q2_3)

});

return rows;

Note the use of + operator in front of each value

Protobi represents your data as a CSV text file, and all data in the file are represented as strings, e.g. the string "2" rather than the number 2. 

When processing data files, be aware of the difference because JavaScript adds strings and numbers differently. Adding two numbers yields a number (e.g., 2 + 2 is 4) but adding two strings yields a string (e.g., "2" + "2" is "22").

We can make sure numeric values are added as numbers and not strings by using the sign in front.

Result

The new variable has numeric values that you can bin into different ranges.

Note: when you create variable in pre-calc they are not automatically added to fields. You have to manually create the element by pressing on the + sign under a group or tab and reference the element key (i.e Q2_sum).  


Data process

The other way to add code is to create a new process in the data tab of Project settings.  

Return a data table as rows

If a project has a data table with  key "main" the most minimal process would simply return the results in that data table. 

var rows= data["main"]

return rows;

Combine waves of data

A common data process is to combine waves of data. If a project contains data tables for each wave of the survey, the stacking function seen below will work as a simple data process.

 var W1 = data["wave1"]
 var W2 = data["wave2"]
 var W3 = data["wave3"]
 var W4 = data["wave4"]
 
 var rows = Protobi.stack_rows([W1 ,W2, W3, W4]);
 
 return rows;

Define new variables

A slightly more complicated example is to create a new variable based on conditional logic. Below we create a new element therapy_use based on the respondent's Q1 and Q5_1 values. 

We recommend assigning integer values to new elements and using Value formats to label the values. This method is consistent with how discrete values in most surveys are programmed.

var rows = rows.main;

rows.forEach(function(row) {  
  if (row.Q1 == 1 || row.Q5_1 == 1) row.therapy_use == 1 //High use
  else row.therapy_use == 0 //Low use
})


Reminder: For data processes, "Save" and "Run" the process after you are done editing the code view. To use the result of the process as the primary data for the project, you will need to set it as "Primary". Data processes are specific to each project, and your code may not look identical to our example.

Was this article helpful?