kobotoolbox / kpi

kpi is the (frontend) server for KoboToolbox. It includes an API for users to access data and manage their forms, question library, sharing settings, create reports, and export data.
https://www.kobotoolbox.org
GNU Affero General Public License v3.0
133 stars 180 forks source link

Create Stata / R syntax #1996

Closed jnm closed 3 years ago

jnm commented 6 years ago

Continuation of https://github.com/kobotoolbox/kpi/issues/1607, which now tracks only SPSS label export capability.

jnm commented 5 years ago

For reference, running the test XLSForm from #1607 through https://github.com/jkpr/odk2stata produces the following Stata do-file:

/*
 *  Generated using odk2stata.py, version 0.2.3
 *
 *  ODK Source: test_syntax.xlsx
 *  Primary CSV dataset: test_syntax.csv
*  Date: 2019-03-03, 18:16:01
 *  Author: beeswax
 */

import delimited "test_syntax.csv", charset("utf-8") delimiters(",") stringcols(_all) bindquote(strict) clear

/* ---------------------------------------------------------
         SECTION 1: Drop columns
   --------------------------------------------------------- */

/* ---------------------------------------------------------
         SECTION 2: Rename
   --------------------------------------------------------- */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
         SUBSECTION: Rename to original ODK names
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
rename what_is_your_age What_is_your_age
rename what_is_your_name What_is_your_name

/* ---------------------------------------------------------
         SECTION 3: Destring
   --------------------------------------------------------- */

destring What_is_your_age, replace

/* ---------------------------------------------------------
         SECTION 4: Encode select ones
   --------------------------------------------------------- */

label define name 1 "Franck_M" 2 "Janvier_P" 3 "Jeanette_L" 4 "mariam_K" 5 "Micheline_K" 6 "thierry_K" 7 "Tite_N" 8 "Winnie_Y" 9 "Francine_W" 10 "Gervais_B" 11 "Gilardin_B" 12 "Hortense_B" 13 "joseph_S" 14 "Lucien_M" 15 "Minerve_B" 16 "Prisca_C" 17 "Yusuf_M" 18 "Prince_M" 19 "benjamin_K" 20 "benjamin_M" 21 "Clarice_A" 22 "Furaha_B" 23 "Gerard_C" 24 "Jean-Christian_M" 25 "London_M" 26 "Sylvain_M" 27 "Djulfa_D" 28 "Christine_F" 29 "Clement_M" 30 "Delphin_M" 31 "diego_L" 32 "Esperance_M" 33 "Francine_N" 34 "Francis_L" 35 "Gauthier_B" 36 "Jacques_M" 37 "Julie_M" 38 "Patient_M" 39 "Sassou_M" 40 "aline_ucb" 41 "chance_ucb" 42 "evelyne_ucb" 43 "grace_bm_ucb" 44 "etienne_ucb" 45 "barthelemy_ucb" 46 "theo_ucb" 47 "charles_ucb" 48 "aimee_ucb" 49 "yvette_ucb" 50 "vianney_ucb" 51 "jules_ucb" 52 "grace_rc_ucb" 53 "solange_mak_ucb" 54 "solange_mon_ucb" 55 "arlette_ulpgl" 56 "plaisir_ulpgl" 57 "christian_Mp_ulpgl" 58 "christian_ulpgl" 59 "daniel_ulpgl" 60 "eloise_ulpgl" 61 "joelle_ulpgl" 62 "mamy_ulpgl" 63 "levi_ulpgl" 64 "lucie_ulpgl" 65 "muyisa_ulpgl" 66 "nadine_ulpgl" 67 "georges_ulpgl" 68 "nathalie_ulpgl" 69 "renilde_ulpgl" 70 "obed_ulpgl" 71 "olivier_ulpgl" 72 "pyame_ulpgl" 73 "thomas_ulpgl" 74 "chrysostome_ulpgl" 75 "david_ulpgl" 76 "safi_ulpgl" 77 "donatiane_ulpgl" 78 "buroko_ulpgl" 79 "annie_ulpgl" 80 "raphael_ulpgl"
label define yesno 1 "1" 2 "2"

encode What_is_your_name, gen(What_is_your_nameV2) lab(name)
order What_is_your_nameV2, after(What_is_your_name)
drop What_is_your_name
rename What_is_your_nameV2 What_is_your_name

foreach var in q_2241 q_2242 approval  {
encode `var', gen(`var'V2) lab(yesno)
order `var'V2, after(`var')
drop `var'
rename `var'V2 `var'
}

label define name 1 "Franck Mbuyu Ziaka" 2 "Janvier Paluku Bwakyanakazi" 3 "Jeanette Longhi Faida" 4 "Mariam Kalimasi Jamil" 5 "Micheline Kitamuliko Masika" 6 "Thierry Katsongo Malikidogo" 7 "Tite Nyangwibe Maneno" 8 "Winnie Yaya" 9 "Francine WINGI" 10 "Gervais BAHOGWERHE BASHIZI" 11 "Gilardin BAGUMA" 12 "Hortense BARHOLERE" 13 "Joseph Salumu Mwamba" 14 "Lucien MUNYAKA" 15 "Minerve BASHIGE BYADUNIA" 16 "Prisca CHINOGERWA" 17 "Yusuf MOHAMMED" 18 "Prince MUCINDI" 19 "Benjamin Kasereba" 20 "Benjamin Muyeme Tabani" 21 "Clarice Akubese Nasemande Avuta Mbafele" 22 "Furaha Bahimbire" 23 "Gerard Cibinda" 24 "Jean Christian Mukena Muntu" 25 "London M. Kalume" 26 "Sylvain Medho Kalumbo" 27 "Djulfa Djuma" 28 "Christine Faida Kabino" 29 "Clement Matembera" 30 "Delphin Muhindo" 31 "Diego Luneno Bwira" 32 "Esperance Masika" 33 "Francine Zalia Ntambwe" 34 "Francis Lenzo" 35 "Gauthier Bisimwa" 36 "Jacques Mubalama Bishikwabo" 37 "Julie Mujinga" 38 "Patient Muhozi" 39 "Sassou Mahatane" 40 "BAHATI Aline" 41 "BAHATI MITIMA Chanceline" 42 "BALYARMWABO MULAGANO Evelyne" 43 "BAZIRIA MIHIGO Grace" 44 "BIRINDWA FLESI Etienne" 45 "BUNANI BIRERE Barthelemy" 46 "BWAMUCHUMA BILINGI Théo" 47 "Charles Muderhwa" 48 "Cirhibuka Aimee" 49 "EKA MUKANIRE Yvette" 50 "Mubagwa Musaka Vianney" 51 "NYUNDA NKURU Jules" 52 "RANGA CINYEMA Gracia" 53 "RIZIKI MAKELELE Solange" 54 "RIZIKI MONGANE Solange" 55 "Arlette Syambite" 56 "Amida SIFA Plaisir" 57 "Christian Mpabwa" 58 "Christian Mwenyemali Milenge" 59 "Daniel BUTOKI KIRINDERA" 60 "Eloise Nkuo Clarisse" 61 "Joelle MUGHUMALEWA" 62 "Kavira Mwendevya Mamy" 63 "Levi Bouesi" 64 "Mapenzi Kissa Lucie" 65 "Muyisa Tasakana" 66 "Mwadjuma Lubuto Nadine" 67 "Namegabe Kubaburhanzi Georges" 68 "Nathalie Bishikwabo" 69 "Nzigire Kalegamire Renilde" 70 "Obed MBUSA MULEMBERI" 71 "Olivier Kisamba" 72 "Pyame Mushagalusa Balemirwe" 73 "Thomas Tcheshe" 74 "Kasereka Muyisa Jean Chrysostome" 75 "David Kubuya Baroki" 76 "Kasoki Wasunduku Safi" 77 "KAVUGHO SIBANZIRE Donatiane" 78 "Kanuni Buroko" 79 "Mutongerwa Venge Annie" 80 "Wema Lukoo Raphael", replace
label define yesno 1 "Yes" 2 "No", replace

/* ---------------------------------------------------------
         SECTION 5: Split select multiples
   --------------------------------------------------------- */

label define o2s_binary_label 0 No 1 Yes

/* ---------------------------------------------------------
         SECTION 6: Label variable
   --------------------------------------------------------- */

* LABEL SKIPPED: variable "start" has no label.
* LABEL SKIPPED: variable "end" has no label.
label var What_is_your_age "What is your age?"
label var What_is_your_name "What is your name?"
label var group_separationquestions "separation"
* LABEL TOO LONG: original label is 84 characters long. the last 4 characters will be lost.
label var q_2241 "Is your family missing a child that was separated from you during your displacem"
* LABEL TOO LONG: original label is 81 characters long. the last 1 characters will be lost.
label var q_2242 "Is your family hosting a child separated from his/her family during displacement"
label var q_2243 "How did their separation occur?"
* LABEL TOO LONG: original label is 771 characters long. the last 691 characters will be lost.
label var approval "Hello, my name is ____ I am here as part of a study on security, protection and "

local today=c(current_date)
local date=subinstr("`today'", " ", "", .)
save "test_syntax_`date'.dta", replace
joshuaberetta commented 3 years ago

Moved to: https://www.notion.so/kobotoolbox/Create-Stata-R-syntax-fd9b9eec4eae494bb785cb0090d481ee