Toggle navigation
Home
▼ Details
Products and pricing
Chart gallery
User stories
Text analytics
CDC NAMCS Library
Blog
Tutorials
Contact
Sign in
Post Editor
← All help posts
View post
Save
Programmatically stack survey data loops (e.g. patient cases) by adding the code below as a project data process. <img src="/uploads%2Fupload%2Fimage%2F5200%2Fdirect%2F1612467709652-1612467709652.jpeg" alt="Diagram showing survey data structure with two sections. Left side: 'Main survey' box containing questions Q1-Q5 with various input types (radio buttons, checkboxes, three-circle options, numeric spinners, and text field) across three rows. Right side: 'Patient report form (loop)' in cyan showing three identical boxes labeled 'Patient Case #1', 'Patient Case #2', and 'Patient Case #3', each containing questions B1-B4 (circles, checkboxes, numeric spinner, and calendar icon). Bottom text reads 'Analyzing and exporting data for loops requires a bit more nuance....'" style="box-sizing: border-box; border: 0px; max-width: calc(100% - 10px); cursor: pointer; padding: 0px 1px; color: rgb(56, 76, 96); font-family: sans-serif; font-size: 16px; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-thickness: initial; text-decoration-style: initial; text-decoration-color: initial; background-color: rgb(255, 255, 255); width: 632px;" class="fr-fic fr-dib"> ## Data process code The example code below selectively stacks data columns that are looped for patient chart. The objective is to modify the data from a file where each row represents a respondent into a file where each row represents a patient chart. ### Identify the number of patient loops Declare a variable called suffixes and set it as an array with the numbers of each patient chart loop. Then, declare a variable called removed\_suffixes and with all the columns that we will delete. The idea here is that all the patient data will be stacked into the first iteration of the patient chart loop. For instance, the flattened patient chart loop has questions q300\_1, q300\_2, q300\_3, q300\_4, and q300\_5, but once we stack the data all the q300 value will be under one column q300\_1 and columns with all other suffixes can be deleted. ### Identify which columns you want to stack Modify the **stacked\_elements** to specify the looped survey data columns (minus the suffix that represents the loop number). ### Weights and new rows You will also need to adjust the conditional statement to work for your survey in order to create the weights and create a new row for each patient chart. In this case we use a column called q3a which asked HCPs for their patient's initials at the start of the patient chart section. If q3a\_5 exists, that means the HCP entered 5 patients charts and their weight should be 1/5, if q3a\_5 doesn't exist, the code will look to q3a\_4 and so forth. Similarly within the suffixes loop, we will only create a patient chart row for each iteration of the loop an HCP answered. ### Delete obsolete columns After all the patient rows of created, the code runs through the stacked\_elements and deletes every column with suffixes that are included in our removed\_suffixes list. Since all the data is stacked into the first iteration of the patient chart loop, the columns that originally represented the 2nd, 3rd, etc. loop are no longer necessary. ## Code ``` var rows = data["main"] //Define rows var rows = []; //Define rows as an empty array var suffixes = ["1","2","3","4","5"] var removed_suffixes = ["2","3","4","5"] // Stack patient case data // This first code block specifies questions looped for patient chart var stacked_elements = [ "q3a", "q300", "q300a", "q301", "q301html_old", "q301a", "q301b", "q302", "q303html", "Q304_1", "Q304_2", "Q304_3", "Q304_4", "Q304_5", "Q304_6", "Q304_7", "Q304_10", "Q304_11", "Q304_12", "Q304_8", "Q304_9", "q304_8_other" ]; rows.forEach(function(row) { if (row.hidstatus == 'COMPLETE') { //Weight is 1/(# of charts) if (row.q3a_5) weight = +1/(5) else if (row.q3a_4) weight = +1/(4) else if (row.q3a_3) weight = +1/(3) else if (row.q3a_2) weight = +1/(2) else if (row.q3a_1) weight = +1/(1) else weight = 0; suffixes.forEach(function(c){ if(row["q3a_"+c]) { var new_row = _.clone(row); new_row.patient = c; new_row.md_weight = weight; new_row.pt_weight = 1; stacked_elements.forEach(function(e){ new_row[e+"_1"]=row[e+"_"+c]; removed_suffixes.forEach(function(x){ delete new_row[e+"_"+x]; }) }) rows.push(new_row); } }) }; }) return rows; ``` [Delete](#) **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.*
Publishing
Date
Status
Published
Draft
Slug
edit
Content
Thumbnail
Categories
Manage
New to Protobi?
Charts
Making Changes
Intermediate topics for editors
Frequently Asked Questions
SERMO Topics
Tutorial Pages
Internal Docs
Data Processing
Videos
Obsolete
GSG Admin
SERMO Admin
GSG Topics
How to...
Basics for viewers
Basics for editors
For project admins
Advanced topics
Assessments
Articles (in-progress)
New and updated articles
Process data in Protobi
superseded
Text open-end questions
Troubleshooting
Tracking studies
Organizing the view
API References
Tools
AI Database
Checking AI...
Convert to MD
Danger zone
Delete