For the purpose of this page, we use the terms “rows” and “cases” interchangeably to refer to the entries of the dataset. In many of the processes made along the deduplication of entries in the C1 dataset, we used unstandardized columns or many other data that was in fact duplicated by HASHs that did not depend on events related to treatment.In order to find and delete duplicated data that does not add information relevant for the purposes of the study, we now may use these standardized variables as a criteria to achieve the goal of having a unique event per HASH, by reducing its complexity based on irrelevant differences.


Once we distinguished between the different treatments in the fourth part of the deduplication process, we proceeded to describe the dataset in terms of the time required for determined events to occur.

The process is shown in the following figure:


#knitr::include_graphics("G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/SUD_CL/Figures/Figure_Duplicates.svg")
#foreign::write.dta(CONS_C1_df_dup_JUL_2020_match_top_sel, "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/c1_top.dta")

#CONS_C1_df_dup_JUL_2020 CONS_C1_df_dup_JUL_2020_match_top CONS_TOP_df_dup_ENE_2020_prev9_match CONS_TOP_df_dup_ENE_2020_prev9
#sólo match
comb_datasets_a_n<-CONS_C1_df%>%
    janitor::clean_names() %>% 
    nrow()
comb_datasets_a_users<-CONS_C1_df%>%
    janitor::clean_names() %>% 
    dplyr::distinct(hash_key)%>% nrow()
#sólo los que tienen compromiso biopsicosocial
comb_datasets_b_n<-CONS_C1_df_dup_JUL_2020%>%
    nrow()
comb_datasets_b_users<-CONS_C1_df_dup_JUL_2020%>%
    dplyr::distinct(hash_key)%>% nrow()
#sólo los que tienen etapa de tratamiento de inicio de tratamiento
comb_datasets_c_n<-CONS_C1_df_dup_JUL_2020%>%
    dplyr::arrange(hash_key,fech_ing) %>% 
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(dup=row_number()) %>%
  #janitor::tabyl(dup) %>% 
#*La ventaja de duplicates_filtered es que considera los perdidos
    dplyr::mutate(duplicates_filtered= n()) %>%
    dplyr::filter(duplicates_filtered==1) %>% 
  #janitor::tabyl(duplicates_filtered)
    nrow()
comb_datasets_c_users<-CONS_C1_df_dup_JUL_2020%>%
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(dup=row_number()) %>%
  #janitor::tabyl(dup) %>% 
#*La ventaja de duplicates_filtered es que considera los perdidos
    dplyr::mutate(duplicates_filtered= n()) %>%
    dplyr::filter(duplicates_filtered==1) %>% 
    dplyr::distinct(hash_key)%>% nrow()

comb_datasets_d_n<-CONS_C1_df_dup_JUL_2020%>%
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(dup=row_number()) %>%
  #janitor::tabyl(dup) %>% 
#*La ventaja de duplicates_filtered es que considera los perdidos
    dplyr::mutate(duplicates_filtered= n()) %>%
      dplyr::filter(dup>1) %>% 
  #janitor::tabyl(duplicates_filtered)
    nrow()
comb_datasets_d_users<-CONS_C1_df_dup_JUL_2020%>%
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(dup=row_number()) %>%
  #janitor::tabyl(dup) %>% 
#*La ventaja de duplicates_filtered es que considera los perdidos
    dplyr::mutate(duplicates_filtered= n()) %>%
    dplyr::filter(dup>1) %>% 
    dplyr::distinct(hash_key)%>% nrow()

#https://stackoverflow.com/questions/46750364/diagrammer-and-graphviz
#https://mikeyharper.uk/flowcharts-in-r-using-diagrammer/
#http://blog.nguyenvq.com/blog/2012/05/29/better-decision-tree-graphics-for-rpart-via-party-and-partykit/
#http://blog.nguyenvq.com/blog/2014/01/17/skeleton-to-create-fast-automatic-tree-diagrams-using-r-and-graphviz/
#https://cran.r-project.org/web/packages/DiagrammeR/vignettes/graphviz-mermaid.html
#https://stackoverflow.com/questions/39133058/how-to-use-graphviz-graphs-in-diagrammer-for-r
#https://subscription.packtpub.com/book/big_data_and_business_intelligence/9781789802566/1/ch01lvl1sec21/creating-diagrams-via-the-diagrammer-package
#https://justlegal.be/2019/05/using-flowcharts-to-display-legal-procedures/
#
#
library(DiagrammeR) #⋉
grViz("digraph flowchart {
      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle,fontsize = 10]        
      tab1 [label = '@@1']
      tab2 [label = '@@2']
      tab3 [label = '@@3']
      tab4 [label = '@@4']

      # edge definitions with the node IDs
      tab1 -> tab2 [label='  Discarded duplicated & abnormal records',fontsize = 9];
      tab2 -> {tab3 tab4} 
      }
Only rows with available data on TOP scores

      [1]:  paste0('Original records \\n(n = ', formatC(comb_datasets_a_n, format='f', big.mark=',', digits=0), ';\\n users: ',formatC(comb_datasets_a_users, format='f', big.mark=',', digits=0),')')
      [2]:  paste0('Normalized & standardized \\nrecords \\n(n = ', formatC(comb_datasets_b_n, format='f', big.mark=',', digits=0), ';\\n users:',formatC(comb_datasets_b_users, format='f', big.mark=',', digits=0),')')
      [3]:  paste0('Only users w/ records at baseline \\n(n = ', formatC(comb_datasets_c_n, format='f', big.mark=',', digits=0), ';\\n users:',formatC(comb_datasets_c_users, format='f', big.mark=',', digits=0),')')
      [4]:  paste0('Users w/ Posterior records \\n(n = ', formatC(comb_datasets_d_n, format='f', big.mark=',', digits=0), ';\\n users:',formatC(comb_datasets_d_users, format='f', big.mark=',', digits=0),')')
      ")

Characteristics of population in Agreement 1 (C1)


We first compared treatments selected in the database to the discarded entries due to duplicated or invalid values.


#Definir la base de datos
library(magrittr)

CONS_C1_df_SEP_2020<-
clean_names(CONS_C1_df) %>% 
    dplyr::filter(!row %in% unlist(CONS_C1_df_dup_JUL_2020$row)) %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #escolaridad 
    dplyr::mutate(escolaridad_rec=ifelse(as.character(escolaridad_ultimo_ano_cursado)=="BASICA COMPLETA"|as.character(escolaridad_ultimo_ano_cursado)=="BASICA INCOMPLETA"|as.character(escolaridad_ultimo_ano_cursado)=="SIN ESTUDIOS","Ed Primaria Completa o Menor",as.character(escolaridad_ultimo_ano_cursado))) %>%
dplyr::mutate(escolaridad_rec=ifelse(escolaridad_rec=="MEDIA COMPLETA"|escolaridad_rec=="MEDIA INCOMPLETA","2-Ed Secundaria Completa o Menor",
    ifelse(escolaridad_rec=="NO SABE O NO SE APLICA",NA,
    ifelse(escolaridad_rec %in% c("TECNICA COMPLETA", "TECNICA INCOMPLETA", "UNIVERSITARIA COMPLETA O MAS", "UNIVERSITARIA INCOMPLETA"), "1-Mayor a Ed Secundaria",ifelse(
      escolaridad_rec=="Ed Primaria Completa o Menor","3-Ed Primaria Completa o Menor",escolaridad_rec))))) %>%
dplyr::mutate(escolaridad_rec=factor(escolaridad_rec,labels=c('1-More than high school','2-Completed high school or less','3-Completed primary school or less')))  %>% 

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frecuencia consumo sus principal 
  
      dplyr::mutate(freq_cons_sus_prin=ifelse(frecuencia_de_consumo_sustancia_principal== "Desconocida",NA,as.character(frecuencia_de_consumo_sustancia_principal))) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #tipo de plan 
      dplyr::mutate(tipo_de_plan_2=dplyr::recode(tipo_de_plan_2,"M-PAI2"= "M-PAI", "M-PR2"="M-PR","PG PAI 2"="PG-PAI", "Otro"="PG-PR", "CALLE"="PG-PR"))%>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #fecha de nacimiento
  dplyr::mutate(fech_nac=lubridate::parse_date_time(stringi::stri_sub(id,-8,-1),"dmY")) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #edad al ingreso
  dplyr::mutate(edad_al_ing=lubridate::time_length(difftime(as.Date(fech_ing), as.Date(fech_nac)),"years")) %>% #AGREGADO EN APR 2020.
  dplyr::mutate(edad_al_ing=replace(edad_al_ing, is.na(edad), NA)) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #sus principal  
  dplyr::mutate(sus_principal= dplyr::recode(sustancia_principal,
                                             "Hipnóticos "= "Tranquilizantes e Hipnóticos",
                                             "Sedantes:  diazepam, Valium, clonazepam, Ravotril, alprazolam, adax, barbitúricos, fenobarbital." = "Tranquilizantes e Hipnóticos",
                                             "Anfetaminas"="Estimulante tipo anfetaminas",
                                             "Extasis"="Estimulante tipo anfetaminas",
                                             "Fenilciclidina"="Estimulante tipo anfetaminas",
                                             "Metanfetaminas y otros derivados"="Estimulante tipo anfetaminas",
                                             "Otros Estimulantes"="Estimulante tipo anfetaminas",
                                             "LSD"="Alucinógenos",
                                             "Otros Alucinógenos"="Alucinógenos",
                                             "Crack"="Pasta Base",
                                             "Heroína"="Opioides",
                                             "Metadona"="Opioides",
                                             "Otros Opioides Analgésicos: morfina, codeína, meperidina,  demerol, tramadol, tramal."="Opioides",
                                             "Inhalables: neopren, GHB, óxido nitroso (gas hilarante), \"poppers\", solventes, gasolina, diluyente"="Inhalables",
                                             "Esteroides Anabólicos"="Otros")) %>%
      dplyr::mutate(sus_principal_mod= dplyr::case_when(sus_principal!="Alcohol"&sus_principal!="Cocaína"&sus_principal!="Marihuana"&sus_principal!="Pasta Base"~"Otros",TRUE~as.character(sus_principal)))%>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #motivo de egreso
      dplyr::mutate(dias_trat_alta_temprana=ifelse(dias_trat>=90,0,1)) %>%
      dplyr::mutate(dias_trat_alta_temprana=as.factor(dias_trat_alta_temprana)) %>%
      dplyr::mutate(dias_trat_alta_temprana= dplyr::recode(dias_trat_alta_temprana, "1"="Menos de 90 días", "0"="Mayor o igual a 90 días")) %>%
      dplyr::mutate(motivodeegreso_mod= ifelse(dias_trat_alta_temprana=="Menos de 90 días" & motivodeegreso=="Abandono", "Abandono Temprano",as.character(motivodeegreso))) %>% 
      dplyr::mutate(motivodeegreso_mod= ifelse(dias_trat_alta_temprana=="Mayor o igual a 90 días" & motivodeegreso=="Abandono", "Abandono Tardio",as.character(motivodeegreso_mod))) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #dias_trat
      dplyr::mutate(fech_ing_num=as.numeric(as.Date(fech_ing)))%>%
      dplyr::mutate(fech_egres_num=as.numeric(as.Date(fech_egres)))%>%
      dplyr::mutate(fech_egres_num=ifelse(is.na(fech_egres),18213,fech_egres_num))%>% #equivalente a 2019-11-13
      dplyr::mutate(dias_treat_imp_sin_na=fech_egres_num-fech_ing_num)%>%
      
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #motivodeegreso_mod_imp
      dplyr::mutate(motivodeegreso_mod_imp=ifelse(dias_treat_imp_sin_na<1095 & is.na(fech_egres),"En curso",as.character(motivodeegreso_mod))) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #origen de ingreso
      dplyr::mutate(origen_ingreso=ifelse(as.character(origen_de_ingreso)=="Consulta Espontánea", "Consulta Espontánea",as.character(origen_de_ingreso))) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Establecimiento Educacional"|origen_ingreso=="Otros"|origen_ingreso=="Trabajo (empresa o empleador)"|origen_ingreso=="Servicios Sociales u otros (iglesia, Mideplan, ser. comunitarios, etc.)", "Otros",origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Juzgado con Competencia en Crimen"|origen_ingreso=="Juzgado de Garantía"|origen_ingreso=="Libertad Vigilada"|origen_ingreso=="Juzgado de Familia"|origen_ingreso=="Juzgado de Policía"|origen_ingreso=="Otros (fiscalía)","Sector Justicia",origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Otros de la Red de Salud General Privado"|origen_ingreso=="Estab. de APS"|origen_ingreso=="Estab. de APS "|origen_ingreso=="Otros de la Red de Salud General Público", "Sector Salud", origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Otro Centro Tratamiento Drogas"|origen_ingreso=="FONODROGAS"|origen_ingreso=="Previene","Otro Centro Tratamiento Drogas/FONODROGAS/Previene",origen_ingreso)) %>%
          
      dplyr::mutate(origen_ingreso_mod= dplyr::case_when(grepl("Otro Centro Tratamiento",as.character(origen_ingreso))~"Derivación Asistida", TRUE~as.character(origen_ingreso)))%>%  
      dplyr::mutate(origen_ingreso_mod=factor(origen_ingreso_mod,labels=c('Spontaneous','Assisted Referral','Other','Justice Sector','Health Sector')))  %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #CIE_10
      dplyr::mutate(across(c(diagnostico_trs_psiquiatrico_cie_10,x2_diagnostico_trs_psiquiatrico_cie_10,x3_diagnostico_trs_psiquiatrico_cie_10),~dplyr::case_when(grepl("En estudio",as.character(.))~0,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~1), .names="dg_{col}"))%>%
      dplyr::mutate(across(c(diagnostico_trs_psiquiatrico_cie_10,x2_diagnostico_trs_psiquiatrico_cie_10,x3_diagnostico_trs_psiquiatrico_cie_10),~dplyr::case_when(grepl("En estudio",as.character(.))~1,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~0), .names="instudy_{col}"))%>%
      dplyr::mutate(instudy_total_cie_10 = base::rowSums(dplyr::select(., instudy_diagnostico_trs_psiquiatrico_cie_10, instudy_x2_diagnostico_trs_psiquiatrico_cie_10, instudy_x3_diagnostico_trs_psiquiatrico_cie_10)))%>%
      dplyr::mutate(dg_total_cie_10 = base::rowSums(dplyr::select(., dg_diagnostico_trs_psiquiatrico_cie_10, dg_x2_diagnostico_trs_psiquiatrico_cie_10, dg_x3_diagnostico_trs_psiquiatrico_cie_10)))%>%
      dplyr::mutate(dg_cie_10_rec=dplyr::case_when(dg_total_cie_10>0 ~2,
                                             instudy_total_cie_10>0 & dg_total_cie_10==0~1,
                                             instudy_total_cie_10>0 & is.na(dg_total_cie_10)~1,
                                             TRUE~0))%>%
      dplyr::mutate(dg_cie_10_rec=as.factor(dg_cie_10_rec))%>%
      dplyr::mutate(dg_cie_10_rec=factor(dg_cie_10_rec,labels=c('Without psychiatric comorbidity','Diagnosis unknown (under study)', 'With psychiatric comorbidity'))) %>% 
      dplyr::mutate(tipo_de_programa_2=dplyr::case_when(grepl("M-", tipo_de_plan_2)~"Women specific",
                                                        grepl("PG-", tipo_de_plan_2)~"General population",
                                                        grepl("LV", tipo_de_plan_2)~"Other")) %>% 
      dplyr::mutate(sexo_2=factor(sexo_2,labels=c('Men','Women'))) %>% 
      dplyr::mutate(edad_al_ing_grupos=dplyr::case_when(
      edad_al_ing>=50~"50+",
          edad_al_ing>=40~"40-49",
          edad_al_ing>=30~"30-39",
          edad_al_ing>=18~"18-29",
          edad_al_ing<18~"<18",
          TRUE~NA_character_)) %>%
      dplyr::mutate(edad_al_ing_grupos=as.factor(edad_al_ing_grupos)) %>%  
    #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Edad Inicio Sus Principal  
   dplyr::mutate(edad_ini_sus_prin_grupos=ifelse(edad_inicio_sustancia_principal>=25,">=25",
                                                ifelse(edad_inicio_sustancia_principal>18,"19-24",
                                                ifelse(edad_inicio_sustancia_principal>15,"16-18",
                                                ifelse(edad_inicio_sustancia_principal>0,"<=15",
                                                NA_character_)))))%>% 
    dplyr::mutate(edad_ini_sus_prin_grupos=as.factor(edad_ini_sus_prin_grupos)) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Cause of discharge
      dplyr::mutate(motivodeegreso_mod_imp=factor(motivodeegreso_mod_imp,labels=c('Late Drop-out',
                                                                                  'Early Drop-out',
                                                                                  'Administrative discharge',
                                                                                  'Therapeutic discharge',
                                                                                  'Referral to another treatment',
                                                                                  'Ongoing treatment'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frequency of consumption
  dplyr::mutate(freq_cons_sus_prin=factor(freq_cons_sus_prin,labels=c("1 day a week or less",
                                                                                  "2 to 3 days a week",
                                                                                  "4 to 6 days a week",
                                                                                  "Less than 1 day a week",
                                                                                  "Did not use",
                                                                                  "Daily"))) %>%     
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #primary substance
    dplyr::mutate(sus_principal_mod=factor(sus_principal_mod,labels=c("Alcohol",
                                                                                  "Cocaine hydrochloride",
                                                                                  "Marijuana",
                                                                                  "Other",
                                                                                  "Cocaine paste"))) %>%   
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #marital status
    dplyr::mutate(estado_conyugal_2=ifelse(as.character(estado_conyugal)=="Casado"|as.character(estado_conyugal)=="Conviviente"|as.character(estado_conyugal)=="conviviente civil", "Casado/Conviviente",stringr::str_trim(as.character(estado_conyugal)))) %>%
    dplyr::mutate(estado_conyugal_2=ifelse(estado_conyugal_2=="Separado"|estado_conyugal_2=="Divorciado"|estado_conyugal_2=="Anulado", "Separado/Divorciado",estado_conyugal_2)) %>% 
    dplyr::mutate(estado_conyugal_2=ifelse(estado_conyugal_2=="Nocontesta",NA,estado_conyugal_2)) %>%
    #dplyr::mutate(estado_conyugal=factor(estado_conyugal, labels=c('asd','asda'))) %>%
    dplyr::mutate(estado_conyugal_2=factor(estado_conyugal_2, labels=c('Married/Shared living arrangements','Separated/Divorced','Single','Widower'))) %>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #region
   dplyr::mutate(macrozona=as.factor(dplyr::recode(region_del_centro,
                                              "DE ANTOFAGASTA"="Norte",
                                              "DE LA ARAUCANIA"="Sur",
                                              "DE ARICA Y PARINACOTA"="Norte",
                                              "DE ATACAMA"="Norte",
                                              "DE AYSEN DEL GENERAL CARLOS IBA?S DEL CAMPO"="Sur",
                                              "DEL BIO-BIO"="Centro",
                                              "DE COQUIMBO"="Norte",
                                              "DE LOS LAGOS"="Sur",
                                              "DE LOS RIOS"="Sur",
                                              "DE MAGALLANES Y LA ANTARTICA CHILENA"="Sur",
                                              "DEL MAULE"="Centro",
                                              "METROPOLITANA"="Centro",
                                              "DE ?BLE"="Centro",
                                              "DEL LIBERTADOR GENERAL  BERNARDO OHIGGINS"="Centro",
                                              "DE TARAPACA"="Norte",
                                              "DE VALPARAISO"="Centro")))%>%
  dplyr::mutate(macrozona=factor(macrozona, labels=c('Center',
                                                                   'North',
                                                                   'South'))) %>%
 dplyr::mutate(nombre_region=as.factor(dplyr::recode(region_del_centro,
                                              "DE ANTOFAGASTA"="Antofagasta (02)",
                                              "DE LA ARAUCANIA"="Araucanía (09)",
                                              "DE ARICA Y PARINACOTA"="Arica (15)",
                                              "DE ATACAMA"="Atacama (03)",
                                              "DE AYSEN DEL GENERAL CARLOS IBA?S DEL CAMPO"="Aysén (11)",
                                              "DEL BIO-BIO"="Biobío (08)",
                                              "DE COQUIMBO"="Coquimbo (04)",
                                              "DE LOS LAGOS"="Los Lagos (10)",
                                              "DE LOS RIOS"="Los Ríos (14)",
                                              "DE MAGALLANES Y LA ANTARTICA CHILENA"="Magallanes (12)",
                                              "DEL MAULE"="Maule (07)",
                                              "METROPOLITANA"="Metropolitana (13)",
                                              "DE ?BLE"="Ñuble (16)",
                                              "DEL LIBERTADOR GENERAL  BERNARDO OHIGGINS"="O'Higgins (06)",
                                              "DE TARAPACA"="Tarapacá (01)",
                                              "DE VALPARAISO"="Valparaíso (05)")))%>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #type of center (pub/priv)
      dplyr::mutate(tipo_centro=factor(tipo_centro, labels=c('Private','Public'))) %>%
      dplyr::mutate(nombre_region=factor(nombre_region)) %>% 
      dplyr::mutate(macrozona=factor(macrozona)) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #occupational status (inactive…)
      dplyr::mutate(estatus_ocupacional=ifelse(condicion_ocupacional=="Trabajando actualmente","Empleado",
                                               ifelse(condicion_ocupacional=="Buscando trabajo por primera vez"|condicion_ocupacional=="Cesante","Desempleado","Inactivo"))) %>%
      dplyr::mutate(estatus_ocupacional=factor(estatus_ocupacional, labels=c('Unemployed',
                                                                             'Employed',
                                                                             'Inactive'))) %>%
      #Comprende la relación entre una persona económicamente activa y su trabajo o empleo. Sólo se aplica aquellas personas que se encuentran trabajando al momento de ingresar a tratamiento (las que respondieron 1 en la pregunta anterior). Para las personas que no están trabajando (estudiantes, cesante, etc.) esta opción estará bloqueada.
      #data.table(table(CONS_C1_df_dup_ENE_2020_prev3$Categoría.Ocupacional))
      dplyr::mutate(cat_ocupacional=ifelse(condicion_ocupacional!="Trabajando actualmente",NA,as.character(categoria_ocupacional)))%>%
      dplyr::mutate(cat_ocupacional=as.factor(cat_ocupacional)) %>%
  dplyr::mutate(cat_ocupacional=factor(cat_ocupacional, labels=c('Salaried',
                                                                             'Self-employed',
                                                                             'Employer',
                                                                             'Unpaid family labour',
                                                                             'Other',
                                                                             'Volunteer worker'))) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #age of onset of drug use
    dplyr::mutate(edad_ini_cons= ifelse(edad_inicio_consumo<=edad_al_ing, edad_inicio_consumo, NA)) %>%
    dplyr::mutate(edad_ini_cons= ifelse(edad_ini_cons<5,NA,edad_ini_cons)) %>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #physical diagnose
    dplyr::mutate(dg_fis_anemia= ifelse(grepl("Anemia",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_card= ifelse(grepl("Cardiopat",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_in_study= ifelse(grepl("En estudio",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_enf_som= ifelse(grepl("somáticas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_ets= ifelse(grepl("ETS",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_alc= ifelse(grepl("Hepatitis alcohólica",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_b= ifelse(grepl("Hepatitis B",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_cro= ifelse(grepl("Hepatitis crónica",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_inf= ifelse(grepl("Infecciosas relacionadas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_otr_cond_fis_ries_vit= ifelse(grepl("Condiciones de Riesgo Vital",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_otr_cond_fis= ifelse(grepl("condiciones Fisicas Limitantes",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_pat_buc= ifelse(grepl("Bucal",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_pat_ges_intrau= ifelse(grepl("niño intrauterino",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_trau_sec= ifelse(grepl("Traumatismos y secuelas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(otros_pr_sm_abu_sex= ifelse(grepl("Abuso Sexual",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
    dplyr::mutate(otros_pr_sm_exp_com_sex= ifelse(grepl("omercial Sexual",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
    dplyr::mutate(otros_pr_sm_otros= ifelse(grepl("Otros",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
    dplyr::mutate(otros_pr_sm_prision= ifelse(grepl("Prisionizaci?",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
    dplyr::mutate(otros_pr_sm_vif= ifelse(grepl("Violencia Intrafamiliar",otros_problemas_de_atencion_de_salud_mental),1,0)) %>% 
    dplyr::mutate(dg_trs_cons_sus_or= dplyr::case_when(grepl("Consumo Perjudicial",diagnostico_trs_consumo_sustancia)~"Hazardous consumption",
                                                    grepl("Dependencia",diagnostico_trs_consumo_sustancia)~"Drug dependence",
                                                    TRUE~NA_character_)) %>%   

    dplyr::select(row, sexo_2, escolaridad_rec, freq_cons_sus_prin, tipo_de_plan_2, tipo_de_programa_2, edad_al_ing_grupos, sus_principal_mod, motivodeegreso_mod_imp, origen_ingreso_mod, dg_cie_10_rec, estado_conyugal_2, edad_ini_cons, cat_ocupacional, estatus_ocupacional, macrozona, nombre_region, tipo_centro,dias_treat_imp_sin_na, dg_fis_anemia, dg_fis_card, dg_fis_in_study, dg_fis_enf_som, dg_fis_ets, dg_fis_hep_alc, dg_fis_hep_b, dg_fis_hep_cro, dg_fis_inf, dg_fis_otr_cond_fis, dg_fis_otr_cond_fis_ries_vit, dg_fis_pat_buc, dg_fis_pat_ges_intrau, dg_fis_trau_sec, otros_pr_sm_abu_sex, otros_pr_sm_exp_com_sex, otros_pr_sm_otros, otros_pr_sm_prision, otros_pr_sm_vif,dg_trs_cons_sus_or,edad_ini_sus_prin_grupos)


label(CONS_C1_df_SEP_2020$sexo_2) <- "Sex"
label(CONS_C1_df_SEP_2020$escolaridad_rec) <- "Educational Attainment"
label(CONS_C1_df_SEP_2020$freq_cons_sus_prin) <- "Frequency of use of primary drug at admission"

label(CONS_C1_df_SEP_2020$tipo_de_plan_2) <- "Treatment Plan"
label(CONS_C1_df_SEP_2020$tipo_de_programa_2) <- "Treatment Program"
label(CONS_C1_df_SEP_2020$edad_al_ing_grupos) <- "Age at Admission"
label(CONS_C1_df_SEP_2020$sus_principal_mod) <- "Primary substance at admission"
label(CONS_C1_df_SEP_2020$motivodeegreso_mod_imp) <- "Motive of discharge"
label(CONS_C1_df_SEP_2020$origen_ingreso_mod) <- "Motive of Admission to Treatment (First Entry)"
label(CONS_C1_df_SEP_2020$dg_cie_10_rec) <- "Psychiatric comorbidity"


label(CONS_C1_df_SEP_2020$estado_conyugal_2) <- "Marital Status"
label(CONS_C1_df_SEP_2020$edad_ini_cons) <- "Age of Onset of Drug Use"
label(CONS_C1_df_SEP_2020$cat_ocupacional) <- "Occupational Category"
label(CONS_C1_df_SEP_2020$estatus_ocupacional) <- "Occupational Status"
label(CONS_C1_df_SEP_2020$macrozona) <- "Macrozones of the Center"
label(CONS_C1_df_SEP_2020$nombre_region) <- "Chilean Region of the Center"
label(CONS_C1_df_SEP_2020$tipo_centro) <- "Type of Center"
label(CONS_C1_df_SEP_2020$dias_treat_imp_sin_na) <- "Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13)"

label(CONS_C1_df_SEP_2020$dg_fis_anemia) <- "Physical Dg. Anemia"
label(CONS_C1_df_SEP_2020$dg_fis_card) <- "Physical Dg. Heart Disease"
label(CONS_C1_df_SEP_2020$dg_fis_in_study) <- "Physical Dg. Under Study"
label(CONS_C1_df_SEP_2020$dg_fis_enf_som) <- "Physical Dg. Somatic illnesses"
label(CONS_C1_df_SEP_2020$dg_fis_ets) <- "Physical Dg. STDs"
label(CONS_C1_df_SEP_2020$dg_fis_hep_alc) <- "Physical Dg. Alcoholic hepatitis"
label(CONS_C1_df_SEP_2020$dg_fis_hep_b) <- "Physical Dg. Hepatitis B"
label(CONS_C1_df_SEP_2020$dg_fis_hep_cro) <- "Physical Dg. Chronic hepatitis"
label(CONS_C1_df_SEP_2020$dg_fis_inf) <- "Physical Dg. Infectuous diseases"
label(CONS_C1_df_SEP_2020$dg_fis_otr_cond_fis_ries_vit) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_SEP_2020$dg_fis_otr_cond_fis) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_SEP_2020$dg_fis_pat_buc) <- "Physical Dg. Oral-care pathologies"
label(CONS_C1_df_SEP_2020$dg_fis_pat_ges_intrau) <- "Physical Dg. Development or Intrauterine"
label(CONS_C1_df_SEP_2020$dg_fis_trau_sec) <- "Traumatisms and disabling sequelae"

label(CONS_C1_df_SEP_2020$otros_pr_sm_abu_sex) <- "Other mental health problems- Sexual abuse"
label(CONS_C1_df_SEP_2020$otros_pr_sm_exp_com_sex) <- "Other mental health problems- Commercial sexual exploitation"
label(CONS_C1_df_SEP_2020$otros_pr_sm_otros) <- "Other mental health problems- Other"
label(CONS_C1_df_SEP_2020$otros_pr_sm_prision) <- "Other mental health problems- Inprisonment"
label(CONS_C1_df_SEP_2020$otros_pr_sm_vif) <- "Other mental health problems- Domestic violence"

label(CONS_C1_df_SEP_2020$dg_trs_cons_sus_or) <- "Diagnosis of substance use"

label(CONS_C1_df_SEP_2020$edad_ini_sus_prin_grupos) <- "Age of Onset of Drug Use Primary/Main Substance"

#   sexo_2 tipo_de_programa_2 tipo_de_plan_2 escolaridad_ultimo_ano_cursado
# diagnostico_trs_psiquiatrico_cie_10 x2_diagnostico_trs_psiquiatrico_cie_10 x3_diagnostico_trs_psiquiatrico_cie_10 motivodeegreso 

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#CONS_C1_df_SEP_2020%>% janitor::tabyl(dg_cie_10_rec)
#CONS_C1_df_dup_JUL_2020%>% janitor::tabyl(tipo_de_programa_2)
#CONS_C1_df_dup_JUL_2020%>% janitor::tabyl(tipo_de_programa_2)

CONS_C1_df_dup_JUL_2020_explore<-
    CONS_C1_df_dup_JUL_2020%>% 
    #dplyr::mutate(treat= ifelse(row %in% unlist(CONS_C1_df_dup_JUL_2020_match_top_sel$row),1,0))%>%
      dplyr::mutate(origen_ingreso_mod= dplyr::case_when(grepl("Otro Centro Tratamiento",as.character(origen_ingreso_mod))~"Derivación Asistida", TRUE~as.character(origen_ingreso_mod)))%>%  
      dplyr::mutate(origen_ingreso_mod=factor(origen_ingreso_mod,labels=c('Spontaneous','Assisted Referral','Other','Justice Sector','Health Sector')))  %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #CIE_10  
      dplyr::mutate(across(c(dg_trs_psiq_cie_10_or,x2_dg_trs_psiq_cie_10_or,x3_dg_trs_psiq_cie_10_or,x4_dg_trs_psiq_cie_10_or,x5_dg_trs_psiq_cie_10_or),~dplyr::case_when(grepl("En estudio",as.character(.))~0,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~1), .names="dg_{col}"))%>%
      dplyr::mutate(dg_total_cie_10 = base::rowSums(dplyr::select(., dg_dg_trs_psiq_cie_10_or, dg_x2_dg_trs_psiq_cie_10_or, dg_x3_dg_trs_psiq_cie_10_or, dg_x4_dg_trs_psiq_cie_10_or, dg_x5_dg_trs_psiq_cie_10_or)))%>%  
      dplyr::mutate(across(c(dg_trs_psiq_cie_10_or,x2_dg_trs_psiq_cie_10_or,x3_dg_trs_psiq_cie_10_or,x4_dg_trs_psiq_cie_10_or,x5_dg_trs_psiq_cie_10_or),~dplyr::case_when(grepl("En estudio",as.character(.))~1,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~0), .names="instudy_{col}"))%>%
      dplyr::mutate(instudy_total_cie_10 = base::rowSums(dplyr::select(., instudy_dg_trs_psiq_cie_10_or, instudy_x2_dg_trs_psiq_cie_10_or, instudy_x3_dg_trs_psiq_cie_10_or, instudy_x4_dg_trs_psiq_cie_10_or, instudy_x5_dg_trs_psiq_cie_10_or)))%>%
      dplyr::mutate(dg_cie_10_rec=dplyr::case_when(dg_total_cie_10>0 ~2,
                                             instudy_total_cie_10>0 & dg_total_cie_10==0~1,
                                             instudy_total_cie_10>0 & is.na(dg_total_cie_10)~1,
                                             TRUE~0))%>%
      dplyr::mutate(dg_cie_10_rec=as.factor(dg_cie_10_rec))%>%
      dplyr::mutate(dg_cie_10_rec=factor(dg_cie_10_rec,labels=c('Without psychiatric comorbidity','Diagnosis unknown (under study)', 'With psychiatric comorbidity'))) %>% 
      dplyr::mutate(macrozona=factor(macrozona, labels=c('Center',
                                                                   'North',
                                                                   'South'))) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Edad grupos  
        dplyr::mutate(edad_al_ing_grupos=dplyr::case_when(
      edad_al_ing>=50~"50+",
          edad_al_ing>=40~"40-49",
          edad_al_ing>=30~"30-39",
          edad_al_ing>=18~"18-29",
          edad_al_ing<18~"<18",
          TRUE~NA_character_)) %>%
  dplyr::mutate(edad_al_ing_grupos=as.factor(edad_al_ing_grupos)) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Tipo de programa
          dplyr::mutate(tipo_de_programa_2=dplyr::case_when(grepl("M-", tipo_de_plan_2)~"Women specific",
                                                        grepl("PG-", tipo_de_plan_2)~"General population",
                                                        grepl("LV", tipo_de_plan_2)~"Other")) %>% 
          dplyr::mutate(tipo_de_programa_2=as.factor(tipo_de_programa_2)) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Sex
      dplyr::mutate(sexo_2=factor(sexo_2,labels=c('Men','Women'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Cause of Discharge
  dplyr::mutate(motivodeegreso_mod_imp=factor(motivodeegreso_mod_imp,labels=c('Late Drop-out',
                                                                                  'Early Drop-out',
                                                                                  'Administrative discharge',
                                                                                  'Therapeutic discharge',
                                                                                  'Referral to another treatment',
                                                                                  'Ongoing treatment',
                                                                              'Death'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frequency of consumption
  dplyr::mutate(freq_cons_sus_prin=factor(freq_cons_sus_prin,labels=c("1 day a week or less",
                                                                                  "2 to 3 days a week",
                                                                                  "4 to 6 days a week",
                                                                                  "Less than 1 day a week",
                                                                                  "Did not use",
                                                                                  "Daily"))) %>%     
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #primary substance
    dplyr::mutate(sus_principal_mod=factor(sus_principal_mod,labels=c("Alcohol",
                                                                                  "Cocaine hydrochloride",
                                                                                  "Marijuana",
                                                                                  "Other",
                                                                                  "Cocaine paste"))) %>%   
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #escolaridad rec
    dplyr::mutate(escolaridad_rec=factor(escolaridad_rec,labels=c('1-More than high school','2-Completed high school or less','3-Completed primary school or less')))  %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #marital status
      dplyr::mutate(estado_conyugal_2=factor(estado_conyugal_2, labels=c('Married/Shared living arrangements','Separated/Divorced','Single','Widower'))) %>%  
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #type of center (pub/priv)
  dplyr::mutate(tipo_centro=factor(tipo_centro, labels=c('Private','Public'))) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #occupational status (inactive…)
  dplyr::mutate(estatus_ocupacional=factor(estatus_ocupacional, labels=c('Unemployed',
                                                                           'Employed',
                                                                           'Inactive'))) %>%
  dplyr::mutate(cat_ocupacional=factor(cat_ocupacional, labels=c('Salaried',
                                                                   'Self-employed',
                                                                   'Employer',
                                                                   'Unpaid family labour',
                                                                   'Other',
                                                                   'Volunteer worker'))) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #age of onset of drug use
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #physical diagnose

    dplyr::mutate(dg_fis_anemia= ifelse(grepl("Anemia",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_card= ifelse(grepl("Cardiopat",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_in_study= ifelse(grepl("En estudio",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_enf_som= ifelse(grepl("somáticas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_ets= ifelse(grepl("ETS",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_alc= ifelse(grepl("Hepatitis alcohólica",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_b= ifelse(grepl("Hepatitis B",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_hep_cro= ifelse(grepl("Hepatitis crónica",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_inf= ifelse(grepl("Infecciosas relacionadas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_otr_cond_fis_ries_vit= ifelse(grepl("Condiciones de Riesgo Vital",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_otr_cond_fis= ifelse(grepl("condiciones Fisicas Limitantes",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_pat_buc= ifelse(grepl("Bucal",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_pat_ges_intrau= ifelse(grepl("niño intrauterino",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(dg_fis_trau_sec= ifelse(grepl("Traumatismos y secuelas",diagnostico_trs_fisico),1,0))%>%
    dplyr::mutate(otros_pr_sm_abu_sex= ifelse(grepl("Abuso Sexual",otros_probl_at_sm_or),1,0))%>%
    dplyr::mutate(otros_pr_sm_exp_com_sex= ifelse(grepl("omercial Sexual",otros_probl_at_sm_or),1,0))%>%
    dplyr::mutate(otros_pr_sm_otros= ifelse(grepl("Otros",otros_probl_at_sm_or),1,0))%>%
    dplyr::mutate(otros_pr_sm_prision= ifelse(grepl("Prisionalización",otros_probl_at_sm_or),1,0))%>%
    dplyr::mutate(otros_pr_sm_vif= ifelse(grepl("Violencia Intrafamiliar",otros_probl_at_sm_or),1,0)) %>% 
 
    dplyr::mutate(dg_trs_cons_sus_or= dplyr::case_when(grepl("Consumo Perjudicial",dg_trs_cons_sus_or)~"Hazardous consumption",
                                                   grepl("Dependencia",dg_trs_cons_sus_or)~"Drug dependence",
                                                   TRUE~NA_character_)) %>%   
#CONS_C1_df_dup_JUL_2020_explore%>% janitor::tabyl(sus_principal_mod)

  dplyr::select(row, sexo_2, escolaridad_rec, freq_cons_sus_prin, tipo_de_plan_2, tipo_de_programa_2, edad_al_ing_grupos, sus_principal_mod, motivodeegreso_mod_imp, origen_ingreso_mod, dg_cie_10_rec, estado_conyugal_2, edad_ini_cons, cat_ocupacional, estatus_ocupacional, macrozona, nombre_region, tipo_centro,dias_treat_imp_sin_na, dg_fis_anemia, dg_fis_card, dg_fis_in_study, dg_fis_enf_som, dg_fis_ets, dg_fis_hep_alc, dg_fis_hep_b, dg_fis_hep_cro, dg_fis_inf, dg_fis_otr_cond_fis_ries_vit, dg_fis_otr_cond_fis, dg_fis_pat_buc, dg_fis_pat_ges_intrau, dg_fis_trau_sec, otros_pr_sm_abu_sex, otros_pr_sm_exp_com_sex, otros_pr_sm_otros, otros_pr_sm_prision, otros_pr_sm_vif, dg_trs_cons_sus_or,edad_ini_sus_prin_grupos)
  
 #Frecuencia de consumo de la droga principal en los 30 días previos a la admisión a tratamiento

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

label(CONS_C1_df_dup_JUL_2020_explore$sexo_2) <- "Sex"
label(CONS_C1_df_dup_JUL_2020_explore$escolaridad_rec) <- "Educational Attainment"
label(CONS_C1_df_dup_JUL_2020_explore$freq_cons_sus_prin) <- "Frequency of use of primary drug at admission"

label(CONS_C1_df_dup_JUL_2020_explore$tipo_de_plan_2) <- "Treatment Plan"
label(CONS_C1_df_dup_JUL_2020_explore$tipo_de_programa_2) <- "Treatment Program"
label(CONS_C1_df_dup_JUL_2020_explore$edad_al_ing_grupos) <- "Age at Admission"
label(CONS_C1_df_dup_JUL_2020_explore$sus_principal_mod) <- "Primary substance at admission"
label(CONS_C1_df_dup_JUL_2020_explore$motivodeegreso_mod_imp) <- "Motive of discharge"
label(CONS_C1_df_dup_JUL_2020_explore$origen_ingreso_mod) <- "Motive of Admission to Treatment (First Entry)"
label(CONS_C1_df_dup_JUL_2020_explore$dg_cie_10_rec) <- "Psychiatric comorbidity"

label(CONS_C1_df_dup_JUL_2020_explore$estado_conyugal_2) <- "Marital Status"
label(CONS_C1_df_dup_JUL_2020_explore$edad_ini_cons) <- "Age of Onset of Drug Use"
label(CONS_C1_df_dup_JUL_2020_explore$cat_ocupacional) <- "Occupational Category"
label(CONS_C1_df_dup_JUL_2020_explore$estatus_ocupacional) <- "Occupational Status"
label(CONS_C1_df_dup_JUL_2020_explore$macrozona) <- "Macrozones of the Center"
label(CONS_C1_df_dup_JUL_2020_explore$nombre_region) <- "Chilean Region of the Center"
label(CONS_C1_df_dup_JUL_2020_explore$tipo_centro) <- "Type of Center"
label(CONS_C1_df_dup_JUL_2020_explore$dias_treat_imp_sin_na) <- "Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13)"

label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_anemia) <- "Physical Dg. Anemia"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_card) <- "Physical Dg. Heart Disease"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_in_study) <- "Physical Dg. Under Study"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_enf_som) <- "Physical Dg. Somatic illnesses"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_ets) <- "Physical Dg. STDs"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_hep_alc) <- "Physical Dg. Alcoholic hepatitis"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_hep_b) <- "Physical Dg. Hepatitis B"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_hep_cro) <- "Physical Dg. Chronic hepatitis"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_inf) <- "Physical Dg. Infectuous diseases"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_otr_cond_fis_ries_vit) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_otr_cond_fis) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_pat_buc) <- "Physical Dg. Oral-care pathologies"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_pat_ges_intrau) <- "Physical Dg. Development or Intrauterine"
label(CONS_C1_df_dup_JUL_2020_explore$dg_fis_trau_sec) <- "Traumatisms and disabling sequelae"

label(CONS_C1_df_dup_JUL_2020_explore$otros_pr_sm_abu_sex) <- "Other mental health problems- Sexual abuse"
label(CONS_C1_df_dup_JUL_2020_explore$otros_pr_sm_exp_com_sex) <- "Other mental health problems- Commercial sexual exploitation"
label(CONS_C1_df_dup_JUL_2020_explore$otros_pr_sm_otros) <- "Other mental health problems- Other"
label(CONS_C1_df_dup_JUL_2020_explore$otros_pr_sm_prision) <- "Other mental health problems- Inprisonment"
label(CONS_C1_df_dup_JUL_2020_explore$otros_pr_sm_vif) <- "Other mental health problems- Domestic violence"

label(CONS_C1_df_dup_JUL_2020_explore$dg_trs_cons_sus_or) <- "Diagnosis of substance use"

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
library(compareGroups)
Warning: package 'compareGroups' was built under R version 4.0.5
CONS_C1_df_SEP_2020_miss<-rbind(CONS_C1_df_dup_JUL_2020_explore,CONS_C1_df_SEP_2020) %>% 
  dplyr::mutate(selected=ifelse(row %in% unlist(CONS_C1_df_dup_JUL_2020$row),1,0))

knitr::opts_chunk$set(echo = FALSE, warning=FALSE, message=FALSE)

label(CONS_C1_df_SEP_2020_miss$sexo_2) <- "Sex"
label(CONS_C1_df_SEP_2020_miss$escolaridad_rec) <- "Educational Attainment"
label(CONS_C1_df_SEP_2020_miss$freq_cons_sus_prin) <- "Frequency of use of primary drug at admission"

label(CONS_C1_df_SEP_2020_miss$tipo_de_plan_2) <- "Treatment Plan"
label(CONS_C1_df_SEP_2020_miss$tipo_de_programa_2) <- "Treatment Program"
label(CONS_C1_df_SEP_2020_miss$edad_al_ing_grupos) <- "Age at Admission"
label(CONS_C1_df_SEP_2020_miss$sus_principal_mod) <- "Primary substance at admission"
label(CONS_C1_df_SEP_2020_miss$motivodeegreso_mod_imp) <- "Motive of discharge"
label(CONS_C1_df_SEP_2020_miss$origen_ingreso_mod) <- "Motive of Admission to Treatment (First Entry)"
label(CONS_C1_df_SEP_2020_miss$dg_cie_10_rec) <- "Psychiatric comorbidity"

label(CONS_C1_df_SEP_2020_miss$estado_conyugal_2) <- "Marital Status"
label(CONS_C1_df_SEP_2020_miss$edad_ini_cons) <- "Age of Onset of Drug Use"
label(CONS_C1_df_SEP_2020_miss$cat_ocupacional) <- "Occupational Category"
label(CONS_C1_df_SEP_2020_miss$estatus_ocupacional) <- "Occupational Status"
label(CONS_C1_df_SEP_2020_miss$macrozona) <- "Macrozones of the Center"
label(CONS_C1_df_SEP_2020_miss$nombre_region) <- "Chilean Region of the Center"
label(CONS_C1_df_SEP_2020_miss$tipo_centro) <- "Type of Center"
label(CONS_C1_df_SEP_2020_miss$dias_treat_imp_sin_na) <- "Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13)"

label(CONS_C1_df_SEP_2020_miss$dg_fis_anemia) <- "Physical Dg. Anemia"
label(CONS_C1_df_SEP_2020_miss$dg_fis_card) <- "Physical Dg. Heart Disease"
label(CONS_C1_df_SEP_2020_miss$dg_fis_in_study) <- "Physical Dg. Under Study"
label(CONS_C1_df_SEP_2020_miss$dg_fis_enf_som) <- "Physical Dg. Somatic illnesses"
label(CONS_C1_df_SEP_2020_miss$dg_fis_ets) <- "Physical Dg. STDs"
label(CONS_C1_df_SEP_2020_miss$dg_fis_hep_alc) <- "Physical Dg. Alcoholic hepatitis"
label(CONS_C1_df_SEP_2020_miss$dg_fis_hep_b) <- "Physical Dg. Hepatitis B"
label(CONS_C1_df_SEP_2020_miss$dg_fis_hep_cro) <- "Physical Dg. Chronic hepatitis"
label(CONS_C1_df_SEP_2020_miss$dg_fis_inf) <- "Physical Dg. Infectuous diseases"
label(CONS_C1_df_SEP_2020_miss$dg_fis_otr_cond_fis_ries_vit) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_SEP_2020_miss$dg_fis_otr_cond_fis) <- "Physical Dg. Other phsysical conditions"
label(CONS_C1_df_SEP_2020_miss$dg_fis_pat_buc) <- "Physical Dg. Oral-care pathologies"
label(CONS_C1_df_SEP_2020_miss$dg_fis_pat_ges_intrau) <- "Physical Dg. Development or Intrauterine"
label(CONS_C1_df_SEP_2020_miss$dg_fis_trau_sec) <- "Traumatisms and disabling sequelae"

label(CONS_C1_df_SEP_2020_miss$otros_pr_sm_abu_sex) <- "Other mental health problems- Sexual abuse"
label(CONS_C1_df_SEP_2020_miss$otros_pr_sm_exp_com_sex) <- "Other mental health problems- Commercial sexual exploitation"
label(CONS_C1_df_SEP_2020_miss$otros_pr_sm_otros) <- "Other mental health problems- Other"
label(CONS_C1_df_SEP_2020_miss$otros_pr_sm_prision) <- "Other mental health problems- Inprisonment"
label(CONS_C1_df_SEP_2020_miss$otros_pr_sm_vif) <- "Other mental health problems- Domestic violence"

label(CONS_C1_df_SEP_2020_miss$dg_trs_cons_sus_or) <- "Diagnosis of substance use"
label(CONS_C1_df_SEP_2020_miss$edad_ini_sus_prin_grupos) <- "Age of Onset of Drug Use Primary/Main Substance"

table <- suppressWarnings(compareGroups(selected ~ sexo_2+ escolaridad_rec+ freq_cons_sus_prin+ tipo_de_plan_2+ tipo_de_programa_2+ 
        edad_al_ing_grupos+  sus_principal_mod+  motivodeegreso_mod_imp+  origen_ingreso_mod+  dg_cie_10_rec+  estado_conyugal_2+  edad_ini_cons+ edad_ini_sus_prin_grupos+ cat_ocupacional+  estatus_ocupacional+  macrozona+  nombre_region+  tipo_centro+ dias_treat_imp_sin_na+ dg_fis_anemia+ dg_fis_card+ dg_fis_in_study+ dg_fis_enf_som+ dg_fis_ets+ dg_fis_hep_alc+ dg_fis_hep_b+ dg_fis_hep_cro+ dg_fis_inf+ dg_fis_otr_cond_fis_ries_vit+ dg_fis_otr_cond_fis+ dg_fis_pat_buc+ dg_fis_pat_ges_intrau+ dg_fis_trau_sec+ otros_pr_sm_abu_sex+ otros_pr_sm_exp_com_sex+ otros_pr_sm_otros+ otros_pr_sm_prision+ otros_pr_sm_vif+ dg_trs_cons_sus_or,
                       method= c(sexo_2=3,
                                      escolaridad_rec=3,
                                      freq_cons_sus_prin=3,
                                      tipo_de_plan_2=3,
                                      tipo_de_programa_2=3,
                                      edad_al_ing_grupos=3,
                                      sus_principal_mod=3,
                                      motivodeegreso_mod_imp=3,
                                      origen_ingreso_mod=3,
                                      dg_cie_10_rec=3,
                                      estado_conyugal_2=3,
                                      edad_ini_cons=2,
                                      edad_ini_sus_prin_grupos=3,
                                      cat_ocupacional=3,
                                      estatus_ocupacional=3,
                                      macrozona=3,
                                      nombre_region=3,
                                      tipo_centro=3,
                                      dias_treat_imp_sin_na=2,
                                      dg_fis_anemia= 3,
                                      dg_fis_card= 3,
                                      dg_fis_in_study= 3, 
                                      dg_fis_enf_som= 3, 
                                      dg_fis_ets= 3, 
                                      dg_fis_hep_alc= 3, 
                                      dg_fis_hep_b= 3, 
                                      dg_fis_hep_cro= 3, 
                                      dg_fis_inf= 3, 
                                      dg_fis_otr_cond_fis_ries_vit= 3,
                                      dg_fis_otr_cond_fis= 3,
                                      dg_fis_pat_buc= 3, 
                                      dg_fis_pat_ges_intrau= 3, 
                                      dg_fis_trau_sec= 3, 
                                      otros_pr_sm_abu_sex= 3, 
                                      otros_pr_sm_exp_com_sex= 3, 
                                      otros_pr_sm_otros= 3, 
                                      otros_pr_sm_prision= 3, 
                                      otros_pr_sm_vif=3,
                                      dg_trs_cons_sus_or=3),
                       data = CONS_C1_df_SEP_2020_miss,
                       include.miss = T,
                       var.equal=T
                       ))
 #Possible values are: 1 - forces analysis as "normal-distributed"; 2 - forces analysis as "continuous non-normal"; 3 - forces analysis as "categorical"; and 4 - NA, which performs a Shapiro-Wilks test to decide between normal or non-normal. 

pvals <- suppressWarnings(getResults(table))
#p.adjust(pvals, method = "BH")
restab <- suppressWarnings(createTable(table, show.p.overall = F))
 suppressWarnings(export2md(restab, size=9, first.strip=T, hide.no="no", position="center",
                            col.names=c("Variables","Discarded Entries", "Selected Entries"),
           format="html",caption= "Table 1. Summary descriptives table by groups, between selected rows (=1) and records that did not matched, duplicate treatments and invalid ones (=0)"))%>%
  kableExtra::add_footnote(c("Note. Variables of C1 dataset had to be standardized before comparison;", "Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;", "Categorical variables are presented as number (%)"), notation = "none")%>%
  kableExtra::scroll_box(width = "100%", height = "375px")
Table 1. Summary descriptives table by groups, between selected rows (=1) and records that did not matched, duplicate treatments and invalid ones (=0)
Variables Discarded Entries Selected Entries
N=53390 N=109756
Sex:
Men 36856 (69.0%) 80305 (73.2%)
Women 16534 (31.0%) 29451 (26.8%)
Educational Attainment:
1-More than high school 10355 (19.4%) 18659 (17.0%)
2-Completed high school or less 28766 (53.9%) 60852 (55.4%)
3-Completed primary school or less 14022 (26.3%) 29808 (27.2%)
‘Missing’ 247 (0.46%) 437 (0.40%)
Frequency of use of primary drug at admission:
1 day a week or less 3530 (6.61%) 6995 (6.37%)
2 to 3 days a week 14288 (26.8%) 28888 (26.3%)
4 to 6 days a week 8512 (15.9%) 17567 (16.0%)
Less than 1 day a week 2070 (3.88%) 3899 (3.55%)
Did not use 898 (1.68%) 1848 (1.68%)
Daily 23770 (44.5%) 49991 (45.5%)
‘Missing’ 322 (0.60%) 568 (0.52%)
Treatment Plan:
M-PAB 39 (0.07%) 76 (0.07%)
M-PAI 4011 (7.51%) 6622 (6.03%)
M-PR 2280 (4.27%) 5522 (5.03%)
PG-PAB 18276 (34.2%) 38036 (34.7%)
PG-PAI 23266 (43.6%) 45319 (41.3%)
PG-PR 5516 (10.3%) 14084 (12.8%)
PAI LV 1 (0.00%) 0 (0.00%)
‘Missing’ 1 (0.00%) 97 (0.09%)
Treatment Program:
General population 47058 (88.1%) 97439 (88.8%)
Women specific 6330 (11.9%) 12220 (11.1%)
Other 1 (0.00%) 0 (0.00%)
‘Missing’ 1 (0.00%) 97 (0.09%)
Age at Admission:
<18 357 (0.67%) 164 (0.15%)
18-29 15145 (28.4%) 36723 (33.5%)
30-39 17612 (33.0%) 37391 (34.1%)
40-49 12194 (22.8%) 22564 (20.6%)
50+ 8082 (15.1%) 12898 (11.8%)
‘Missing’ 0 (0.00%) 16 (0.01%)
Primary substance at admission:
Alcohol 19459 (36.4%) 34834 (31.7%)
Cocaine hydrochloride 9013 (16.9%) 20067 (18.3%)
Marijuana 2917 (5.46%) 6548 (5.97%)
Other 1357 (2.54%) 1926 (1.75%)
Cocaine paste 20643 (38.7%) 46380 (42.3%)
‘Missing’ 1 (0.00%) 1 (0.00%)
Motive of discharge:
Late Drop-out 13982 (26.2%) 37956 (34.6%)
Early Drop-out 1087 (2.04%) 17950 (16.4%)
Administrative discharge 3553 (6.65%) 9778 (8.91%)
Therapeutic discharge 19441 (36.4%) 24371 (22.2%)
Referral to another treatment 13446 (25.2%) 11834 (10.8%)
Ongoing treatment 1393 (2.61%) 7846 (7.15%)
Death 0 (0.00%) 1 (0.00%)
‘Missing’ 488 (0.91%) 20 (0.02%)
Motive of Admission to Treatment (First Entry):
Spontaneous 22296 (41.8%) 50260 (45.8%)
Assisted Referral 6986 (13.1%) 11965 (10.9%)
Other 2768 (5.18%) 5483 (5.00%)
Justice Sector 4801 (8.99%) 9762 (8.89%)
Health Sector 16539 (31.0%) 32286 (29.4%)
Psychiatric comorbidity:
Without psychiatric comorbidity 22066 (41.3%) 39695 (36.2%)
Diagnosis unknown (under study) 2275 (4.26%) 20723 (18.9%)
With psychiatric comorbidity 29049 (54.4%) 49338 (45.0%)
Marital Status:
Married/Shared living arrangements 18199 (34.1%) 36362 (33.1%)
Separated/Divorced 6440 (12.1%) 11923 (10.9%)
Single 27880 (52.2%) 60008 (54.7%)
Widower 746 (1.40%) 1265 (1.15%)
‘Missing’ 125 (0.23%) 198 (0.18%)
Age of Onset of Drug Use 15.0 [14.0;18.0] 15.0 [14.0;18.0]
Age of Onset of Drug Use Primary/Main Substance:
<=15 14801 (27.7%) 30295 (27.6%)
>=25 13297 (24.9%) 24585 (22.4%)
16-18 12963 (24.3%) 28687 (26.1%)
19-24 12314 (23.1%) 25184 (22.9%)
‘Missing’ 15 (0.03%) 1005 (0.92%)
Occupational Category:
Salaried 14476 (27.1%) 30754 (28.0%)
Self-employed 6423 (12.0%) 13787 (12.6%)
Employer 822 (1.54%) 1586 (1.45%)
Unpaid family labour 107 (0.20%) 233 (0.21%)
Other 507 (0.95%) 1039 (0.95%)
Volunteer worker 115 (0.22%) 205 (0.19%)
‘Missing’ 30940 (58.0%) 62152 (56.6%)
Occupational Status:
Unemployed 18428 (34.5%) 39711 (36.2%)
Employed 24237 (45.4%) 50638 (46.1%)
Inactive 10724 (20.1%) 19406 (17.7%)
‘Missing’ 1 (0.00%) 1 (0.00%)
Macrozones of the Center:
Center 39516 (74.0%) 82919 (75.5%)
North 7450 (14.0%) 16301 (14.9%)
South 6413 (12.0%) 10508 (9.57%)
‘Missing’ 11 (0.02%) 28 (0.03%)
Type of Center:
Private 16206 (30.4%) 33322 (30.4%)
Public 37173 (69.6%) 76406 (69.6%)
‘Missing’ 11 (0.02%) 28 (0.03%)
Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 309 [177;483] 164 [90.0;290]
Physical Dg. Anemia:
0 53344 (99.9%) 109650 (99.9%)
1 46 (0.09%) 106 (0.10%)
Physical Dg. Heart Disease:
0 52772 (98.8%) 108644 (99.0%)
1 618 (1.16%) 1112 (1.01%)
Physical Dg. Under Study:
0 24864 (46.6%) 50839 (46.3%)
1 28526 (53.4%) 58917 (53.7%)
Physical Dg. Somatic illnesses:
0 52914 (99.1%) 108970 (99.3%)
1 476 (0.89%) 786 (0.72%)
Physical Dg. STDs:
0 53067 (99.4%) 109045 (99.4%)
1 323 (0.60%) 711 (0.65%)
Physical Dg. Alcoholic hepatitis:
0 53195 (99.6%) 109428 (99.7%)
1 195 (0.37%) 328 (0.30%)
Physical Dg. Hepatitis B:
0 53320 (99.9%) 109638 (99.9%)
1 70 (0.13%) 118 (0.11%)
Physical Dg. Chronic hepatitis:
0 53319 (99.9%) 109629 (99.9%)
1 71 (0.13%) 127 (0.12%)
Physical Dg. Infectuous diseases:
0 53314 (99.9%) 109598 (99.9%)
1 76 (0.14%) 158 (0.14%)
Physical Dg. Other phsysical conditions:
0 52609 (98.5%) 108371 (98.7%)
1 781 (1.46%) 1385 (1.26%)
Physical Dg. Other phsysical conditions:
0 51715 (96.9%) 106800 (97.3%)
1 1675 (3.14%) 2956 (2.69%)
Physical Dg. Oral-care pathologies:
0 52696 (98.7%) 108205 (98.6%)
1 694 (1.30%) 1551 (1.41%)
Physical Dg. Development or Intrauterine:
0 53353 (99.9%) 109672 (99.9%)
1 37 (0.07%) 84 (0.08%)
Traumatisms and disabling sequelae:
0 52543 (98.4%) 107938 (98.3%)
1 847 (1.59%) 1818 (1.66%)
Other mental health problems- Sexual abuse:
0 52309 (98.0%) 107666 (98.1%)
1 1081 (2.02%) 2090 (1.90%)
Other mental health problems- Commercial sexual exploitation:
0 53089 (99.4%) 109066 (99.4%)
1 301 (0.56%) 690 (0.63%)
Other mental health problems- Other:
0 46071 (86.3%) 93865 (85.5%)
1 7319 (13.7%) 15891 (14.5%)
Other mental health problems- Inprisonment:
0 53390 (100%) 109755 (100.0%)
1 0 (0.00%) 1 (0.00%)
Other mental health problems- Domestic violence:
0 40123 (75.2%) 81888 (74.6%)
1 13267 (24.8%) 27868 (25.4%)
Diagnosis of substance use:
Drug dependence 39220 (73.5%) 81855 (74.6%)
Hazardous consumption 14169 (26.5%) 27900 (25.4%)
‘Missing’ 1 (0.00%) 1 (0.00%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)


As we can see in Table 1, there was a greater proportion of treatments of women and +50 users that were not available for selection. Also, among the non-selected treatments, there was a greater share of alcohol, but a lower rate of cocaine-paste as a primary substance at admission. Additionally, there was a greater proportion of therapeutic discharges and referrals, but a lower proportion of late and early drop-outs. Regarding the motives of admission to treatment, spontaneous motives had a lower percentage in comparison to the selected cases. For psychiatric comorbidity, there was a greater proportion of treatments with psychiatric comorbidity, but a lower proportion had a diagnosis under study. Another variable that was worth mentioning was occupational status, in which the inactive group had greater proportions among the non- selected group. The treatment lengths were greater in the non-selected treatments.


Format the database

Person days are conceived as the difference in each user between the minimum date of admission (the first time that each patient was admitted between 2010 and 2019) and the maximum date of discharge (the last time that a patient was discharged from a treatment between 2010 and 2019).


#** Ordena primero por hash key, y después por fecha de ingreso, 
#** desde el mas reciente al más nuevo.
#*dependiendo del signo que se le asigne a fecha de ingreso, 
#*es si será de menor (más antiguo) a mayor (más reciente) (+), o mayor a menor (-)
CONS_C1_df_dup_SEP_2020_prev<-
  CONS_C1_df_dup_JUL_2020 %>% 
    dplyr::arrange(hash_key,desc(fech_ing)) %>% 
#*eliminar a menores de 18?
    #dplyr::filter(edad_al_ing<18) %>% 
#*0.d. Contar cuántas veces se repite cada HASH
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(dup=row_number()) %>%
#*La ventaja de duplicates_filtered es que considera los perdidos
    dplyr::mutate(duplicates_filtered= n()) %>% 
#*obtener la última fecha en que registra la observación
    dplyr::mutate(startdate= dplyr::last(fech_ing_num), enddate= dplyr::first(fech_egres_num), 
                  person_days=enddate-startdate) %>% 
    dplyr::ungroup()
#+     #dplyr::arrange(hash_key, fech_ing) %>% 

label(CONS_C1_df_dup_SEP_2020_prev$dup) <- "Número de Tratamientos por HASH (posición a medida que aparecen, menor más reciente)/Number of Treatments by User"
label(CONS_C1_df_dup_SEP_2020_prev$duplicates_filtered) <- "Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total)"
label(CONS_C1_df_dup_SEP_2020_prev$startdate) <- "Fecha de Ingreso Primer Tratamiento (x HASH)/Date of Admission to the First Treatment (By User)"
label(CONS_C1_df_dup_SEP_2020_prev$enddate) <-  "Fecha de Egreso del Último Tratamiento (x HASH)/ Date of Discharge of the Last Treatment (By User)"
label(CONS_C1_df_dup_SEP_2020_prev$person_days) <- "Días en los que estado un usuario en el sistema para el estudio/User's Days available in the system for the study"

invisible(
CONS_C1_df_dup_SEP_2020_prev %>% 
  dplyr::filter(duplicates_filtered>4,person_days>100) %>% 
  dplyr::select(hash_key, fech_ing, fech_ing_num, fech_egres_imp, fech_egres_num, person_days, dup, duplicates_filtered)
)

if(CONS_C1_df_dup_SEP_2020_prev %>% 
   dplyr::filter(person_days<0) %>% 
   nrow()>0){stop("There are still some cases with negative days of treatment")}


We selected the first treatment of each user, in order to make an individual overview of users and time available in the dataset.


#*ALTERNATIVA 1= primer tratamiento a la cabeza
CONS_C1_df_dup_SEP_2020_prev2<-
  CONS_C1_df_dup_SEP_2020_prev %>% 
    dplyr::arrange(hash_key,fech_ing) %>% 
    dplyr::group_by(hash_key) %>% 
    dplyr::mutate(a_botar=ifelse(row_number()==1,NA_real_,1)) %>% 
    dplyr::ungroup()
CONS_C1_df_dup_SEP_2020_prev2%>% 
    janitor::tabyl(a_botar) %>% 
  
      knitr::kable(format= "html", format.args= list(decimal.mark= ".", big.mark= ","),
               caption="Table 2. First entries of each user",
               align= c("l",rep('c', 5)), col.names = c("Discarded","n","%", "Valid %"))%>%
  kableExtra::kable_styling(bootstrap_options = c("striped", "hover"),font_size= 8) 
Table 2. First entries of each user
Discarded n % Valid %
1 24,708 0.2251175 1
NA 85,048 0.7748825 NA
#%>%  kableExtra::add_footnote("Note= Treatments that were ")

label(CONS_C1_df_dup_SEP_2020_prev2$a_botar) <- "Descarta tratamientos que no permitan ver la duración del usuario entre 2010-2019"


Next, we discarded posterior treatments, selecting 85,048 records.


#85048+17764+4853+1449+435+149+45+11+1+1
#En STATA entrega es en total 85,048 entradas
#0 |     67,284 = 
#1 |     17,764 =  
CONS_C1_df_dup_SEP_2020_prev3<-  
CONS_C1_df_dup_SEP_2020_prev2 %>% 
    dplyr::filter(is.na(a_botar)) %>% #nrow() 85,048, QUEDARME SÓLO CON LOS CASOS QUE SON IS.NA
    dplyr::mutate(event=ifelse(duplicates_filtered>1,1,0)) %>% 
      #dplyr::mutate(event=factor(event,labels=c("W/o Readmissions","W/Readmissions"))) %>% 
    dplyr::mutate(person_years=person_days/365.25)#%>% janitor::tabyl(event)
#table(CONS_C1_df_dup_SEP_2020_prev2$duplicates_filtered)
label(CONS_C1_df_dup_SEP_2020_prev3$event) <- "Segundo, Tercero o Tratamiento Posterior (1=Reingreso x HASH) /Posterior Treatment of Each User (1=Readmission)"
#**STSET
#stset person_days, failure(event==1) scale(365.25) id(row)
#stdescribe, weight
#**TOTAL INCIDENCE RATE
#egen sum_0 = total(person_days) if event==0
#egen sum_1 = total(person_days) if event==1
#egen sum_tot = total(person_days)
#codebook sum_0 sum_1 sum_tot
#tab _d if _d==1
#*cumulative incidence rate of readmission
#di %9.1f (r(N)/(sum_tot/365.25)*1000)

#table(CONS_C1_df_dup_SEP_2020_prev3$event)
#table(is.na(CONS_C1_df_dup_SEP_2020_prev3$person_days))

#http://www.columbia.edu/~cjd11/charles_dimaggio/DIRE/resources/R/packages.pdf


The total incidence rate was 174.1 readmissions per 1,000 person-years.


Incidence rate of the first readmission by groups


We recoded the variables of cause of discharge, in order to account for early drop-out and late drop-out. Also we recoded the variables of main type of drug, in order to account for the main substance and comparisons between them: cocaine vs. alcohol, cocaine paste vs. alcohol, and marihuana vs. alcohol.


CONS_C1_df_dup_SEP_2020_prev4<-
CONS_C1_df_dup_SEP_2020_prev3 %>% 
  dplyr::mutate(mot_egres_latewdl=dplyr::case_when(grepl("Tard",motivodeegreso_mod_imp)~"1.Late WDRL",
                                                        grepl("Terap",motivodeegreso_mod_imp)~"0.Therapeutic Disch.",TRUE~NA_character_)) %>% 
  dplyr::mutate(mot_egres_latewdl= factor(mot_egres_latewdl)) %>% 
  dplyr::mutate(mot_egres_earlywdl=dplyr::case_when(grepl("Temprano",motivodeegreso_mod_imp)~"1.Early WDRL",
                                                        grepl("Terap",motivodeegreso_mod_imp)~"0.Therapeutic Disch.",TRUE~NA_character_)) %>% 
  dplyr::mutate(mot_egres_earlywdl= as.factor(mot_egres_earlywdl)) %>% 
  dplyr::mutate(droga_rec_coc=dplyr::case_when(grepl("Coca",sus_principal_mod)~"1.Cocaine",
                                                        grepl("Alcohol",sus_principal_mod)~"0.Alcohol",
                                                        TRUE~NA_character_)) %>% 
  dplyr::mutate(droga_rec_coc=as.factor(droga_rec_coc)) %>% 
  dplyr::mutate(droga_rec_pbase=dplyr::case_when(grepl("Pasta",sus_principal_mod)~"1.Paste Base",
                                                        grepl("Alcohol",sus_principal_mod)~"0.Alcohol",
                                                        TRUE~NA_character_)) %>% 
  dplyr::mutate(droga_rec_pbase=as.factor(droga_rec_pbase)) %>% 
  dplyr::mutate(droga_rec_mar=dplyr::case_when(grepl("Mari",sus_principal_mod)~"1.Marijuana",
                                                        grepl("Alcohol",sus_principal_mod)~"0.Alcohol",
                                                        TRUE~NA_character_)) %>% 
  dplyr::mutate(droga_rec_mar=as.factor(droga_rec_mar)) %>% 
  ungroup()


We calculated the incidence-rate ratios of different groups and checked whether there was an association between the exposure and the rate of readmissions.


#https://www.stata.com/features/tables-for-epidemiologists/
#https://www.stata.com/manuals/repitab.pdf
#ir event mot_egres_earlywdl _t
#ir is used with incidence-rate (incidence-density or person-time) data. It calculates point estimates and confidence intervals for the incidence-rate ratio (IRR) and incidence-rate difference (IRD), along with attributable or prevented fractions for the exposed and total population. iri is the immediate form of ir;

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_
#table(CONS_C1_df_dup_SEP_2020_prev4$event,as.numeric(CONS_C1_df_dup_SEP_2020_prev4$mot_egres_earlywdl)-1)
 
irrs_early<-irrs(x="mot_egres_earlywdl" ,db="CONS_C1_df_dup_SEP_2020_prev4")
irrs_late<-irrs(x="mot_egres_latewdl" ,db="CONS_C1_df_dup_SEP_2020_prev4")

irrs_coc<-irrs(x="droga_rec_coc" ,db="CONS_C1_df_dup_SEP_2020_prev4")
irrs_pbas<-irrs(x="droga_rec_pbase" ,db="CONS_C1_df_dup_SEP_2020_prev4")
irrs_mar<-irrs(x="droga_rec_mar" ,db="CONS_C1_df_dup_SEP_2020_prev4")


Compared to users with therapeutic discharge as the cause of discharge, the incidence rate of readmission was 2.46 (95% IC 2.34-2.58) times higher for those that dropped-out of treatment before 3 months in treatment.


Compared to users with therapeutic discharge as the cause of discharge, the incidence rate of readmission was 1.54 (95% IC 1.48-1.6) times higher for those that dropped-out of treatment after 3 months in treatment.


Compared to those with alcohol as the main substance at admission, the cumulative incidence rate of readmission was 1.26 (IC95% 1.2-1.31) times higher for those using cocaine.


Compared to those with alcohol as the main substance at admission, the cumulative incidence rate of readmission was 1.47 (IC95% 1.42-1.53) times higher for those using cocaine paste.


Compared to those with alcohol as the main substance at admission, there were non-significant differences in the cumulative incidence for those using marijuana in comparison to those using alcohol (p = 0.165).


Time outside treatment


To calculate the average time outside treatment, we needed to drop cases that only had one treatment and keep those with more than one treatment.


Then, we had to order the dataset by HASHs and date of admission and date of discharge, and replace the date of discharge if it is greater than the next date of admission.


Graphical representation

The following figures shows the Nelson-Aalen for the cumulative hazard of experiencing readmission in the study period.


Figure 2. Cum. Hazards to Experience Readmission to SUD Treatment, by cause of Discharge

Figure 2. Cum. Hazards to Experience Readmission to SUD Treatment, by cause of Discharge


Figure 3. Cum. Hazards to Experience Readmission to SUD Treatment, by cause of Discharge

Figure 3. Cum. Hazards to Experience Readmission to SUD Treatment, by cause of Discharge

Figure 4. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission

Figure 4. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission

Figure 5. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission

Figure 5. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission


Figure 6. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission

Figure 6. Cum. Hazards to Experience Readmission to SUD Treatment, by Primary Substance At Admission

  #http://minato.sip21c.org/msb/man/ratedifference.html
library(fmsb)
diff_rate<- ratedifference(table(CONS_C1_df_dup_SEP_2020_prev4$event,as.numeric(CONS_C1_df_dup_SEP_2020_prev4$mot_egres_earlywdl)-1)[2],table(CONS_C1_df_dup_SEP_2020_prev4$event,as.numeric(CONS_C1_df_dup_SEP_2020_prev4$mot_egres_earlywdl)-1)[4],as.numeric(xtabs(person_days ~ mot_egres_earlywdl, data=CONS_C1_df_dup_SEP_2020_prev4)[2]),as.numeric(xtabs(person_days ~ mot_egres_earlywdl, data=CONS_C1_df_dup_SEP_2020_prev4)[1]), CRC=T, conf.level=0.95) 
 
library(survminer)
ggsurvplot(
   fit,                     # survfit object with calculated statistics.
   pval = TRUE,             # show p-value of log-rank test.
   conf.int = TRUE,         # show confidence intervals for 
                            # point estimaes of survival curves.
   conf.int.style = "step",  # customize style of confidence intervals
   xlab = "Time in days",   # customize X axis label.
   break.time.by = 200,     # break X axis in time intervals by 200.
   ggtheme = theme_light(), # customize plot and risk table with a theme.
   risk.table = "abs_pct",  # absolute number and percentage at risk.
  risk.table.y.text.col = T,# colour risk table text annotations.
  risk.table.y.text = FALSE,# show bars instead of names in text annotations
                            # in legend of risk table.
  ncensor.plot = TRUE,      # plot the number of censored subjects at time t
  surv.median.line = "hv",  # add the median survival pointer.
  legend.labs = 
    c("Male", "Female"),    # change legend labels.
  palette = 
    c("#E7B800", "#2E9FDF") # custom color palettes.
)

ggsurvplot(fit2, fun = "event", conf.int = TRUE,
           ggtheme = theme_bw())

#Fitting the survival model
survival_func=survfit(Surv(CONS_C1_df_dup_SEP_2020_prev3$person_years, CONS_C1_df_dup_SEP_2020_prev3$event== 1)~1)

#http://rstudio-pubs-static.s3.amazonaws.com/316989_83cbe556125645b698c9ff6cf88c4c1a.html
#https://thriv.github.io/biodatasci2018/r-survival.html
#http://si.biostat.washington.edu/sites/default/files/modules/SISCR_2018_11_all-2pp_0.pdf
#https://www.researchgate.net/profile/Claudia_Castro-Kuriss/publication/325390160_Analisis_de_Supervivencia_mediante_el_empleo_de_R/links/5b0aba27a6fdcc8c25333860/Analisis-de-Supervivencia-mediante-el-empleo-de-R.pdf?origin=publication_detail
#http://www.sthda.com/english/wiki/survival-analysis-basics

#SURVIVAL= Explores factors that are thought to influence the chance that the event occurs
#Datos censurados= pueden ser por distintas causas:
    #- El paciente no refirió un evento (la readmissión) durante el estudio, y no sabemos si el evento ocurrió después. ESTOS SON LOS QUE TENGO QUE DARLES UN DIFF TREAT HASTA EL DIA DE HOY. SIEMPRE Y CUANDO TENGAN MENOS DE 1095 DIAS PARA PERDIDOS EN FECHA DE EGRESO, Y NO ESTÉN TRUNCADOS A LA DERECHA PORQUE NO SE LES TERMINÓ EL PRIMER TRAT.
    #Esta censura puede ocurrir cuando un usuario abandona un estudio, se pierde el seguimiento o no experimenta el evento una vez finaliza el estudio
    #- Truncado a la derecha: quien se perdió por una razón. Truncado a la derecha
    #Las muestras con censura aleatoria se consideran generalmente censuradas por derecha debido a que se van incorporando progresivamente los tiempos de fallas de distintas unidades
    #Los eventos que no experimentaron el evento en el tiempo de estudio se les censurará hasta el último tiempo de registro
    # Una suposición menos restrictiva que la suposición de independencia entre Ci y Ti, pero que alcanza para que los métodos sean válidos, es “la censura independiente” o “censura no informativa”:la probabilidad de que un individuo sea censurado en el instante t0 no depende de que ese individuo tenga inusualmente alto (o bajo) riesgo de evento.
    #Censoring may arise in the following ways:
    ###a patient has not (yet) experienced the event of interest, such as relapse or death, within the study time period;
    ###a patient is lost to follow-up during the study period;
    ###a patient experiences a different event that makes further follow-up impossible.
    #This type of censoring, named right censoring, is handled in survival analysis.

#– Recurrence rate
survfit_days_new_treat<-survfit(Surv(diff_bet_treat, status) ~ motivodeegreso_mod_imp, 
                                data=CONS_C1_df_dup_JUN_2020%>% 
                                  dplyr::mutate(diff_nas_fech_egres= as.numeric(difftime(lubridate::ymd("2019-11-13"),fech_ing, units = "days")))%>%
                                  #dplyr::filter(is.na(fech_egres_imp))%>% dplyr::select(fech_ing,fech_egres_imp,diff_nas_fech_egres)
                                  dplyr::mutate(perdi_seguimiento=dplyr::case_when(is.na(fech_egres_imp)&diff_nas_fech_egres>=1095~1,TRUE~0))%>% 
                                  dplyr::filter(perdi_seguimiento==0)%>% #NI SIQUIERA TERMINARON EL PIMER EVNTO, Y LO MAS PROBABLE ES QUE NUNCA REGISTRARON FECHA DE TÉRMINO. ESTPS SI QUE SI DEBO SACARLOS.
                                  dplyr::mutate(no_tienen_ni_el_primer_evento=dplyr::case_when(is.na(fech_egres_imp)&diff_nas_fech_egres<1095~1,
                                                                                     TRUE~0))%>% 
                                 # dplyr::filter(no_tienen_ni_el_primer_evento==0)%>%#NO HAN TERMINADO EL TRATAMIENTO. CENSURA SIMPLE TIPO 1, PERO SE DIFERENCIA DE LOS QUE NUNCA LLEGARON SIQUIERA A TENER EL PRIMER EVENTO. POR ESO A ESOS CASOS DEBO SACARLOS. AUNQUE NO ESTOY SEGURO, PORQUE PUEDE QUE ESTOS CASOSO TAMBIÉN FORMEN PARTE ED LA CENSURA AUTOMATICA QUE HACE R.
                                  #LOS QUE TIENEN 
                                  dplyr::mutate(status=dplyr::case_when(!is.na(diff_bet_treat)~1,TRUE~0)), #censurar si no tienen fechas entre trat porque no tienen un siguiente
                                    #mutate(status=dplyr::case_when(!is.na(fech_egres_imp)~1,TRUE~0)), #censurar fechas de egreso ##se supone q este es más puro, no sé
                                type = "kaplan-meier", #The Kaplan-Meier curve is a nonparametric estimator of the survival distribution (i.e. the “estimation” component of the “test/estimation” approach to analysis of time-to-event data)
                                error = "tsiatis", conf.type = "log-log", conf.int = 0.95)
#So we only know that the patient survived AT LEAST 13 months, but we have no other information available about the patient's status.  This type of censoring (also known as "right censoring") makes linear regression an inappropriate way to analyze the data due to censoring bias.

#simple
#survfit_days_new_treat_simple<-survfit(Surv(diff_bet_treat, status) ~ motivodeegreso_mod_imp, 
#                                data=CONS_C1_df_dup_JUN_2020%>% mutate(status=dplyr::case_when(!is.na(diff_bet_treat)~1,TRUE~0))%>% data.frame())

#Utilizando esta información se compara si existe alguna diferencia de las curvas de supervivencia entre los estados 
#In order to determine if there is a statistically significant difference between the survival curves, we perform what is known as a log-rank test, which tests the following hypothesis:
##H0: There is no difference in the survival function between those who were on maintenance chemotherapy and those who weren't on maintenance chemotherapy.
##Ha: There is a difference in the survival function between those who were on maintenance chemotherapy and those who weren't on maintenance chemotherapy.
#También con la orden “survdiff”, podemos realizar un test de hipótesis no paramétrico que nos diga si la diferencia de la probabilidad de supervivencia entre subgrupos es significativa o no. En este caso lo sería, al obtener un p-value < 0,05, experimentando esas diferencias en las zonas Centro y Sur, que sería en donde deberíamos de realizar un estudio más en profundidad.
  invisible(  
  survdiff(Surv(diff_bet_treat, status) ~ motivodeegreso_mod_imp, data=CONS_C1_df_dup_JUN_2020%>% mutate(status=dplyr::case_when(!is.na(diff_bet_treat)~1,TRUE~0)), rho = 0)  
  )
  #Prueba log-rank
  invisible(  
  survdiff(Surv(diff_bet_treat, status) ~ motivodeegreso_mod_imp, data=CONS_C1_df_dup_JUN_2020%>% mutate(status=dplyr::case_when(!is.na(diff_bet_treat)~1,TRUE~0)), rho = 1) 
  )

#survfit_days_new_treat_simple
survfit_days_new_treat_dataframe<-summary(survfit_days_new_treat, times=seq(0, 3500, 100), print.rmean=T,digits=2)
#
data.table(survfit_days_new_treat_dataframe$table,keep.rownames = T)%>%
  knitr::kable(format= "html", format.args= list(decimal.mark= ".", big.mark= ","),
               caption="Table 3. Estimates related to the probability that an entry kept free of a posterior one",
               align= c("l",rep('c', 5)), col.names = c("Cause of Discharge","Records","n.max", "n.start","events","rmean","se(rmean)","median", "95%CI Lower","95%CI Upper"))%>%
  kableExtra::kable_classic(bootstrap_options = c("striped", "hover"),font_size= 8)%>%
        kableExtra::add_footnote(paste0("Note= Treatments that did not finished their first treatment were discarded (n=",CONS_C1_df_dup_JUN_2020%>% 
    dplyr::mutate(diff_nas_fech_egres= as.numeric(difftime(lubridate::ymd("2019-11-13"),fech_ing, units = "days")))%>%dplyr::mutate(perdi_seguimiento=dplyr::case_when(is.na(fech_egres_imp)&diff_nas_fech_egres>=1095~1,TRUE~0))%>%dplyr::filter(perdi_seguimiento==1)%>% nrow()%>% formatC(big.mark=","),"); Excluded cases with no cause of discharge (n=",CONS_C1_df_dup_JUN_2020%>% dplyr::mutate(diff_nas_fech_egres= as.numeric(difftime(lubridate::ymd("2019-11-13"),fech_ing, units = "days")))%>% dplyr::mutate(perdi_seguimiento=dplyr::case_when(is.na(fech_egres_imp)&diff_nas_fech_egres>=1095~1,TRUE~0))%>%  dplyr::filter(perdi_seguimiento==0)%>% dplyr::mutate(status=dplyr::case_when(!is.na(diff_bet_treat)~1,TRUE~0))%>% dplyr::filter(!is.na(diff_bet_treat),is.na(motivodeegreso_mod_imp))%>% nrow() %>% formatC(big.mark=","),")"), notation = "none")%>%
  kableExtra::scroll_box(width= "100%", height = "250x")

#median time to event (the time when half the records have an event).
#Even if median survival has been reached in a group, it might not be possible to calculate complete confidence intervals for those median values,
# just knowing the difference in median survival values doesn't necessarily tell you which is better for prognosis--then you have to specify which prognosis time you care about.
#The restricted mean (rmean) and its standard error se(rmean) are based on a truncated estimator. When the last censoring time is not random this quantity is occasionally of interest.



Descriptives at baseline

Bivariate descriptive analyses were conducted using student’s t, ANOVA, Wilcoxon or Kruskall-Wallis tests (two-tailed) for continuous data and chi-square tests for categorical data stratified with readmission status.


Table 4. Summary descriptives at baseline, between Users with more than one treatment (=1) and Users with only a single treatment (=0) from 2010-2019
Variables Users w/ only one treatment Users w/ more than one treatment p-value
N=67284 N=17764
Sex: <0.001
Men 51277 (76.2%) 12348 (69.5%)
Women 16007 (23.8%) 5416 (30.5%)
People that Share Household with the User (Cohabitation Status): .
With relatives 29611 (44.0%) 8567 (48.2%)
With couple and children 17367 (25.8%) 4451 (25.1%)
Other 5880 (8.74%) 1407 (7.92%)
Alone 6524 (9.70%) 1502 (8.46%)
With children only 2592 (3.85%) 603 (3.39%)
With couple only 5309 (7.89%) 1234 (6.95%)
‘Missing’ 1 (0.00%) 0 (0.00%)
Have Children in Treatment: <0.001
Without children 58766 (87.3%) 13757 (77.4%)
With children 642 (0.95%) 409 (2.30%)
‘Missing’ 7876 (11.7%) 3598 (20.3%)
Educational Attainment: <0.001
1-More than high school 11810 (17.6%) 2258 (12.7%)
2-Completed high school or less 36503 (54.3%) 10150 (57.1%)
3-Completed primary school or less 18652 (27.7%) 5291 (29.8%)
‘Missing’ 319 (0.47%) 65 (0.37%)
Frequency of use of primary drug at admission: <0.001
1 day a week or more 4589 (6.82%) 1021 (5.75%)
2 to 3 days a week 19109 (28.4%) 4607 (25.9%)
4 to 6 days a week 10994 (16.3%) 2929 (16.5%)
Less than 1 day a week 2471 (3.67%) 525 (2.96%)
Did not use 983 (1.46%) 199 (1.12%)
Daily 28807 (42.8%) 8393 (47.2%)
‘Missing’ 331 (0.49%) 90 (0.51%)
Treatment Plan: 0.000
M-PAB 40 (0.06%) 26 (0.15%)
M-PAI 3759 (5.59%) 1059 (5.96%)
M-PR 1948 (2.90%) 1386 (7.80%)
PG-PAB 26367 (39.2%) 5680 (32.0%)
PG-PAI 28542 (42.4%) 6794 (38.2%)
PG-PR 6585 (9.79%) 2787 (15.7%)
‘Missing’ 43 (0.06%) 32 (0.18%)
Treatment Program: <0.001
General population 61494 (91.4%) 15261 (85.9%)
Women specific 5747 (8.54%) 2471 (13.9%)
‘Missing’ 43 (0.06%) 32 (0.18%)
Age at Admission: .
<18 154 (0.23%) 10 (0.06%)
18-29 22574 (33.6%) 7039 (39.6%)
30-39 21319 (31.7%) 6243 (35.1%)
40-49 14043 (20.9%) 3184 (17.9%)
50+ 9178 (13.6%) 1288 (7.25%)
‘Missing’ 16 (0.02%) 0 (0.00%)
Primary substance at admission: .
Alcohol 24404 (36.3%) 4455 (25.1%)
Cocaine hydrochloride 12930 (19.2%) 3221 (18.1%)
Marijuana 4811 (7.15%) 960 (5.40%)
Other 1310 (1.95%) 275 (1.55%)
Cocaine paste 23828 (35.4%) 8853 (49.8%)
‘Missing’ 1 (0.00%) 0 (0.00%)
Starting Substance (Most vulnerable value): 0.000
Alcohol 37591 (55.9%) 10027 (56.4%)
Cocaine hydrochloride 2270 (3.37%) 703 (3.96%)
Marijuana 17112 (25.4%) 5289 (29.8%)
Other 1625 (2.42%) 483 (2.72%)
Cocaine paste 2465 (3.66%) 1062 (5.98%)
‘Missing’ 6221 (9.25%) 200 (1.13%)
Motive of discharge: .
Late Drop-out 23335 (34.7%) 7032 (39.6%)
Early Drop-out 10690 (15.9%) 3015 (17.0%)
Administrative discharge 5403 (8.03%) 2108 (11.9%)
Therapeutic discharge 15782 (23.5%) 3496 (19.7%)
Referral to another treatment 6550 (9.73%) 2107 (11.9%)
Ongoing treatment 5521 (8.21%) 0 (0.00%)
‘Missing’ 3 (0.00%) 6 (0.03%)
Motive of Admission to Treatment (First Entry): <0.001
Spontaneous 30306 (45.0%) 7722 (43.5%)
Assisted Referral 6005 (8.92%) 1962 (11.0%)
Other 3511 (5.22%) 1003 (5.65%)
Justice Sector 6651 (9.88%) 1325 (7.46%)
Health Sector 20811 (30.9%) 5752 (32.4%)
Psychiatric comorbidity: <0.001
Without psychiatric comorbidity 26150 (38.9%) 6187 (34.8%)
Diagnosis unknown (under study) 12635 (18.8%) 3464 (19.5%)
With psychiatric comorbidity 28499 (42.4%) 8113 (45.7%)
Marital Status: <0.001
Married/Shared living arrangements 23307 (34.6%) 5809 (32.7%)
Separated/Divorced 7273 (10.8%) 1776 (10.00%)
Single 35729 (53.1%) 9987 (56.2%)
Widower 841 (1.25%) 162 (0.91%)
‘Missing’ 134 (0.20%) 30 (0.17%)
Age of Onset of Drug Use 15.0 [14.0;18.0] 15.0 [14.0;17.0] <0.001
Age of Onset of Drug Use Primary/Main Substance: <0.001
<=15 19637 (29.2%) 4549 (25.6%)
>=25 14842 (22.1%) 3917 (22.1%)
16-18 17102 (25.4%) 4965 (27.9%)
19-24 14830 (22.0%) 4278 (24.1%)
‘Missing’ 873 (1.30%) 55 (0.31%)
Occupational Category: <0.001
Salaried 20589 (30.6%) 4856 (27.3%)
Self-employed 9167 (13.6%) 1898 (10.7%)
Employer 1082 (1.61%) 208 (1.17%)
Unpaid family labour 144 (0.21%) 40 (0.23%)
Other 679 (1.01%) 172 (0.97%)
Volunteer worker 129 (0.19%) 25 (0.14%)
‘Missing’ 35494 (52.8%) 10565 (59.5%)
Occupational Status: .
Unemployed 22243 (33.1%) 6997 (39.4%)
Employed 34026 (50.6%) 7524 (42.4%)
Inactive 11014 (16.4%) 3243 (18.3%)
‘Missing’ 1 (0.00%) 0 (0.00%)
Macrozones of the Center: .
Center 50895 (75.6%) 13446 (75.7%)
North 9295 (13.8%) 2856 (16.1%)
South 7075 (10.5%) 1461 (8.22%)
‘Missing’ 19 (0.03%) 1 (0.01%)
Type of Center: <0.001
Private 17923 (26.6%) 6147 (34.6%)
Public 49342 (73.3%) 11616 (65.4%)
‘Missing’ 19 (0.03%) 1 (0.01%)
Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13) 168 [92.0;298] 166 [91.0;288] <0.001
Physical Dg. Anemia: 0.163
0 67224 (99.9%) 17741 (99.9%)
1 60 (0.09%) 23 (0.13%)
Physical Dg. Heart Disease: 0.022
0 66572 (98.9%) 17611 (99.1%)
1 712 (1.06%) 153 (0.86%)
Physical Dg. Under Study: <0.001
0 30752 (45.7%) 8404 (47.3%)
1 36532 (54.3%) 9360 (52.7%)
Physical Dg. Somatic illnesses: 0.876
0 66809 (99.3%) 17636 (99.3%)
1 475 (0.71%) 128 (0.72%)
Physical Dg. STDs: <0.001
0 66960 (99.5%) 17632 (99.3%)
1 324 (0.48%) 132 (0.74%)
Physical Dg. Alcoholic hepatitis: 0.341
0 67078 (99.7%) 17718 (99.7%)
1 206 (0.31%) 46 (0.26%)
Physical Dg. Hepatitis B: 0.709
0 67221 (99.9%) 17745 (99.9%)
1 63 (0.09%) 19 (0.11%)
Physical Dg. Chronic hepatitis: 0.207
0 67196 (99.9%) 17748 (99.9%)
1 88 (0.13%) 16 (0.09%)
Physical Dg. Infectuous diseases: 0.344
0 67193 (99.9%) 17734 (99.8%)
1 91 (0.14%) 30 (0.17%)
Physical Dg. Other phsysical conditions: 0.390
0 66458 (98.8%) 17531 (98.7%)
1 826 (1.23%) 233 (1.31%)
Physical Dg. Other phsysical conditions: 0.066
0 65510 (97.4%) 17340 (97.6%)
1 1774 (2.64%) 424 (2.39%)
Physical Dg. Oral-care pathologies: 0.079
0 66476 (98.8%) 17521 (98.6%)
1 808 (1.20%) 243 (1.37%)
Physical Dg. Development or Intrauterine: 0.032
0 67246 (99.9%) 17745 (99.9%)
1 38 (0.06%) 19 (0.11%)
Traumatisms and disabling sequelae: <0.001
0 66369 (98.6%) 17433 (98.1%)
1 915 (1.36%) 331 (1.86%)
Other mental health problems- Sexual abuse: <0.001
0 66287 (98.5%) 17355 (97.7%)
1 997 (1.48%) 409 (2.30%)
Other mental health problems- Commercial sexual exploitation: <0.001
0 66996 (99.6%) 17602 (99.1%)
1 288 (0.43%) 162 (0.91%)
Other mental health problems- Other: <0.001
0 57827 (85.9%) 14921 (84.0%)
1 9457 (14.1%) 2843 (16.0%)
Other mental health problems- Domestic violence: <0.001
0 51503 (76.5%) 12944 (72.9%)
1 15781 (23.5%) 4820 (27.1%)
Diagnosis of substance use: <0.001
Drug dependence 48021 (71.4%) 13815 (77.8%)
Hazardous consumption 19262 (28.6%) 3949 (22.2%)
‘Missing’ 1 (0.00%) 0 (0.00%)
Note. Variables of C1 dataset had to be standardized before comparison;
Continuous variables are presented as Medians and Percentiles 25 and 75 were shown;
Categorical variables are presented as number (%)

Differences within users with more than one treatment


We selected the entries of users that had at least 2 treatments (n= 42,472; users= 17,764).


p<-ggplot(CONS_C1_df_dup_JUL_2020,aes(x=diff_bet_treat), alpha=.7)+
  geom_histogram_interactive(bins=120)+
  #   geom_vline(aes(xintercept=30), color="darkred", linetype="dashed")+
  #   geom_vline(aes(xintercept=45), color="darkmagenta", linetype="solid")+
  #   geom_vline(aes(xintercept=60), color="darkblue", linetype="dotted")+
  theme_bw() +
  #facet_wrap(~motivodeegreso_mod_imp)+
labs(y = "Frequency",x="Diff. Between Treatments")#+
# xlim(c(-1,2000))
tooltip_css <- "background-color:gray;color:white;font-style:italic;padding:10px;border-radius:10px 20px 10px 20px;"
ggiraph(code = {print(p)}, tooltip_extra_css = tooltip_css, tooltip_opacity = .75)

Figure 7. Histogram of Diff Between Treatments


Summarizing by HASHs, the average treatment span between treatments among the readmitted cases (17,764) was around 1.64 years.


CONS_C1_df_dup_JUL_2020 %>% 
  dplyr::group_by(hash_key)%>%
  dplyr::mutate(n_hash=n())%>% 
  dplyr::filter(n_hash>1)%>%
  slice(1) %>% 
  ungroup() %>% 
  dplyr::select(starts_with("diff_bet_treat_")) %>%  #mean_cum_dias_trat_sin_na_1
  gather(option,value) %>%
  dplyr::mutate(option=dplyr::case_when(option=="diff_bet_treat_1"~"01",
                                        option=="diff_bet_treat_2"~"02",
                                        option=="diff_bet_treat_3"~"03",
                                        option=="diff_bet_treat_4"~"04",
                                        option=="diff_bet_treat_5"~"05",
                                        option=="diff_bet_treat_6"~"06",
                                        option=="diff_bet_treat_7"~"07",
                                        option=="diff_bet_treat_8"~"08",
                                        option=="diff_bet_treat_9"~"09",
                                        option=="diff_bet_treat_10"~"10")) %>% 
  ggplot(aes(x = factor(option), y=value,group=option)) +
  stat_summary(fun = mean, geom="bar", alpha=.8)+
  stat_summary(fun = median, geom="point")+
  stat_summary(fun = median,
                fun.min = function(x) quantile(x,.25), 
                fun.max = function(x) quantile(x,.75), 
               geom = "errorbar", width = 0.5)+
  labs(x="Number of Treatment of Each User", y="Difference Between Treatments",caption=paste0("Note. ",CONS_C1_df_dup_JUL_2020 %>% dplyr::group_by(hash_key)%>% dplyr::mutate(n_hash=n())%>%  dplyr::filter(n_hash>1)%>%slice(1) %>% nrow() %>%  formatC(big.mark=",")," users; Dots= Medians, Error bars= Percentiles 25 and 75"))+
  theme_classic2()
Figure 8. Average Cum. Differences Between Treatments, depending on the amount of treatments by User

Figure 8. Average Cum. Differences Between Treatments, depending on the amount of treatments by User


A view on the differences between treatments while considering the times each user had been readmitted from 2010-2019 showed that on average, the differences between the first and the second treatment were apparently greater than the rest. However, this difference also had a greater variability.


CONS_C1_df_dup_JUL_2020 %>% 
  dplyr::group_by(hash_key)%>%
  dplyr::mutate(n_hash=n())%>% 
  dplyr::filter(n_hash>1)%>%
  slice(1) %>% 
  ungroup() %>% 
  dplyr::select(starts_with("mean_cum_dias_trat_sin_na_")) %>%  #mean_cum_dias_trat_sin_na_1
  gather(option,value) %>%
  dplyr::mutate(option=dplyr::case_when(option=="mean_cum_dias_trat_sin_na_1"~"01",
                                        option=="mean_cum_dias_trat_sin_na_2"~"02",
                                        option=="mean_cum_dias_trat_sin_na_3"~"03",
                                        option=="mean_cum_dias_trat_sin_na_4"~"04",
                                        option=="mean_cum_dias_trat_sin_na_5"~"05",
                                        option=="mean_cum_dias_trat_sin_na_6"~"06",
                                        option=="mean_cum_dias_trat_sin_na_7"~"07",
                                        option=="mean_cum_dias_trat_sin_na_8"~"08",
                                        option=="mean_cum_dias_trat_sin_na_9"~"09",
                                        option=="mean_cum_dias_trat_sin_na_10"~"10")) %>% 
  ggplot(aes(x = factor(option), y=value,group= option)) +
      stat_summary(fun = mean, geom="bar",alpha=.8)+
    stat_summary(fun = median, geom="point")+
  stat_summary(fun.y = median,
                   fun.min = function(x) quantile(x,.25), 
               fun.max = function(x) quantile(x,.75), 
               geom = "errorbar", width = 0.5)+
  #geom_bar(stat = "identity")+
    #geom_errorbar() +
  labs(x="Number of Treatment of Each User", y="Avg. Cum. Days in Treatment",caption=paste0("Note. ",CONS_C1_df_dup_JUL_2020 %>% dplyr::group_by(hash_key)%>% dplyr::mutate(n_hash=n())%>%  dplyr::filter(n_hash>1)%>%slice(1) %>% nrow() %>%  formatC(big.mark=",")," users; Dots= Medians, Error bars= Percentiles 25 and 75"))+
  theme_classic2()
Figure 9. Average Cum. Treatment Length, depending on the amount of treatments by User

Figure 9. Average Cum. Treatment Length, depending on the amount of treatments by User


The average cumulative days in treatment tended to remain stable, independently of the amount of previous treatments.


Consolidating a database for access

We first translated relevant categories, restricted some imputed dates that trespassed the study period (over 2019-11-13), standardized ICD-10 and DSM-IV sum of categories, corrected labels such as owning status of housing conditions, and generated a dummyfied version of every physical diagnostic and other mental health related problems into ones and zeros for each category, as well as the most vulnerable substance of the starting substances (if the user indicated more than one) (sus_ini_mod_mvv, “Paste Base”>“Cocaine hydrochloride”>“Marijuana”>“Alcohol”>“Other”).


CONS_C1_df_SEP_2020<-
clean_names(CONS_C1_df) %>% 
    dplyr::filter(!row %in% unlist(CONS_C1_df_dup_JUL_2020$row)) %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #escolaridad 
    dplyr::mutate(escolaridad_rec=ifelse(as.character(escolaridad_ultimo_ano_cursado)=="BASICA COMPLETA"|as.character(escolaridad_ultimo_ano_cursado)=="BASICA INCOMPLETA"|as.character(escolaridad_ultimo_ano_cursado)=="SIN ESTUDIOS","Ed Primaria Completa o Menor",as.character(escolaridad_ultimo_ano_cursado))) %>%
dplyr::mutate(escolaridad_rec=ifelse(escolaridad_rec=="MEDIA COMPLETA"|escolaridad_rec=="MEDIA INCOMPLETA","2-Ed Secundaria Completa o Menor",
    ifelse(escolaridad_rec=="NO SABE O NO SE APLICA",NA,
    ifelse(escolaridad_rec %in% c("TECNICA COMPLETA", "TECNICA INCOMPLETA", "UNIVERSITARIA COMPLETA O MAS", "UNIVERSITARIA INCOMPLETA"), "1-Mayor a Ed Secundaria",ifelse(
      escolaridad_rec=="Ed Primaria Completa o Menor","3-Ed Primaria Completa o Menor",escolaridad_rec))))) %>%
dplyr::mutate(escolaridad_rec=factor(escolaridad_rec,labels=c('1-More than high school','2-Completed high school or less','3-Completed primary school or less')))  %>% 

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frecuencia consumo sus principal 
      dplyr::mutate(freq_cons_sus_prin=ifelse(frecuencia_de_consumo_sustancia_principal== "Desconocida",NA,as.character(frecuencia_de_consumo_sustancia_principal))) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #tipo de plan 
      dplyr::mutate(tipo_de_plan_2=dplyr::recode(tipo_de_plan_2,"M-PAI2"= "M-PAI", "M-PR2"="M-PR","PG PAI 2"="PG-PAI", "Otro"="PG-PR", "CALLE"="PG-PR"))%>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #fecha de nacimiento
  dplyr::mutate(fech_nac=lubridate::parse_date_time(stringi::stri_sub(id,-8,-1),"dmY")) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #edad al ingreso
  dplyr::mutate(edad_al_ing=lubridate::time_length(difftime(as.Date(fech_ing), as.Date(fech_nac)),"years")) %>% #AGREGADO EN APR 2020.
  dplyr::mutate(edad_al_ing=replace(edad_al_ing, is.na(edad), NA)) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Edad Inicio Sus Principal  
   dplyr::mutate(edad_ini_sus_prin_grupos=ifelse(edad_ini_sus_prin>=25,">=25",
                                                ifelse(edad_ini_sus_prin>18,"19-24",
                                                ifelse(edad_ini_sus_prin>15,"16-18",
                                                ifelse(edad_ini_sus_prin>0,"<=15",
                                                NA_character_)))))%>% 
    dplyr::mutate(edad_ini_sus_prin_grupos=as.factor(edad_ini_sus_prin_grupos)) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #sus principal  
  dplyr::mutate(sus_principal= dplyr::recode(sustancia_principal,
                                             "Hipnóticos "= "Tranquilizantes e Hipnóticos",
                                             "Sedantes:  diazepam, Valium, clonazepam, Ravotril, alprazolam, adax, barbitúricos, fenobarbital." = "Tranquilizantes e Hipnóticos",
                                             "Anfetaminas"="Estimulante tipo anfetaminas",
                                             "Extasis"="Estimulante tipo anfetaminas",
                                             "Fenilciclidina"="Estimulante tipo anfetaminas",
                                             "Metanfetaminas y otros derivados"="Estimulante tipo anfetaminas",
                                             "Otros Estimulantes"="Estimulante tipo anfetaminas",
                                             "LSD"="Alucinógenos",
                                             "Otros Alucinógenos"="Alucinógenos",
                                             "Crack"="Pasta Base",
                                             "Heroína"="Opioides",
                                             "Metadona"="Opioides",
                                             "Otros Opioides Analgésicos: morfina, codeína, meperidina,  demerol, tramadol, tramal."="Opioides","Inhalables: neopren, GHB, óxido nitroso (gas hilarante), \"poppers\", solventes, gasolina, diluyente"="Inhalables","Esteroides Anabólicos"="Otros")) %>%
      dplyr::mutate(sus_principal_mod= dplyr::case_when(sus_principal!="Alcohol"&sus_principal!="Cocaína"&sus_principal!="Marihuana"&sus_principal!="Pasta Base"~"Otros",TRUE~as.character(sus_principal)))%>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #dias_trat
      dplyr::mutate(fech_ing_num=as.numeric(as.Date(fech_ing)))%>%
      dplyr::mutate(fech_egres_num=as.numeric(as.Date(fech_egres)))%>%
      dplyr::mutate(fech_egres_num=ifelse(is.na(fech_egres),18213,fech_egres_num))%>% #equivalente a 2019-11-13
      dplyr::mutate(dias_treat_imp_sin_na=fech_egres_num-fech_ing_num)%>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #dias tratamiento
      dplyr::mutate(dias_trat_alta_temprana=ifelse(dias_trat>=90,0,1)) %>%
      dplyr::mutate(dias_trat_alta_temprana=as.factor(dias_trat_alta_temprana)) %>%
      dplyr::mutate(dias_trat_alta_temprana= dplyr::recode(dias_trat_alta_temprana, "1"="Menos de 90 días", "0"="Mayor o igual a 90 días")) %>%
      dplyr::mutate(motivodeegreso_mod= ifelse(dias_trat_alta_temprana=="Menos de 90 días" & motivodeegreso=="Abandono", "Abandono Temprano",as.character(motivodeegreso))) %>% 
      dplyr::mutate(motivodeegreso_mod= ifelse(dias_trat_alta_temprana=="Mayor o igual a 90 días" & motivodeegreso=="Abandono", "Abandono Tardio",as.character(motivodeegreso_mod))) %>% 
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #motivodeegreso_mod_imp
      dplyr::mutate(motivodeegreso_mod_imp=ifelse(dias_treat_imp_sin_na<1095 & is.na(fech_egres),"En curso",as.character(motivodeegreso_mod))) %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #origen de ingreso
      dplyr::mutate(origen_ingreso=ifelse(as.character(origen_de_ingreso)=="Consulta Espontánea", "Consulta Espontánea",as.character(origen_de_ingreso))) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Establecimiento Educacional"|origen_ingreso=="Otros"|origen_ingreso=="Trabajo (empresa o empleador)"|origen_ingreso=="Servicios Sociales u otros (iglesia, Mideplan, ser. comunitarios, etc.)", "Otros",origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Juzgado con Competencia en Crimen"|origen_ingreso=="Juzgado de Garantía"|origen_ingreso=="Libertad Vigilada"|origen_ingreso=="Juzgado de Familia"|origen_ingreso=="Juzgado de Policía"|origen_ingreso=="Otros (fiscalía)","Sector Justicia",origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Otros de la Red de Salud General Privado"|origen_ingreso=="Estab. de APS"|origen_ingreso=="Estab. de APS "|origen_ingreso=="Otros de la Red de Salud General Público", "Sector Salud", origen_ingreso)) %>%
      dplyr::mutate(origen_ingreso=ifelse(origen_ingreso=="Otro Centro Tratamiento Drogas"|origen_ingreso=="FONODROGAS"|origen_ingreso=="Previene","Otro Centro Tratamiento Drogas/FONODROGAS/Previene",origen_ingreso)) %>%
          
      dplyr::mutate(origen_ingreso_mod= dplyr::case_when(grepl("Otro Centro Tratamiento",as.character(origen_ingreso))~"Derivación Asistida", TRUE~as.character(origen_ingreso)))%>%  
      dplyr::mutate(origen_ingreso_mod=factor(origen_ingreso_mod,labels=c('Spontaneous','Assisted Referral','Other','Justice Sector','Health Sector')))  %>% 

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #CIE_10
      dplyr::mutate(across(c(diagnostico_trs_psiquiatrico_cie_10,x2_diagnostico_trs_psiquiatrico_cie_10,x3_diagnostico_trs_psiquiatrico_cie_10),~dplyr::case_when(grepl("En estudio",as.character(.))~0,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~1), .names="dg_{col}"))%>%
      dplyr::mutate(across(c(diagnostico_trs_psiquiatrico_cie_10,x2_diagnostico_trs_psiquiatrico_cie_10,x3_diagnostico_trs_psiquiatrico_cie_10),~dplyr::case_when(grepl("En estudio",as.character(.))~1,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~0), .names="instudy_{col}"))%>%
      dplyr::mutate(instudy_total_cie_10 = base::rowSums(dplyr::select(., instudy_diagnostico_trs_psiquiatrico_cie_10, instudy_x2_diagnostico_trs_psiquiatrico_cie_10, instudy_x3_diagnostico_trs_psiquiatrico_cie_10)))%>%
      dplyr::mutate(dg_total_cie_10 = base::rowSums(dplyr::select(., dg_diagnostico_trs_psiquiatrico_cie_10, dg_x2_diagnostico_trs_psiquiatrico_cie_10, dg_x3_diagnostico_trs_psiquiatrico_cie_10)))%>%
      dplyr::mutate(dg_cie_10_rec=dplyr::case_when(dg_total_cie_10>0 ~2,
                                             instudy_total_cie_10>0 & dg_total_cie_10==0~1,
                                             instudy_total_cie_10>0 & is.na(dg_total_cie_10)~1,
                                             TRUE~0))%>%
      dplyr::mutate(dg_cie_10_rec=as.factor(dg_cie_10_rec))%>%
      dplyr::mutate(dg_cie_10_rec=factor(dg_cie_10_rec,labels=c('Without psychiatric comorbidity','Diagnosis unknown (under study)', 'With psychiatric comorbidity'))) %>% 
      dplyr::mutate(tipo_de_programa_2=dplyr::case_when(grepl("M-", tipo_de_plan_2)~"Women specific",
                                                        grepl("PG-", tipo_de_plan_2)~"General population",
                                                        grepl("LV", tipo_de_plan_2)~"Other")) %>% 
      dplyr::mutate(sexo_2=factor(sexo_2,labels=c('Men','Women'))) %>% 
      dplyr::mutate(edad_al_ing_grupos=dplyr::case_when(
      edad_al_ing>=50~"50+",
          edad_al_ing>=40~"40-49",
          edad_al_ing>=30~"30-39",
          edad_al_ing>=18~"18-29",
          edad_al_ing<18~"<18",
          TRUE~NA_character_)) %>%
      dplyr::mutate(edad_al_ing_grupos=as.factor(edad_al_ing_grupos)) %>%  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Edad Inicio Sus Principal  
   dplyr::mutate(edad_ini_sus_prin_grupos=ifelse(edad_inicio_sustancia_principal>=25,">=25",
                                                ifelse(edad_inicio_sustancia_principal>18,"19-24",
                                                ifelse(edad_inicio_sustancia_principal>15,"16-18",
                                                ifelse(edad_inicio_sustancia_principal>0,"<=15",
                                                NA_character_)))))%>% 
    dplyr::mutate(edad_ini_sus_prin_grupos=as.factor(edad_ini_sus_prin_grupos)) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Cause of discharge
      dplyr::mutate(motivodeegreso_mod_imp=factor(motivodeegreso_mod_imp,labels=c('Late Drop-out',
                                                                                  'Early Drop-out',
                                                                                  'Administrative discharge',
                                                                                  'Therapeutic discharge',
                                                                                  'Referral to another treatment',
                                                                                  'Ongoing treatment'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frequency of consumption
  dplyr::mutate(freq_cons_sus_prin=factor(freq_cons_sus_prin,labels=c("1 day a week or more",
                                                                                  "2 to 3 days a week",
                                                                                  "4 to 6 days a week",
                                                                                  "Less than 1 day a week",
                                                                                  "Did not use",
                                                                                  "Daily"))) %>%     
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #primary substance
    dplyr::mutate(sus_principal_mod=factor(sus_principal_mod,labels=c("Alcohol",
                                                                                  "Cocaine hydrochloride",
                                                                                  "Marijuana",
                                                                                  "Other",
                                                                                  "Cocaine paste"))) %>%   
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #marital status
    dplyr::mutate(estado_conyugal_2=ifelse(as.character(estado_conyugal)=="Casado"|as.character(estado_conyugal)=="Conviviente"|as.character(estado_conyugal)=="conviviente civil", "Casado/Conviviente",stringr::str_trim(as.character(estado_conyugal)))) %>%
    dplyr::mutate(estado_conyugal_2=ifelse(estado_conyugal_2=="Separado"|estado_conyugal_2=="Divorciado"|estado_conyugal_2=="Anulado", "Separado/Divorciado",estado_conyugal_2)) %>% 
    dplyr::mutate(estado_conyugal_2=ifelse(estado_conyugal_2=="Nocontesta",NA,estado_conyugal_2)) %>%
    #dplyr::mutate(estado_conyugal=factor(estado_conyugal, labels=c('asd','asda'))) %>%
    dplyr::mutate(estado_conyugal_2=factor(estado_conyugal_2, labels=c('Married/Shared living arrangements','Separated/Divorced','Single','Widower'))) %>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #region
   dplyr::mutate(macrozona=as.factor(dplyr::recode(region_del_centro,
                                              "DE ANTOFAGASTA"="Norte",
                                              "DE LA ARAUCANIA"="Sur",
                                              "DE ARICA Y PARINACOTA"="Norte",
                                              "DE ATACAMA"="Norte",
                                              "DE AYSEN DEL GENERAL CARLOS IBA?S DEL CAMPO"="Sur",
                                              "DEL BIO-BIO"="Centro",
                                              "DE COQUIMBO"="Norte",
                                              "DE LOS LAGOS"="Sur",
                                              "DE LOS RIOS"="Sur",
                                              "DE MAGALLANES Y LA ANTARTICA CHILENA"="Sur",
                                              "DEL MAULE"="Centro",
                                              "METROPOLITANA"="Centro",
                                              "DE ?BLE"="Centro",
                                              "DEL LIBERTADOR GENERAL  BERNARDO OHIGGINS"="Centro",
                                              "DE TARAPACA"="Norte",
                                              "DE VALPARAISO"="Centro")))%>%
  dplyr::mutate(macrozona=factor(macrozona, labels=c('Center',
                                                                   'North',
                                                                   'South'))) %>%
  dplyr::mutate(nombre_region=as.factor(dplyr::recode(region_del_centro,
                                              "DE ANTOFAGASTA"="Antofagasta (02)",
                                              "DE LA ARAUCANIA"="Araucanía (09)",
                                              "DE ARICA Y PARINACOTA"="Arica (15)",
                                              "DE ATACAMA"="Atacama (03)",
                                              "DE AYSEN DEL GENERAL CARLOS IBA?S DEL CAMPO"="Aysén (11)",
                                              "DEL BIO-BIO"="Biobío (08)",
                                              "DE COQUIMBO"="Coquimbo (04)",
                                              "DE LOS LAGOS"="Los Lagos (10)",
                                              "DE LOS RIOS"="Los Ríos (14)",
                                              "DE MAGALLANES Y LA ANTARTICA CHILENA"="Magallanes (12)",
                                              "DEL MAULE"="Maule (07)",
                                              "METROPOLITANA"="Metropolitana (13)",
                                              "DE ?BLE"="Ñuble (16)",
                                              "DEL LIBERTADOR GENERAL  BERNARDO OHIGGINS"="O'Higgins (06)",
                                              "DE TARAPACA"="Tarapacá (01)",
                                              "DE VALPARAISO"="Valparaíso (05)")))%>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #type of center (pub/priv)
  dplyr::mutate(tipo_centro=factor(tipo_centro, labels=c('Private','Public'))) %>%
  dplyr::mutate(nombre_region=factor(nombre_region)) %>% 
  dplyr::mutate(macrozona=factor(macrozona)) %>% 
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #occupational status (inactive…)
  dplyr::mutate(estatus_ocupacional=ifelse(condicion_ocupacional=="Trabajando actualmente","Empleado",
                                           ifelse(condicion_ocupacional=="Buscando trabajo por primera vez"|condicion_ocupacional=="Cesante","Desempleado","Inactivo"))) %>%
  dplyr::mutate(estatus_ocupacional=factor(estatus_ocupacional, labels=c('Unemployed',
                                                                         'Employed',
                                                                         'Inactive'))) %>%
      #Comprende la relación entre una persona económicamente activa y su trabajo o empleo. Sólo se aplica aquellas personas que se encuentran trabajando al momento de ingresar a tratamiento (las que respondieron 1 en la pregunta anterior). Para las personas que no están trabajando (estudiantes, cesante, etc.) esta opción estará bloqueada.
      #data.table(table(CONS_C1_df_dup_ENE_2020_prev3$Categoría.Ocupacional))
  dplyr::mutate(cat_ocupacional=ifelse(condicion_ocupacional!="Trabajando actualmente",NA,as.character(categoria_ocupacional)))%>%
  dplyr::mutate(cat_ocupacional=as.factor(cat_ocupacional)) %>%
  dplyr::mutate(cat_ocupacional=factor(cat_ocupacional, labels=c('Salaried',
                                                                             'Self-employed',
                                                                             'Employer',
                                                                             'Unpaid family labour',
                                                                             'Other',
                                                                             'Volunteer worker'))) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #age of onset of drug use
  dplyr::mutate(edad_ini_cons= ifelse(edad_inicio_consumo<=edad_al_ing, edad_inicio_consumo, NA)) %>%
  dplyr::mutate(edad_ini_cons= ifelse(edad_ini_cons<5,NA,edad_ini_cons)) %>%

  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #physical diagnose
  dplyr::mutate(dg_fis_anemia= ifelse(grepl("Anemia",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_card= ifelse(grepl("Cardiopat",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_in_study= ifelse(grepl("En estudio",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_enf_som= ifelse(grepl("somáticas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_ets= ifelse(grepl("ETS",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_alc= ifelse(grepl("Hepatitis alcohólica",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_b= ifelse(grepl("Hepatitis B",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_cro= ifelse(grepl("Hepatitis crónica",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_inf= ifelse(grepl("Infecciosas relacionadas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_otr_cond_fis_ries_vit= ifelse(grepl("Condiciones de Riesgo Vital",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_otr_cond_fis= ifelse(grepl("condiciones Fisicas Limitantes",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_pat_buc= ifelse(grepl("Bucal",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_pat_ges_intrau= ifelse(grepl("niño intrauterino",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_trau_sec= ifelse(grepl("Traumatismos y secuelas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(otros_pr_sm_abu_sex= ifelse(grepl("Abuso Sexual",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
  dplyr::mutate(otros_pr_sm_exp_com_sex= ifelse(grepl("omercial Sexual",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
  dplyr::mutate(otros_pr_sm_otros= ifelse(grepl("Otros",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
  dplyr::mutate(otros_pr_sm_prision= ifelse(grepl("Prisionizaci?",otros_problemas_de_atencion_de_salud_mental),1,0))%>%
  dplyr::mutate(otros_pr_sm_vif= ifelse(grepl("Violencia Intrafamiliar",otros_problemas_de_atencion_de_salud_mental),1,0)) %>% 
  dplyr::mutate(dg_trs_cons_sus_or= dplyr::case_when(grepl("Consumo Perjudicial",diagnostico_trs_consumo_sustancia)~"Hazardous consumption",
                                                  grepl("Dependencia",diagnostico_trs_consumo_sustancia)~"Drug dependence",
                                                  TRUE~NA_character_)) 
#CONS_C1_df_dup_SEP_2020 %>% summarise(mean=mean(dias_treat_imp_sin_na),mdn=median(dias_treat_imp_sin_na),p25=quantile(dias_treat_imp_sin_na,.25),p75=quantile(dias_treat_imp_sin_na,.75))
#   mean   mdn   p25   p75
#  <dbl> <dbl> <dbl> <dbl>
##  215.   164    90   290
CONS_C1_df_dup_JUL_2020 %>% 
  dplyr::arrange(hash_key,fech_ing) %>% 
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(dup=row_number()) %>%
  #*La ventaja de duplicates_filtered es que considera los perdidos
  dplyr::mutate(duplicates_filtered= n()) %>% 
  #*obtener la última fecha en que registra la observación
  dplyr::ungroup() %>% 
  dplyr::mutate(a_botar=ifelse(dup==1,1,NA_real_)) %>% 
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #date of discharge: correction  (más detalle en change_dates_out_of_study.R)
  dplyr::mutate(fech_egres_imp=dplyr::case_when(fech_egres_imp>"2019-11-13"~as.Date("2019-11-13"),TRUE~fech_egres_imp)) %>%
      #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
      #dias_trat
      dplyr::mutate(fech_ing_num=as.numeric(as.Date(fech_ing)))%>%
      dplyr::mutate(fech_egres_num=as.numeric(as.Date(fech_egres_imp)))%>%
      dplyr::mutate(fech_egres_num=ifelse(is.na(fech_egres_imp),18213,fech_egres_num))%>%
    #equivalente a 2019-11-13
      dplyr::mutate(dias_treat_imp_sin_na=fech_egres_num-fech_ing_num)%>%
      
      #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
      #dias tratamiento
      dplyr::mutate(dias_trat_alta_temprana=ifelse(dias_treat_imp_sin_na>=90,0,1)) %>%
      dplyr::mutate(dias_trat_alta_temprana=as.factor(dias_trat_alta_temprana)) %>%
      dplyr::mutate(dias_trat_alta_temprana= dplyr::recode(dias_trat_alta_temprana, "1"="Menos de 90 días", "0"="Mayor o igual a 90 días")) %>%
      dplyr::mutate(motivodeegreso_mod_imp= ifelse(dias_trat_alta_temprana=="Menos de 90 días" & grepl("Abandono",motivodeegreso_mod_imp), "Abandono Temprano",as.character(motivodeegreso_mod_imp))) %>% 
      dplyr::mutate(motivodeegreso_mod_imp= ifelse(dias_trat_alta_temprana=="Mayor o igual a 90 días" & grepl("Abandono",motivodeegreso_mod_imp), "Abandono Tardio",as.character(motivodeegreso_mod_imp))) %>% 
      
      #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
      #motivodeegreso_mod_imp
      dplyr::mutate(motivodeegreso_mod_imp=ifelse(dias_treat_imp_sin_na<1095 & 
                                                    is.na(fech_egres_imp),"En curso",
                                                  as.character(motivodeegreso_mod_imp))) %>% 
                    
  dplyr::mutate(event=ifelse(duplicates_filtered>1,1,0)) %>% 
  #dplyr::select(hash_key,fech_ing,dup, startdate, enddate,person_days,a_botar,event)
  dplyr::group_by(hash_key) %>% 
  dplyr::mutate(startdate= dplyr::first(fech_ing_num), enddate= dplyr::last(fech_egres_num), 
              person_days=enddate-startdate) %>% 
  dplyr::mutate(person_years=person_days/365.25)%>% 
  dplyr::ungroup() %>% 
  #dplyr::mutate(treat= ifelse(row %in% unlist(CONS_C1_df_dup_JUL_2020_match_top_sel$row),1,0))%>%
  dplyr::mutate(origen_ingreso_mod= dplyr::case_when(grepl("Otro Centro Tratamiento",as.character(origen_ingreso_mod))~"Derivación Asistida", TRUE~as.character(origen_ingreso_mod)))%>%  
  dplyr::mutate(origen_ingreso_mod=factor(origen_ingreso_mod,labels=c('Spontaneous','Assisted Referral','Other','Justice Sector','Health Sector')))  %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #CIE_10 - CORREGIDO
  dplyr::mutate(across(c(dg_trs_psiq_cie_10_or,x2_dg_trs_psiq_cie_10_or,x3_dg_trs_psiq_cie_10_or,x4_dg_trs_psiq_cie_10_or,x5_dg_trs_psiq_cie_10_or),~dplyr::case_when(grepl("En estudio",as.character(.))~0,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~1), .names="dg_{col}"))%>%
  dplyr::mutate(dg_total_cie_10 = base::rowSums(dplyr::select(., dg_dg_trs_psiq_cie_10_or, dg_x2_dg_trs_psiq_cie_10_or, dg_x3_dg_trs_psiq_cie_10_or, dg_x4_dg_trs_psiq_cie_10_or, dg_x5_dg_trs_psiq_cie_10_or), na.rm=T))%>%  
  dplyr::mutate(across(c(dg_trs_psiq_cie_10_or,x2_dg_trs_psiq_cie_10_or,x3_dg_trs_psiq_cie_10_or,x4_dg_trs_psiq_cie_10_or,x5_dg_trs_psiq_cie_10_or),~dplyr::case_when(grepl("En estudio",as.character(.))~1,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~0), .names="instudy_{col}"))%>%
  dplyr::mutate(instudy_total_cie_10 = base::rowSums(dplyr::select(., instudy_dg_trs_psiq_cie_10_or, instudy_x2_dg_trs_psiq_cie_10_or, instudy_x3_dg_trs_psiq_cie_10_or, instudy_x4_dg_trs_psiq_cie_10_or, instudy_x5_dg_trs_psiq_cie_10_or), na.rm=T))%>%
  dplyr::mutate(dg_cie_10_rec=dplyr::case_when(dg_total_cie_10>0 ~2,
                                               instudy_total_cie_10>0 & dg_total_cie_10==0~1,
                                               instudy_total_cie_10>0 & is.na(dg_total_cie_10)~1,
                                               TRUE~0))%>%
  dplyr::mutate(dg_cie_10_rec=as.factor(dg_cie_10_rec))%>%
  dplyr::mutate(dg_cie_10_rec=factor(dg_cie_10_rec,labels=c('Without psychiatric comorbidity','Diagnosis unknown (under study)', 'With psychiatric comorbidity'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
    #DSM-IV -CORREGIDO
dplyr::mutate(across(c(dg_trs_psiq_dsm_iv_or,x2_dg_trs_psiq_dsm_iv_or,x3_dg_trs_psiq_dsm_iv_or,x4_dg_trs_psiq_dsm_iv_or),~dplyr::case_when(grepl("En estudio",as.character(.))~1,grepl("Sin trastorno",as.character(.))~0,is.na(.)~0,TRUE~0),.names = "{col}_mod1b"))%>%
  dplyr::mutate(total_dsm_iv_en_est = base::rowSums(dplyr::select(.,ends_with("_mod1b"))))%>%  
dplyr::mutate(across(c(dg_trs_psiq_dsm_iv_or,x2_dg_trs_psiq_dsm_iv_or,x3_dg_trs_psiq_dsm_iv_or,x4_dg_trs_psiq_dsm_iv_or),~dplyr::case_when(grepl("En estudio",as.character(.),ignore.case = T)~0,grepl("Sin trastorno",as.character(.),ignore.case = T)~0,is.na(.)~0,TRUE~1),.names = "{col}_mod2b"))%>%
  dplyr::mutate(dg_total_dsm_iv = base::rowSums(dplyr::select(.,ends_with("_mod2b"))))%>%  #total_dsm_iv_dg
  
  dplyr::mutate(dg_dsm_iv_rec=dplyr::case_when(dg_total_dsm_iv>0 ~"With psychiatric comorbidity",
                       is.na(dg_total_dsm_iv) & total_dsm_iv_en_est>0~"Diagnosis unknown (under study)",
                       dg_total_dsm_iv==0 & total_dsm_iv_en_est>0~"Diagnosis unknown (under study)",
                       TRUE~"Without psychiatric comorbidity"))%>%
  dplyr::mutate(dg_dsm_iv_rec=as.factor(dg_dsm_iv_rec)) %>% 
  dplyr::select(-ends_with("_mod1b"),-ends_with("_mod2b"),-total_dsm_iv_en_est)%>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
  #MACROZONA
  dplyr::mutate(macrozona=factor(macrozona, labels=c('Center', 'North', 'South'))) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Edad grupos  
  dplyr::mutate(edad_al_ing_grupos=dplyr::case_when(
    edad_al_ing>=50~"50+",
    edad_al_ing>=40~"40-49",
    edad_al_ing>=30~"30-39",
    edad_al_ing>=18~"18-29",
    edad_al_ing<18~"<18",
    TRUE~NA_character_)) %>%
  dplyr::mutate(edad_al_ing_grupos=as.factor(edad_al_ing_grupos)) %>%  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Tipo de programa
  dplyr::mutate(tipo_de_programa_2=dplyr::case_when(grepl("M-", tipo_de_plan_2)~"Women specific",
                                                    grepl("PG-", tipo_de_plan_2)~"General population",
                                                    grepl("LV", tipo_de_plan_2)~"Other")) %>% 
  dplyr::mutate(tipo_de_programa_2=as.factor(tipo_de_programa_2)) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Sex
  dplyr::mutate(sexo_2=factor(sexo_2,labels=c('Men','Women'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Cause of Discharge
  dplyr::mutate(motivodeegreso_mod_imp=factor(motivodeegreso_mod_imp,labels=c('Late Drop-out',
                                                                              'Early Drop-out',
                                                                              'Administrative discharge',
                                                                              'Therapeutic discharge',
                                                                              'Referral to another treatment',
                                                                              'Ongoing treatment',
                                                                              'Death'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #frequency of consumption
  dplyr::mutate(freq_cons_sus_prin=factor(freq_cons_sus_prin,labels=c("1 day a week or more",
                                                                      "2 to 3 days a week",
                                                                      "4 to 6 days a week",
                                                                      "Less than 1 day a week",
                                                                      "Did not use",
                                                                      "Daily"))) %>%     
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #primary substance
  dplyr::mutate(sus_principal_mod=factor(sus_principal_mod,labels=c("Alcohol",
                                                                    "Cocaine hydrochloride",
                                                                    "Marijuana",
                                                                    "Other",
                                                                    "Cocaine paste"))) %>%   
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #starting substance
  #Segunda Sustancia de Inicio(Sólo más frecuentes) Second Starting Substance
  #Tercera Sustancia de Inicio(Sólo más frecuentes) Third Starting Substance
  #Sustancia de Inicio (Sólo más frecuentes)    Starting Substance (Only more frequent)
  dplyr::mutate(sus_ini_mod_pb=dplyr::case_when(grepl("Past",sus_ini_3_mod)~"Paste Base",
                                                grepl("Past",sus_ini_2_mod)~"Paste Base",
                                                grepl("Past",sus_ini_mod)~"Paste Base",
                                                TRUE~NA_character_)) %>%
  dplyr::mutate(sus_ini_mod_oh=dplyr::case_when(grepl("Alcohol",sus_ini_3_mod)~"Alcohol",
                                                grepl("Alcohol",sus_ini_2_mod)~"Alcohol",
                                                grepl("Alcohol",sus_ini_mod)~"Alcohol",
                                                TRUE~NA_character_)) %>% 
  dplyr::mutate(sus_ini_mod_coc=dplyr::case_when(grepl("Coca",sus_ini_3_mod)~"Cocaine hydrochloride",
                                                grepl("Coca",sus_ini_2_mod)~"Cocaine hydrochloride",
                                                grepl("Coca",sus_ini_mod)~"Cocaine hydrochloride",
                                                TRUE~NA_character_)) %>%   
  dplyr::mutate(sus_ini_mod_mar=dplyr::case_when(grepl("Mari",sus_ini_3_mod)~"Marijuana",
                                                grepl("Mari",sus_ini_2_mod)~"Marijuana",
                                                grepl("Mari",sus_ini_mod)~"Marijuana",
                                                TRUE~NA_character_)) %>%   
  dplyr::mutate(sus_ini_mod_otr=dplyr::case_when(grepl("Ot",sus_ini_3_mod)~"Other",
                                                grepl("Ot",sus_ini_2_mod)~"Other",
                                                grepl("Ot",sus_ini_mod)~"Other",
                                                TRUE~NA_character_)) %>%
  dplyr::mutate(sus_ini_mod_mvv=dplyr::case_when(grepl("Paste Base",sus_ini_mod_pb)~"Paste Base",
                                                    grepl("Cocaine hydrochloride",sus_ini_mod_coc)~"Cocaine hydrochloride",
                                                    grepl("Marijuana",sus_ini_mod_mar)~"Marijuana",
                                                    grepl("Alcohol",sus_ini_mod_oh)~"Alcohol",
                                                    grepl("Other",sus_ini_mod_otr)~"Other",
                                                    TRUE~NA_character_)) %>%
  dplyr::mutate(sus_ini_mod_mvv=factor(sus_ini_mod_mvv,labels=c("Alcohol",
                                                                "Cocaine hydrochloride",
                                                                "Marijuana",
                                                                "Other",
                                                                "Cocaine paste"))) %>%   
#  "sus_ini_2_mod"                  "sus_ini_3_mod"                  "sus_ini_mod"
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #escolaridad rec
  dplyr::mutate(escolaridad_rec=factor(escolaridad_rec,labels=c('1-More than high school','2-Completed high school or less','3-Completed primary school or less')))  %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #marital status
  dplyr::mutate(estado_conyugal_2=factor(estado_conyugal_2, labels=c('Married/Shared living arrangements','Separated/Divorced','Single','Widower'))) %>%  
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #type of center (pub/priv)
  dplyr::mutate(tipo_centro=factor(tipo_centro, labels=c('Private','Public'))) %>%
  
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #occupational status (inactive…)
  # Due to the collapse in the databases, many occupational status did not coincide with the occupational category in some users. This is why we decided to consider as employed those users that had an occupational category.
  dplyr::mutate(estatus_ocupacional_rec= dplyr::case_when(!is.na(cat_ocupacional)&!is.na(estatus_ocupacional)~"Empleado",
                                                      TRUE~as.character(estatus_ocupacional)))%>% 
  dplyr::mutate(estatus_ocupacional_rec=factor(estatus_ocupacional_rec, labels=c('Unemployed',
                                                                         'Employed',
                                                                         'Inactive'))) %>%
  dplyr::mutate(cat_ocupacional=factor(cat_ocupacional, labels=c('Salaried',
                                                                 'Self-employed',
                                                                 'Employer',
                                                                 'Unpaid family labour',
                                                                 'Other',
                                                                 'Volunteer worker'))) %>%
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Had children into Residential Treatment
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  dplyr::mutate(hijos_trat_res=ifelse(num_hijos_trat_res_mod>0,1,0)) %>% 
  dplyr::mutate(hijos_trat_res=factor(hijos_trat_res, labels=c('Did not had children in treatment',
                                                              'Had children in treatments'))) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #Cohabitation Status
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  dplyr::mutate(con_quien_vive_rec=factor(con_quien_vive_rec,labels=c("With relatives",
                                                                      "With couple and children",
                                                                      "Other",
                                                                      "Alone",
                                                                      "With children only",
                                                                      "With couple only"))) %>%    
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #physical diagnose
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #
  dplyr::mutate(dg_fis_anemia= ifelse(grepl("Anemia",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_card= ifelse(grepl("Cardiopat",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_in_study= ifelse(grepl("En estudio",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_enf_som= ifelse(grepl("somáticas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_ets= ifelse(grepl("ETS",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_alc= ifelse(grepl("Hepatitis alcohólica",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_b= ifelse(grepl("Hepatitis B",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_hep_cro= ifelse(grepl("Hepatitis crónica",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_inf= ifelse(grepl("Infecciosas relacionadas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_otr_cond_fis_ries_vit= ifelse(grepl("Condiciones de Riesgo Vital",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_otr_cond_fis= ifelse(grepl("condiciones Fisicas Limitantes",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_pat_buc= ifelse(grepl("Bucal",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_pat_ges_intrau= ifelse(grepl("niño intrauterino",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(dg_fis_trau_sec= ifelse(grepl("Traumatismos y secuelas",diagnostico_trs_fisico),1,0))%>%
  dplyr::mutate(otros_pr_sm_abu_sex= ifelse(grepl("Abuso Sexual",otros_probl_at_sm_or),1,0))%>%
  dplyr::mutate(otros_pr_sm_exp_com_sex= ifelse(grepl("omercial Sexual",otros_probl_at_sm_or),1,0))%>%
  dplyr::mutate(otros_pr_sm_otros= ifelse(grepl("Otros",otros_probl_at_sm_or),1,0))%>%
  dplyr::mutate(otros_pr_sm_otros= ifelse(grepl("Prisionalización",otros_probl_at_sm_or),1,otros_pr_sm_otros))%>%
  dplyr::mutate(otros_pr_sm_vif= ifelse(grepl("Violencia Intrafamiliar",otros_probl_at_sm_or),1,0)) %>% 
  
  dplyr::mutate(dg_trs_cons_sus_or= dplyr::case_when(grepl("Consumo Perjudicial",dg_trs_cons_sus_or)~"Hazardous consumption",grepl("Dependencia",dg_trs_cons_sus_or)~"Drug dependence",TRUE~NA_character_)) %>% 
  dplyr::mutate(dg_trs_cons_sus_or=as.factor(dg_trs_cons_sus_or)) %>% 
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
  #route of administration of primary substance
  #:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:    
  dplyr::mutate(via_adm_sus_prin_act= dplyr::case_when(grepl("Intranasal",via_adm_sus_prin_act)~"Intranasal (aspiración de polvo por la nariz)",TRUE~as.character(via_adm_sus_prin_act))) %>%   
      dplyr::mutate(via_adm_sus_prin_act=factor(via_adm_sus_prin_act,labels=c("Smoked or Pulmonary Aspiration",
                                                                        "Intranasal (powder aspiration)",
                                                                        "Injected Intravenously or Intramuscularly",
                                                                        "Oral (drunk or eaten)",
                                                                        "Other"))) %>%   
  #dplyr::rename("cnt_mod_cie_10_or_2"="dg_total_cie_10") %>% 
  dplyr::select(-sus_ini_mod_pb, -sus_ini_mod_oh, -sus_ini_mod_coc, -sus_ini_mod_mar, -sus_ini_mod_otr,
                -dg_dg_trs_psiq_cie_10_or,-dg_x2_dg_trs_psiq_cie_10_or,-dg_x3_dg_trs_psiq_cie_10_or,-dg_x4_dg_trs_psiq_cie_10_or,-dg_x5_dg_trs_psiq_cie_10_or,-instudy_dg_trs_psiq_cie_10_or,-instudy_x2_dg_trs_psiq_cie_10_or,-instudy_x3_dg_trs_psiq_cie_10_or,-instudy_x4_dg_trs_psiq_cie_10_or,-instudy_x5_dg_trs_psiq_cie_10_or,-instudy_total_cie_10) %>% 
  dplyr::mutate_at(vars(starts_with("otros_pr_sm_"),
                                   starts_with("dg_fis_")),~factor(., labels=c("Absence", "Presence")))%>%
  
  #_#_#_#_#_#_#_#_#_#_#_
#Corrección número de diagnósticos cie-10 dsm-iv
  #_#_#_#_#_#_#_#_#_#_#_
    dplyr::mutate(cnt_mod_cie_10_or= dplyr::case_when(as.character(cie_10)=="Diagnosticado/a (uno en estudio)"~cnt_mod_cie_10_or-1, is.na(dg_trs_psiq_cie_10_or)~0, TRUE~cnt_mod_cie_10_or)) %>% 
      dplyr::mutate(cnt_mod_dsm_iv_or= dplyr::case_when(as.character(dsm_iv)=="Diagnosticado/a (uno en estudio)"~cnt_mod_dsm_iv_or-1, is.na(dg_trs_psiq_dsm_iv_or)~0, TRUE~cnt_mod_dsm_iv_or)) %>% 
    #_#_#_#_#_#_#_#_#_#_#_
  #sumrows de los diagnósticos, 
#otros_pr_sm_abu_sex, otros_pr_sm_exp_com_sex, otros_pr_sm_otros, otros_pr_sm_vif
      dplyr::mutate(across(c(dg_fis_anemia, dg_fis_card,  dg_fis_enf_som, dg_fis_ets, dg_fis_hep_alc, dg_fis_hep_b, dg_fis_hep_cro, dg_fis_inf, dg_fis_otr_cond_fis_ries_vit, dg_fis_otr_cond_fis, dg_fis_pat_buc, dg_fis_pat_ges_intrau, dg_fis_trau_sec),~as.numeric(.)-1, .names="num_dg_fis_{col}"))%>%
      dplyr::mutate(cnt_diagnostico_trs_fisico= base::rowSums(dplyr::select(., num_dg_fis_dg_fis_anemia,  num_dg_fis_dg_fis_card,  num_dg_fis_dg_fis_enf_som, num_dg_fis_dg_fis_ets, num_dg_fis_dg_fis_hep_alc, num_dg_fis_dg_fis_hep_b, num_dg_fis_dg_fis_hep_cro, num_dg_fis_dg_fis_inf, num_dg_fis_dg_fis_otr_cond_fis_ries_vit, num_dg_fis_dg_fis_otr_cond_fis, num_dg_fis_dg_fis_pat_buc, num_dg_fis_dg_fis_pat_ges_intrau, num_dg_fis_dg_fis_trau_sec)))%>% 
      dplyr::select(-starts_with("num_dg_fis")) %>% 
#_#_#_#_#_#_#_#_#_#_#_
  #compromiso biospicosocial  
#_#_#_#_#_#_#_#_#_#_#_
    dplyr::mutate(compromiso_biopsicosocial=dplyr::case_when(as.character(compromiso_biopsicosocial)=="1-Leve"~'1-Mild',
                                                           as.character(compromiso_biopsicosocial)=="2-Moderado"~'2-Moderate',
                                                           as.character(compromiso_biopsicosocial)=="3-Severo"~'3-Severe',
                                                           TRUE~NA_character_)) %>% 
  dplyr::mutate(compromiso_biopsicosocial=factor(compromiso_biopsicosocial)) %>% 
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:  
#Corrección Edad Inicio Sus Principal  
dplyr::mutate(edad_ini_sus_prin_grupos=ifelse(edad_ini_sus_prin>=25,">=25",
                                            ifelse(edad_ini_sus_prin>18,"19-24",
                                            ifelse(edad_ini_sus_prin>15,"16-18",
                                            ifelse(edad_ini_sus_prin>0,"<=15",
                                            NA_character_)))))%>% 
dplyr::mutate(edad_ini_sus_prin_grupos=as.factor(edad_ini_sus_prin_grupos)) %>%
#_#_#_#_#_#_#_#_#_#_#_
  #corrección tenencia de la vivienda.
#_#_#_#_#_#_#_#_#_#_#_
  dplyr::mutate(tenencia_de_la_vivienda_mod=dplyr::case_when(as.character(tenencia_de_la_vivienda_mod)=="Ocupación Irregularrregular"~'Ocupación Irregular',
                                                             TRUE~as.character(tenencia_de_la_vivienda_mod))) %>%
  dplyr::mutate(tenencia_de_la_vivienda_mod=factor(tenencia_de_la_vivienda_mod)) %>% 
#_#_#_#_#_#_#_#_#_#_#_
  #Corrección de otros problemas de salud mental
#_#_#_#_#_#_#_#_#_#_#_
      dplyr::mutate(across(c(otros_pr_sm_abu_sex, otros_pr_sm_exp_com_sex, otros_pr_sm_otros, otros_pr_sm_vif),~as.numeric(.)-1, .names="num_dg_otros_pr_{col}"))%>%
     dplyr::mutate(cnt_otros_probl_at_sm_or= base::rowSums(dplyr::select(., num_dg_otros_pr_otros_pr_sm_abu_sex, num_dg_otros_pr_otros_pr_sm_exp_com_sex, num_dg_otros_pr_otros_pr_sm_otros, num_dg_otros_pr_otros_pr_sm_vif))) %>% 
      dplyr::select(-starts_with("num_dg_otros_pr_")) %>% 
#_#_#_#_#_#_#_#_#_#_#_
# 2020-11-15: sacar el último tratamiento acumulado que se replica, para no tener errores con la estimación de los four
  dplyr::mutate(cum_diff_bet_treat_2= dplyr::case_when(!is.na(cum_diff_bet_treat_2) & duplicates_filtered==2~NA_real_,TRUE~cum_diff_bet_treat_2)) %>% 
  dplyr::mutate(cum_diff_bet_treat_3= dplyr::case_when(!is.na(cum_diff_bet_treat_3) & duplicates_filtered==3~NA_real_,TRUE~cum_diff_bet_treat_3)) %>% 
  dplyr::mutate(cum_diff_bet_treat_4= dplyr::case_when(!is.na(cum_diff_bet_treat_4) & duplicates_filtered==4~NA_real_,TRUE~cum_diff_bet_treat_4)) %>% 
  dplyr::mutate(cum_diff_bet_treat_5= dplyr::case_when(!is.na(cum_diff_bet_treat_5) & duplicates_filtered==5~NA_real_,TRUE~cum_diff_bet_treat_5)) %>% 
  dplyr::mutate(cum_diff_bet_treat_6= dplyr::case_when(!is.na(cum_diff_bet_treat_6) & duplicates_filtered==6~NA_real_,TRUE~cum_diff_bet_treat_6)) %>% 
  dplyr::mutate(cum_diff_bet_treat_7= dplyr::case_when(!is.na(cum_diff_bet_treat_7) & duplicates_filtered==7~NA_real_,TRUE~cum_diff_bet_treat_7)) %>% 
  dplyr::mutate(cum_diff_bet_treat_8= dplyr::case_when(!is.na(cum_diff_bet_treat_8) & duplicates_filtered==8~NA_real_,TRUE~cum_diff_bet_treat_8)) %>% 
  dplyr::mutate(cum_diff_bet_treat_9= dplyr::case_when(!is.na(cum_diff_bet_treat_9) & duplicates_filtered==9~NA_real_,TRUE~cum_diff_bet_treat_9)) %>% 
  dplyr::mutate(cum_diff_bet_treat_10= dplyr::case_when(!is.na(cum_diff_bet_treat_10) & duplicates_filtered==10~NA_real_,TRUE~cum_diff_bet_treat_10)) %>% 
  
  assign("CONS_C1_df_dup_SEP_2020",., envir = .GlobalEnv)


Adding another criteria for classifying occupations

We repeated the task of collapsing the occupational condition (condicion_ocupacional) into an occupational status composed of three categories: Employed, Unemployed and Inactive made on Duplicatates process (1st deduplication stage). However, many values of the different rows may be different due to some changes in the stage of collapsing continuous treatments (Desc4). For this, we had to replicate this steps in order to track down the value of the largest intermediate treatment to generate a occupational condition following a different criteria.


#Primero es necesario determinar qué tratamiento fue más largo, para volver a recuperar el valor de la `condicion_ocupacional` presente en las primeras bases de datos.

#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#¿cómo determinar qué tratamiento fue el más largo?
CONS_C1_JUN_2020_row_sig_row<-
  CONS_C1_df_dup_JUN_2020%>%
  #dplyr::filter(!is.na(diff_bet_treat))%>% #31,609, no hay NAs en derivación, los que son NA son 0.
  dplyr::mutate(filter_complex= dplyr::case_when(!is.na(diff_bet_treat) & diff_bet_treat<45 & as.character(motivoegreso_derivacion)=="Referral"~1,TRUE~0))%>%
  dplyr::arrange(hash_key)%>%
  dplyr::group_by(hash_key)%>%
  dplyr::mutate(sig_row=lag(row), sig_fech_egres_imp=lag(fech_egres_imp),sig_motivoegres_ref=lag(motivoegreso_derivacion),n_por_hash=n())%>%
  ungroup()%>%
  dplyr::filter(filter_complex==1)%>%
  # dplyr::select(row,hash_key,fech_ing,fech_egres_imp,motivoegreso_derivacion,obs_cambios,diff_bet_treat,sig_row,n_por_hash) %>% View() #0007678b8b35fa0961d1e8110fbf9620 
  dplyr::select(row,sig_row)  

CONS_C1_JUN_2020_cont_treat<-
  CONS_C1_df_dup_JUN_2020%>%
  dplyr::filter(row %in% unlist(c(CONS_C1_JUN_2020_row_sig_row$row,CONS_C1_JUN_2020_row_sig_row$sig_row)))%>%
  dplyr::arrange(hash_key,desc(fech_ing))%>%
  dplyr::mutate(filter_complex_anterior= dplyr::case_when(!is.na(lag(diff_bet_treat)) & lag(diff_bet_treat)<45 & as.character(lag(motivoegreso_derivacion))=="Referral"~1,TRUE~0))%>%
  dplyr::mutate(filter_complex= dplyr::case_when(!is.na(diff_bet_treat) & diff_bet_treat<45 & as.character(motivoegreso_derivacion)=="Referral"~1,TRUE~0))%>%
  dplyr::ungroup() %>% 
  dplyr::group_by(hash_key)%>%
  dplyr::slice_max(dias_trat) %>% 
  dplyr::ungroup() %>% 
  dplyr::select(row,hash_key,dias_trat,cat_ocupacional,estatus_ocupacional) %>% 
  dplyr::mutate(row_larg_treat=row)
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:
#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:#:

#Se hace el pareamiento de la base de datos anterior con la actual, y se elije de una fila con los registros colapsados, aquel registro que contiene más días de tratamiento. Para eso me traigo los valores de condición ocupacional de la base de datos original, para cada cada `row`. De ellos, dejo aquel valor de la fila que tenga más días de tratamiento  (considerando que ese fue el criterio de selección para esta variable de acuerdo a la Fig. 5 de Duplicates4). Hubo hasta 7 eventos que se colapsaron en uno.
#Ver qué row corresponde al tratamiento más largo
CONS_C1_df_dup_SEP_2020 %>% 
  #dplyr::mutate(row=as.character(row)) %>%
  dplyr::mutate(row_cont_entries2=row_cont_entries) %>% 
  tidyr::separate(row_cont_entries2,into=paste0("rows",1:7),sep="; ", convert=T) %>% #se supone que tiene 7 distintos
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by="row")%>% 
  dplyr::rename("condicion_ocupacional_row_all"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows1"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row1"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows2"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row2"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows3"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row3"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows4"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row4"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows5"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row5"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows6"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row6"="Condicion.Ocupacional") %>% 
  dplyr::left_join(dplyr::select(CONS_C1,row,Condicion.Ocupacional),by=c("rows7"="row"))%>% 
  dplyr::rename("condicion_ocupacional_row7"="Condicion.Ocupacional") %>% 
  
  dplyr::left_join(CONS_C1_JUN_2020_cont_treat[,c("row","row_larg_treat")],by="row")%>% 
  dplyr::mutate(condicion_ocupacional_larg_treat=dplyr::case_when(row_larg_treat==rows1~condicion_ocupacional_row1,
                                                                  row_larg_treat==rows2~condicion_ocupacional_row2,
                                                                  row_larg_treat==rows3~condicion_ocupacional_row3,
                                                                  row_larg_treat==rows4~condicion_ocupacional_row4,
                                                                  row_larg_treat==rows5~condicion_ocupacional_row5,
                                                                  row_larg_treat==rows6~condicion_ocupacional_row6,
                                                                  row_larg_treat==rows7~condicion_ocupacional_row7)) %>% 
  dplyr::mutate(condicion_ocupacional_row_all2=ifelse(!is.na(condicion_ocupacional_larg_treat),
                                                     condicion_ocupacional_larg_treat,condicion_ocupacional_row_all)) %>% 
  dplyr::select(-condicion_ocupacional_row1,-condicion_ocupacional_row2,-condicion_ocupacional_row3,-condicion_ocupacional_row4,
                -condicion_ocupacional_row5,-condicion_ocupacional_row6,-condicion_ocupacional_row7,-rows1,-rows2,-rows3,-rows4,
                -rows5,-rows6,-rows7,-row_larg_treat) %>%
  dplyr::mutate(condicion_ocupacional_corr=dplyr::case_when(
    #categorizados como inactive en estatus_ocupacional_rec
    grepl("Buscando trabajo por primera vez",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"Looking for a job for the first time",
    grepl("Cesante",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"Unemployed",
    grepl("No busca Trabajo",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"Not seeking for work",
    grepl("Sin actividad",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"No activity",
    grepl("Trabajando actualmente",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"Employed",
    grepl("Trabajando y estudiando",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Inactive",estatus_ocupacional_rec)~"Employed",
  #categorizados como unemployed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Unemployed",
    grepl("Estudiando sin trabajar",condicion_ocupacional_row_all2) & is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",
    grepl("Incapacitado permanente para trabajar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",
    grepl("Otra razón",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",
    grepl("Pensionado o jubilado sin trabajar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",
    grepl("Rentista",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Inactive",  
    grepl("No busca Trabajo",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Not seeking for work",
    grepl("Sin actividad",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"No activity",
    grepl("Buscando trabajo por primera vez",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Looking for a job for the first time",
    grepl("Trabajando actualmente",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Unemployed",estatus_ocupacional_rec)~"Employed",
  #categorizados como employed en estatus_ocupacional_rec
  #categorizados como Salaried en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed", #--> debiese no ser cat_ocupacional Salaried
    grepl("Estudiando sin trabajar",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Salaried
    grepl("Incapacitado permanente para trabajar",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Salaried  
    grepl("Otra razón",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Salaried
    grepl("Pensionado o jubilado sin trabajar",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Salaried
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Salaried
    grepl("No busca Trabajo",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Not seeking for work", #--> debiese no ser cat_ocupacional Salaried
    grepl("Sin actividad",condicion_ocupacional_row_all2)& grepl("Salaried",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity", #--> debiese no ser cat_ocupacional Salaried
  #categorizados como Employer en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Buscando trabajo por primera vez",condicion_ocupacional_row_all2)& grepl("Self-employed",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Looking for a job for the first time", #--> debiese no ser cat_ocupacional Self-employed
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Self-employed",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed", #--> debiese no ser cat_ocupacional Self-employed
    grepl("Estudiando sin trabajar|Incapacitado permanente para trabajar|Otra razón|Pensionado o jubilado sin trabajar|Quehaceres del hogar",condicion_ocupacional_row_all2)& grepl("Self-employed",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Self-employed
    grepl("No busca Trabajo",condicion_ocupacional_row_all2)& grepl("Self-employed",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Not seeking for work",
    grepl("Sin actividad",condicion_ocupacional_row_all2)& grepl("Self-employed",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity",
  #categorizados como Employer en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Employer",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed",
    grepl("Estudiando sin trabajar",condicion_ocupacional_row_all2)& grepl("Employer",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Otra razón",condicion_ocupacional_row_all2)& grepl("Employer",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& grepl("Employer",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Sin actividad",condicion_ocupacional_row_all2)& grepl("Employer",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity",
  #categorizados como Unpaid family labour en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Unpaid family labour",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed",
  #categorizados como Other en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Sin actividad",condicion_ocupacional_row_all2)& grepl("Other",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity",
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Other",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed",
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& grepl("Other",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer  
  #categorizados como Volunteer worker en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& grepl("Volunteer worker",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed",
    grepl("No busca Trabajo",condicion_ocupacional_row_all2)& grepl("Volunteer worker",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Not seeking for work",
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& grepl("Volunteer worker",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive",
    grepl("Sin actividad",condicion_ocupacional_row_all2)& grepl("Volunteer worker",cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity",
  #categorizados como NA en cat__ocupacional y employed en estatus_ocupacional_rec
    grepl("Cesante",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Unemployed",
    grepl("Estudiando sin trabajar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Pensionado o jubilado sin trabajar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Quehaceres del hogar",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"Inactive", #--> debiese no ser cat_ocupacional Employer
    grepl("Sin actividad",condicion_ocupacional_row_all2)& is.na(cat_ocupacional) & grepl("Employed",estatus_ocupacional_rec)~"No activity",
  TRUE~as.character(estatus_ocupacional_rec))) %>% 
  assign("CONS_C1_df_dup_SEP_2020_joel",., envir=.GlobalEnv)

#generar cat_ocupacional_corr
#:#:#:#:#:#:#:#:#:#:#:
CONS_C1_df_dup_SEP_2020_joel %>% 
    dplyr::mutate(cat_ocupacional_corr=dplyr::case_when(
    grepl("Employed",condicion_ocupacional_corr)~as.character(cat_ocupacional),
    TRUE~NA_character_)) %>% 
  dplyr::select(-condicion_ocupacional_row_all2,-condicion_ocupacional_larg_treat,-condicion_ocupacional_row_all) %>% 
  dplyr::mutate(cat_ocupacional_corr=factor(cat_ocupacional_corr),condicion_ocupacional_corr= factor(condicion_ocupacional_corr))%>% 
assign("CONS_C1_df_dup_SEP_2020",., envir=.GlobalEnv)
#De ahí generar un cat_ocupacional_corr que converse con condicion_ocupacional_row_all2 y con esttus_ocupacional_corr  

#CONS_C1_df_dup_SEP_2020_joel %>%
#    dplyr::filter(condicion_ocupacional_row_all2=="Estudiando sin trabajar",estatus_ocupacional_rec=="Employed",condicion_ocupacional_corr=="Employed") %>% 
#    dplyr::select(condicion_ocupacional_row_all2, cat_ocupacional, estatus_ocupacional_rec, condicion_ocupacional_corr)
  
#NO CALZAN PORQUE PUSE UNA OPCIÖN DE QUE SI HABÏA VACIO EN UNO; LO REEMPLAZARA
#CONS_C1_df_dup_SEP_2020_joel %>%  janitor::tabyl(cat_ocupacional,condicion_ocupacional_larg_treat)    
#CONS_C1_df_dup_SEP_2020_joel %>%  janitor::tabyl(estatus_ocupacional,condicion_ocupacional_larg_treat)
#CONS_C1_df_dup_SEP_2020_joel %>%  janitor::tabyl(condicion_ocupacional_row_all2,cat_ocupacional)  


Health conditions & readmissions

To describe the incidence rate of SUD treatment readmissions by type of health conditions, we recoded the different variables of interest into pairs of different groups by each variable.


CONS_C1_df_dup_SEP_2020_irrs_health<-  
CONS_C1_df_dup_SEP_2020 %>% 
    dplyr::filter(!is.na(a_botar)) %>% #nrow() 85,048, QUEDARME SÓLO CON LOS CASOS QUE SON IS.NA
      #dplyr::mutate(event=factor(event,labels=c("W/o Readmissions","W/Readmissions"))) %>% 
    #%>% janitor::tabyl(event)

       # dg_cie_10_rec cnt_mod_cie_10_or cnt_otros_probl_at_sm_or
      dplyr::mutate(cnt_diagnostico_trs_fisico_irr=dplyr::case_when(cnt_diagnostico_trs_fisico==0 & dg_fis_in_study!="Presence"~0,
                                                                    cnt_diagnostico_trs_fisico>0~1,
                                                                    dg_fis_in_study=="Presence"~NA_real_)) %>% 
      dplyr::mutate(dg_fis_in_study_irr=dplyr::case_when(dg_fis_in_study=="Presence"~1,
                                                         diagnostico_trs_fisico=="Sin trastorno"~0,
                                                         TRUE~NA_real_)) %>% 
      dplyr::mutate(cnt_mod_cie_10_or_irr=dplyr::case_when(cnt_mod_cie_10_or==0 & dg_cie_10_rec!="Diagnosis unknown (under study)"~0,
                                                           cnt_mod_cie_10_or>0~1,
                                                           TRUE~NA_real_)) %>% 
      dplyr::mutate(cnt_mod_cie_10_or_in_study_irr=dplyr::case_when(as.character(dg_cie_10_rec)=="Diagnosis unknown (under study)"~1,
                                                                    as.character(dg_cie_10_rec)=="Without psychiatric comorbidity"~0,
                                                                    TRUE~NA_real_)) %>% 

      dplyr::mutate(cnt_otros_probl_at_sm_or_irr=dplyr::case_when(cnt_otros_probl_at_sm_or==0~0,
                                                                  cnt_otros_probl_at_sm_or>0~1)) %>% 
      dplyr::mutate(dg_trs_cons_sus_or_irr=dplyr::case_when(dg_trs_cons_sus_or=="Drug dependence"~1,
                                                            dg_trs_cons_sus_or=="Hazardous consumption"~0,
                                                            TRUE ~NA_real_)) %>% 
      dplyr::mutate(cnt_diagnostico_trs_fisico_irr=factor(cnt_diagnostico_trs_fisico_irr),
                    dg_fis_in_study_irr=as.factor(dg_fis_in_study_irr),
                    cnt_mod_cie_10_or_irr=as.factor(cnt_mod_cie_10_or_irr),
                    cnt_mod_cie_10_or_in_study_irr= as.factor(cnt_mod_cie_10_or_in_study_irr),
                    cnt_otros_probl_at_sm_or_irr=as.factor(cnt_otros_probl_at_sm_or_irr),
                    dg_trs_cons_sus_or_irr=as.factor(dg_trs_cons_sus_or_irr))


# CONS_C1_df_dup_SEP_2020_irrs_health%>% janitor::tabyl(cnt_diagnostico_trs_fisico_irr)
#label(CONS_C1_df_dup_SEP_2020_prev4_explore$dg_fis_anemia) <- "Physical Dg. Anemia"
#   cnt_mod_cie_10_or cnt_otros_probl_at_sm_or

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

irrs_tr_fis<-irrs(x="cnt_diagnostico_trs_fisico_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")
irrs_tr_fis_in_study<-irrs(x="dg_fis_in_study_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")

irrs_cie_10<-irrs(x="cnt_mod_cie_10_or_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")
irrs_cie_10_in_study<-irrs(x="cnt_mod_cie_10_or_in_study_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")

irrs_otros_pr_sm<-irrs(x="cnt_otros_probl_at_sm_or_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")
irrs_dg_trs_cons_sus_or<-irrs(x="dg_trs_cons_sus_or_irr" ,db="CONS_C1_df_dup_SEP_2020_irrs_health")


The incidence rate of readmission was 0.98 (95% IC 0.93-1.04) in users that had at least one physical condition, compared to users that did not have a physical condition at baseline (p = 0.524).


Figure 10. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Diagnostic at Baseline

Figure 10. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Diagnostic at Baseline


Compared to users with no physical diagnosis at baseline, users that had a physical condition in study had an incidence rate of readmission of 1.07 (95% IC 1.03-1.1, p = 0.0001).


Figure 11. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Condition In Study at Baseline

Figure 11. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Condition In Study at Baseline


Compared to users with no psychiatric diagnosis at baseline, users that had a psychiatric condition in study had an incidence rate of readmission of 1.21 (95% IC 1.17-1.24, p = 0.0000).


Figure 12. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Psychiatric Condition at Baseline

Figure 12. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Psychiatric Condition at Baseline


Compared to users with no psychiatric diagnosis at baseline, users that had a psychiatric condition in study had an incidence rate of readmission of 1.7 (95% IC 1.63-1.77, p = 0.0000).


Figure 13. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Diagnostic In Study at Baseline

Figure 13. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Physical Diagnostic In Study at Baseline


Compared to users with no other problems related to mental health conditions worth clinical surveillance, users that had other problems related to mental health conditions had an incidence rate of readmission of 1.08 (95% IC 1.04-1.11, p = 0.0000).


Figure 14. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Other Problems Related to Mental Health at Baseline

Figure 14. Cum. Hazards to Experience Readmission to SUD Treatment, by Presence of Other Problems Related to Mental Health at Baseline


Compared to users with hazardous consumption, users with a drug dependence had an incidence rate of readmission of 1.17 (95% IC 1.13-1.21, p = 0.0000).


Figure 15. Cum. Hazards to Experience Readmission to SUD Treatment, by Substance Use Disorder at Baseline

Figure 15. Cum. Hazards to Experience Readmission to SUD Treatment, by Substance Use Disorder at Baseline


#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_  

  metadata(CONS_C1_df_dup_SEP_2020)$name <- "Agreement 1 SENDA"
  metadata(CONS_C1_df_dup_SEP_2020)$description <- "Information About Agreement 1 of SENDA and MINSAL. Contians information about treatments.(*) Intermediate events are collapsed and concatenated in some variables; Criteria to select values from entries that were collapsed into single treatments: Wide format(a),Maximum/Last value(b), Minimum/First value(c), Kept more vulnerable category(d), Same value(e), Largest treatment(f), Favored dgs.-a(g), Sum values(h). In case of 'tipo_de_plan_2','dias_treat_imp_sin_na', 'diff_bet_treat', 'cum_dias_trat_sin_na', 'mean_cum_dias_trat_sin_na' & 'cum_diff_bet_treat', the first variable, 10 variables were generated for each variable and represents each treatment of user, since the first(1) to the last(10)"

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_  
  
codebook::var_label(CONS_C1_df_dup_SEP_2020) <- list(
row= 'Numerador de los eventos presentes en la Base de Datos (Último registro)/Events in the Dataset (Last Entry)',
row_cont_entries= 'Numerador de los eventos presentes en la Base de Datos(*)/Events in the Dataset(*)',
hash_key= 'Codificación del RUN/Masked Identifier (RUN)',
hash_rut_completo= 'HASH alternativo, en el escenario en que se asuma que el individuo al que se le codificó el RUN presente mayor edad/Alternative HASH-Key',
id= 'Codigo Identificación de SENDA/SENDA ID',
id_mod= 'ID de SENDA para Presentación en Página Web (enmascara caracteres 5 y 6)/SENDA ID (mask characters 5 & 6)',
fech_ing= 'Fecha de Ingreso a Tratamiento (Primera Entrada)/Date of Admission to Treatment (First Entry)',
fech_egres_imp= 'Fecha de Egreso (Imputados KNN & Lógico) del Último Registro(b)/Date of Discharge (Imputed KNN & Logic) of the Last Entry(b)',
tipo_de_plan_2= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry',
tipo_de_plan_2_largest_treat= 'Tipo de Plan del Registro Más Largo entre entradas intermedias(f)/Type of Plan of the Largest Entry Among Intermediate Entries(f)',
tipo_de_plan_2_concat_a= 'Tipo de Plan(*)/Type of Plan(*)', 
tipo_de_programa_2= 'Tipo de Programa del Registro Más Largo entre Entradas Intermedias/Type of Program of the Largest Entry Among Intermediate Entries',
id_centro= 'ID de Centro(b)/Treatment center ID(b)',
nombre_centro= 'Nombre del Centro de Tratamiento(*)/Treatment Center(*)',
id_centro_concat_a= 'ID de Centro(*)/Treatment center ID(*)',
tipo_centro= 'Tipo de Centro del Último Registro/Type of Center of the Last Entry',
servicio_de_salud= 'Servicio de Salud(*)/Health Service(*)',
senda= 'SENDA del Último Registro/SENDA of the Last Entry',
numero_de_hijos_mod= 'Número de Hijos (Valor Max.)/Number of Children (Max. Value)',
num_hijos_trat_res_mod= 'Número de Hijos para Ingreso a Tratamiento Residencial del Último Registro/Number of Children to Residential Treatment of the Last Entry',
tipo_centro_derivacion= 'Tipo de Centro al que el Usuario es Derivado del Último Registro(b)/Type of Center of Derivation of the Last Entry(b)',
motivodeegreso_mod_imp= 'Motivo de Egreso (con abandono temprano y tardío)(Imputados KNN & Lógico) del Último Registro(b)/Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b)',
macrozona= "Macrozona del Centro del Último Registro(b)/Macrozones of the Center of the Last Entry(b)",
nombre_region= "Región del Centro del Último Registro(b)/Chilean Region of the Center of the Last Entry(b)",
comuna_residencia_cod= "Comuna de Residencia del Último Registro(b)/Municipality or District of Residence of the Last Entry(b)",
fecha_ingreso_a_convenio_senda= 'Fecha de Ingreso a Convenio SENDA (aún no formateada como fecha) (Primera Entrada)/Date of Admission to SENDA Agreement (First Entry)',
identidad_de_genero= 'Identidad de Género (Último Registro)(b)/Gender Identity (Last Entry)(b)',
edad_al_ing= 'Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry)',
origen_ingreso_mod= 'Origen de Ingreso (Primera Entrada)/Motive of Admission to Treatment (First Entry)',
x_se_trata_mujer_emb= 'Mujer Embarazada al Ingreso (d)/Pregnant at Admission (d)',
compromiso_biopsicosocial= 'Compromiso Biopsicosocial(d)/Biopsychosocial Involvement(d)',
dg_global_nec_int_soc_or= 'Diagnóstico Global de Necesidades de Integración Social (Al Ingreso)(d)/Global Diagnosis of Social Integration (At Admission)(d)',
dg_nec_int_soc_cap_hum_or= 'Diagnóstico de Necesidades de Integración Social en Capital Humano (Al Ingreso)(d)/Global Diagnosis of Social Integration in Human Capital (At Admission)(d)',
dg_nec_int_soc_cap_fis_or= 'Diagnóstico de Necesidades de Integración Social en Capital Físico (Al Ingreso)(d)/Global Diagnosis of Social Integration in Physical Capital (At Admission)(d)',
dg_nec_int_soc_cap_soc_or= 'Diagnóstico de Necesidades de Integración Social en Capital Social (Al Ingreso)(d)/Global Diagnosis of Social Integration in Social Capital (At Admission)(d)',
usuario_tribunal_trat_droga= 'Usuario de modalidad Tribunales de Tratamiento de Drogas(d)/User of Drug Treatment Courts Modality(d)',
evaluacindelprocesoteraputico= 'Evaluación del Proceso Terapéutico(d)/Evaluation of the Therapeutic Process(d)',
eva_consumo= 'Evaluación al Egreso Respecto al Patrón de consumo(d)/Evaluation at Discharge regarding to Consumption Pattern(d)',
eva_fam= 'Evaluación al Egreso Respecto a Situación Familiar(d)/Evaluation at Discharge regarding to Family Situation(d)',
eva_relinterp= 'Evaluación al Egreso Respecto a Relaciones Interpersonales(d)/Evaluation at Discharge regarding to Interpersonal Relations(d)',
eva_ocupacion= 'Evaluación al Egreso Respecto a Situación Ocupacional(d)/Evaluation at Discharge regarding to Occupational Status(d)',
eva_sm= 'Evaluación al Egreso Respecto a Salud Mental(d)/Evaluation at Discharge regarding to Mental Health(d)',
eva_fisica= 'Evaluación al Egreso Respecto a Salud Física(d)/Evaluation at Discharge regarding to Physical Health(d)',
eva_transgnorma= 'Evaluación al Egreso Respecto a Trasgresión a la Norma Social(d)/Evaluation at Discharge regarding to Transgression to the Norm(d)',
dg_global_nec_int_soc_or_1= 'Diagnóstico Global de Necesidades de Integración Social (Al Egreso)(d)/Global Diagnosis of Social Integration (At Discharge)(d)',
dg_nec_int_soc_cap_hum_or_1= 'Diagnóstico de Necesidades de Integración Social en Capital Humano (Al Egreso)(d)/Global Diagnosis of Social Integration in Human Capital (At Discharge)(d)',
dg_nec_int_soc_cap_fis_or_1= 'Diagnóstico de Necesidades de Integración Social en Capital Físico (Al Egreso)(d)/Global Diagnosis of Social Integration in Physical Capital (At Discharge)(d)',
dg_nec_int_soc_cap_soc_or_1= 'Diagnóstico de Necesidades de Integración Social en Capital Social (Al Egreso)(d)/Global Diagnosis of Social Integration in Social Capital (At Discharge)(d)',
tiene_menores_de_edad_a_cargo= 'Menores de Edad A Cargo(d)/Minor Dependants(d)',
ha_estado_embarazada_egreso= '¿Ha estado embarazada? (al Egreso)(d)/Have you been Pregnant (at Discharge)(d)',
discapacidad= 'Presenta Discapacidad(d)/Disability(d)',
opcion_discapacidad= 'Origen de Discapacidad(d)/Cause of Disability(d)',
escolaridad= 'Escolaridad: Nivel Eduacional(d)/Educational Attainment(d)',
escolaridad_rec= 'Escolaridad: Nivel Eduacional(d) Normalizado a Progresión de Tratamientos/Educational Attainment(d) & Normalized Following Progression of Treatments',
edad_al_ing_grupos= 'Edad a la Fecha de Ingreso a Tratamiento en Grupos(c)/Age at Admission to Treatment In Groups(c)',
nacionalidad= 'Nacionalidad/Nationality',
sexo_2= 'Sexo Usuario/Sex of User',
embarazo= 'Embarazo al Ingreso(c)/Pregnant at Admission(c)',
fech_nac= 'Fecha de Nacimiento/Date of Birth',
edad_ini_cons= 'Edad de Inicio de Consumo/Age of Onset of Drug Use',
edad_ini_sus_prin=  'Edad de Inicio de Consumo Sustancia Principal/Age of Onset of Drug Use of Primary Substance',
edad_ini_sus_prin_grupos=  'Edad de Inicio de Consumo Sustancia Principal (en Grupos)/Age of Onset of Drug Use of Primary Substance (in Groups)',
estado_conyugal_2= 'Estado Conyugal/Marital Status',
edad_grupos= 'Edad agrupada/Age in groups',
freq_cons_sus_prin= 'Frecuencia de Consumo de la Sustancia Principal (30 días previos a la admisión)(f)/Frequency of Consumption of the Primary or Main Substance (30 days previous to admission)(f)',
via_adm_sus_prin_act= 'Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f)',
etnia_cor= 'Etnia/Ethnic Group',
nacionalidad_2= 'Segunda Nacionalidad/Second Nationality',
etnia_cor_2= 'Etnia (2)/Second Ethnic Group',
sus_ini_2_mod= 'Segunda Sustancia de Inicio(Sólo más frecuentes)/Second Starting Substance',
sus_ini_3_mod= 'Tercera Sustancia de Inicio(Sólo más frecuentes)/Third Starting Substance',
sus_ini_mod= "Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent)",
con_quien_vive= 'Persona con la que vive el Usuario(f)/People that Share Household with the User (Cohabitation Status)(f)',
con_quien_vive_rec= 'Persona con la que vive el Usuario (Recodificada)(f)/People that Share Household with the User (Cohabitation Status)(Recoded)(f)',
estatus_ocupacional= 'Condición Ocupacional(f)/Occupational Status(f)',
estatus_ocupacional_rec= 'Condición Ocupacional, corregido por categoría(f)/Occupational Status, corrected for categories(f)',
cat_ocupacional= 'Categoría Ocupacional(f)/Occupational Category(f)',
sus_principal_mod= 'Sustancia Principal de Consumo (Sólo más frecuentes)(f)/Primary or Main Substance of Consumption at Admission (Only more frequent)(f)',
tipo_de_vivienda_mod= 'Tipo de Vivienda(f)/Type of Housing(f)', 
tenencia_de_la_vivienda_mod= 'Tenencia de la Vivienda(f)/Tenure status of Households(f)',
rubro_trabaja_mod= 'Rubro de Trabajo(f)/Area of Work(f)',
otras_sus1_mod= 'Otras Sustancias (1)(Sólo más frecuentes)(f)/Other Substances (1)(Only more frequent)(f)',
otras_sus2_mod= 'Otras Sustancias (2)(Sólo más frecuentes)(f)/Other Substances (2)(Only more frequent)(f)',
otras_sus3_mod= 'Otras Sustancias (3)(Sólo más frecuentes)(f)/Other Substances (3)(Only more frequent)(f)',
dg_trs_cons_sus_or= 'Diagnósico de Trastorno por Consumo de Sustancias(d)/Diagnosed of Substance Use Disorder(d)',
dg_trs_psiq_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria(g)',
dg_trs_psiq_sub_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification)(g)',
x2_dg_trs_psiq_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (2)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (2)(g)',
x2_dg_trs_psiq_sub_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion) (2)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification) (2)(g)',
x3_dg_trs_psiq_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (3)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (3)(g)',
x3_dg_trs_psiq_sub_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion) (3)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification) (3)(g)',
dg_trs_psiq_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria(g)',
dg_trs_psiq_sub_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification)(g)',
x2_dg_trs_psiq_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (2)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (2)(g)',
x2_dg_trs_psiq_sub_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion) (2)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification) (2)(g)',
x3_dg_trs_psiq_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (3)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (3)(g)',
x3_dg_trs_psiq_sub_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion) (3)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification) (3)(g)',
diagnostico_trs_fisico= 'Diagnóstico de Trastorno Físico(g)/Diagnosis of Physical Disorder(g)',
otros_probl_at_sm_or= 'Otros Problemas de Atención Vinculados a Salud Mental(g)/Other problems linked to Mental Health(g)',
x4_dg_trs_psiq_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (4)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (4)(g)',
x4_dg_trs_psiq_sub_dsm_iv_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion)(4)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification)(4)(g)',
x4_dg_trs_psiq_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (4)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (4)(g)',
x5_dg_trs_psiq_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (5)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (5)(g)',
x4_dg_trs_psiq_sub_cie_10_or= 'Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion)(4)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification)(4)(g)',
ano_bd_first= 'Año de la Base de Datos(c)/Year of the Dataset (Source)(c)',
ano_bd_last= 'Año de la Base de Datos(b)/Year of the Dataset (Source)(b)',
obs= 'Observaciones al Proceso de Limpieza y Estandarización de Casos(e)/Observations to the Process of Data Tidying & Standardization(e)',
obs_concat_a= 'Observaciones al Proceso de Limpieza y Estandarización de Casos(*)/Observations to the Process of Data Tidying & Standardization(*)',
rn_common_treats2= 'Cuenta de Entradas Comunes(b)/Count of Common Entries(b)',
concat_hash_id_treatments='Combination of User & Distint Entries',
at_least_one_cont_entry= "Casos de Usuarios con más de una entrada después de otra/Cases of users with more than one entry after another one",
senda_concat_a= 'SENDA(*)/SENDA(*)',
tipo_centro_concat_a= 'Tipo de Centro(*)/Type of Center(*)',
fech_ing_num= 'Fecha de Ingreso a Tratamiento (Numérico)(c)/Date of Admission to Treatment (Numeric)(c)',
fech_egres_num= 'Fecha de Egreso (Imputados KNN & Lógico)(Numérico)(b)/Date of Discharge (Imputed KNN & Logic)(Numeric)(b) of the Next Treatment',
fech_ing_next_treat= 'Fecha de Ingreso a Tratamiento (Numérico)(c) del Tratamiento Posterior/Date of Admission to Treatment (Numeric)(c)',
diff_bet_treat= 'Días de diferencia con el Tratamiento Posterior/Days of difference between the Next Treatment',
id_centro_sig_trat= "ID del Centro del Tratamiento Posterior/Center ID of the Next Treatment",
tipo_plan_sig_trat= "Tipo de Plan del Tratamiento Posterior/Type of Plan of the Next Treatment",
tipo_programa_sig_trat= "Tipo de Programa del Tratamiento Posterior/Type of Program of the Next Treatment", 
senda_sig_trat= "SENDA del Tratamiento Posterior/SENDA of the Next Treatment",
menor_60_dias_diff= 'Menor a 60 días de diferencia con el Tratamiento Posterior/Menor a 60 days of difference between the Next Treatment',
menor_45_dias_diff= 'Menor a 45 días de diferencia con el Tratamiento Posterior/Less than 45 days of difference between the Next Treatment',
motivoegreso_derivacion= "Motivo de Egreso= Derivación(b)/Cause of Discharge= Derivación(b)",
dias_treat_imp_sin_na= 'Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13)',
obs_cambios= "Cambios del tratamiento en comparación al Tratamiento Posterior/Changes in treatment compared to the Next Treatment",
obs_cambios_ninguno= "Sin cambios del tratamiento en comparación al Tratamiento Posterior/No changes in treatment compared to the Next Treatment",
obs_cambios_num= "Recuento de cambios del tratamiento en comparación al Tratamiento Posterior/Count of changes in treatment compared to the Next Treatment",
obs_cambios_fac= "Recuento de cambios del tratamiento en comparación al Tratamiento Posterior(factor)/Count of changes in treatment compared to the Next Treatment(factor)",
hash_key_sex_program= 'Usuarios a los que se le ha cambiado el sexo de acuerdo al tipo de plan/Users that changed of sex considering the types of plan',
centro_muj= 'ID de centro que alude a un centro específico para mujeres/Center ID aludes to a women-specific center',
dsm_iv= 'Diagnóstico DSM-IV (1 o más)/Psychiatric Diagnoses (DSM-IV)(one or more)',
cie_10= 'Diagnóstico CIE-10 (1 o más)/Psychiatric Diagnoses (ICD-10)(one or more)',
abandono_temprano= 'Abandono temprano(<3 meses)/ Early Drop-out(<3 months)',
cnt_mod_dsm_iv_or= 'Recuento de Valores en Diagnóstico DSM-IV (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric Diagnoses (DSM-IV)',
cnt_mod_cie_10_or= 'Recuento de Valores en Diagnóstico CIE-10 (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric Diagnoses (ICD-10)',
cnt_diagnostico_trs_fisico= 'Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder',
cnt_otros_probl_at_sm_or= 'Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health',
cum_dias_trat_sin_na= 'Suma acumulada de Días de Tratamiento por Usuario/Cumulative Days of Treatment by User',
mean_cum_dias_trat_sin_na= 'Promedio acumulado de Días de Tratamiento por Usuario/Cumulative Average Days of Treatment by User',
cum_diff_bet_treat= 'Suma acumulada de Diferencia en Días con Tratamiento Siguiente por Usuario/Cumulative sum of Days of difference between the Next Treatment by User',
mean_cum_diff_bet_treat= 'Promedio acumulado de Diferencia en Días entre Tratamientos por Usuario/Cumulative Average Days of Differences Between Treatments By User',
rn_hash= 'Número de Tratamientos por Usuario (menor, tratamiento más antiguo)/Number of Treatments by User (less, older treatment)',
n_hash= 'Número total de Tratamientos por Usuario/Total Number of Treatments by User',

dup= "Número de Tratamientos por HASH (posición a medida que aparecen, menor más antiguo)/Number of Treatments by User",
duplicates_filtered= "Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total)",
startdate= "Fecha de Ingreso Primer Tratamiento (x HASH)/Date of Admission to the First Treatment (By User)",
enddate=  "Fecha de Egreso del Último Tratamiento (x HASH)/Date of Discharge of the Last Treatment (By User)",
person_days= "Días en los que estado un usuario en el sistema para el estudio/User's Days available in the system for the study",
a_botar = "Descarta tratamientos posteriores por usuario(Perdidos= Trat Posteriores)/Discard posterior treatments by each user (Missing= Posterior Treatments)",
event= "Usuarios con Tratamiento Posterior (1=Reingreso x HASH)/Users with Posterior Treatments (1=Readmission)",
person_years= "Años en los que estado un usuario en el sistema para el estudio/User's Years available in the system for the study",

dg_cie_10_rec= 'Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded)',
dg_dsm_iv_rec= 'Diagnóstico DSM-IV (1 o más)(Recodificado)/Psychiatric Diagnoses (DSM-IV)(one or more)(Recoded)',
dg_fis_anemia= "Dignóstico Físico, Anemia/Physical Dg. Anemia",
dg_fis_card= "Dignóstico Físico, Enfermedad Cardiaca/Physical Dg. Heart Disease",
dg_fis_in_study= "Dignóstico Físico, En estudio/Physical Dg. Under Study",
dg_fis_enf_som= "Dignóstico Físico, /Physical Dg. Somatic illnesses",
dg_fis_ets= "Dignóstico Físico, ETSs/Physical Dg. STDs",
dg_fis_hep_alc= "Dignóstico Físico, Hepatitis Alcohólica Subaguda/Physical Dg. Alcoholic hepatitis",
dg_fis_hep_b= "Dignóstico Físico, Hepatitis B, C, D/Physical Dg. Hepatitis B, C, D",
dg_fis_hep_cro= "Dignóstico Físico, Hepatitis Crónica/Physical Dg. Chronic hepatitis",
dg_fis_inf= "Dignóstico Físico, Enfermedades Infeccisas Rel. con Consumo/Physical Dg. Infectuous diseases related to SUDs",
dg_fis_otr_cond_fis_ries_vit= "Dignóstico Físico, Otras condiciones de riesgo vital/Physical Dg. Other phsysical conditions of vital risk",
dg_fis_otr_cond_fis= "Dignóstico Físico, Otras condiciones limitantes/Physical Dg. Other phsysical limitations",
dg_fis_pat_buc= "Dignóstico Físico, Patología Bucal/Physical Dg. Oral-care pathologies",
dg_fis_pat_ges_intrau= "Dignóstico Físico, Patología de la gestión y del niño intrauterino/Physical Dg. Development or Intrauterine",
dg_fis_trau_sec= "Dignóstico Físico, Traumatismos y secuelas secundarias/Physical Dg. Traumatisms and disabling sequelae",

otros_pr_sm_abu_sex= "Otros problemas de Atención de Salud Mental, Abuso Sexual/Other mental health problems, Sexual abuse",
otros_pr_sm_exp_com_sex= "Otros problemas de Atención de Salud Mental, Explotación Sexual Comercial/Other mental health problems, Commercial sexual exploitation",
otros_pr_sm_otros= "Otros problemas de Atención de Salud Mental, Otros/Other mental health problems, Other",

otros_pr_sm_vif= "Otros problemas de Atención de Salud Mental, Violencia Intrafamiliar/Other mental health problems, Domestic violence",

sus_ini_mod_mvv= "Sustancia de Inicio (Valor más vulnerable)/Starting Substance (Most vulnerable value)",
hijos_trat_res= "Tiene Hijos en Ingreso a Tratamiento Residencial del Último Registro/Have Children in Residential Treatment of the Last Entry",
dg_total_cie_10= "Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses)",
dg_total_dsm_iv= "Conteo de Diagnósticos DSM-IV(sólo diagnósticos)/Count of DSM-IV Diagnostics(only diagnoses)",

condicion_ocupacional_corr= 'Condición Ocupacional Criterio Corregido(f)/Occupational Status Corrected(f)',
cat_ocupacional_corr= 'Categoría Ocupacional Criterio Corregido(f)/Occupational Category Corrected(f)',
#2022
dias_trat_alta_temprana= 'Si el tratamiento duró 90 días o más (1= <90d)/If the treatment lasted 90 days or more (1= Less than 90d)',

tipo_de_plan_2_1= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (1st Treatment)',
tipo_de_plan_2_2= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (2nd Treatment)',
tipo_de_plan_2_3= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (3rd Treatment)',
tipo_de_plan_2_4= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (4th Treatment)',
tipo_de_plan_2_5= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (5th Treatment)',
tipo_de_plan_2_6= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (6th Treatment)',
tipo_de_plan_2_7= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (7th Treatment)',
tipo_de_plan_2_8= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (8th Treatment)',
tipo_de_plan_2_9= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (9th Treatment)',
tipo_de_plan_2_10= 'Tipo de Plan del Último Registro/Type of Plan of the Last Entry (10th Treatment)',

motivodeegreso_mod_imp_1= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (1st Treatment)',
motivodeegreso_mod_imp_2= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (2nd Treatment)', 
motivodeegreso_mod_imp_3= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (3th Treatment)',
motivodeegreso_mod_imp_4= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (4th Treatment)',
motivodeegreso_mod_imp_5= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (5th Treatment)',
motivodeegreso_mod_imp_6= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (6th Treatment)',
motivodeegreso_mod_imp_7= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (7th Treatment)',
motivodeegreso_mod_imp_8= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (8th Treatment)',
motivodeegreso_mod_imp_9= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (9th Treatment)',
motivodeegreso_mod_imp_10= 'Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (10th Treatment)',

dias_treat_imp_sin_na_1= "Days of Treatment (1st Treatment)",
dias_treat_imp_sin_na_2= "Days of Treatment (2nd Treatment)",
dias_treat_imp_sin_na_3= "Days of Treatment (3rd Treatment)",
dias_treat_imp_sin_na_4= "Days of Treatment (4th Treatment)",
dias_treat_imp_sin_na_5= "Days of Treatment (5th Treatment)",
dias_treat_imp_sin_na_6= "Days of Treatment (6th Treatment)",
dias_treat_imp_sin_na_7= "Days of Treatment (7th Treatment)",
dias_treat_imp_sin_na_8= "Days of Treatment (8th Treatment)",
dias_treat_imp_sin_na_9= "Days of Treatment (9th Treatment)",
dias_treat_imp_sin_na_10="Days of Treatment (10th Treatment)",

diff_bet_treat_1= "Diff Between Treatments (1st Treatment)",
diff_bet_treat_2= "Diff Between Treatments (2nd Treatment)",
diff_bet_treat_3= "Diff Between Treatments (3rd Treatment)",
diff_bet_treat_4= "Diff Between Treatments (4th Treatment)",
diff_bet_treat_5= "Diff Between Treatments (5th Treatment)",
diff_bet_treat_6= "Diff Between Treatments (6th Treatment)",
diff_bet_treat_7= "Diff Between Treatments (7th Treatment)",
diff_bet_treat_8= "Diff Between Treatments (8th Treatment)",
diff_bet_treat_9= "Diff Between Treatments (9th Treatment)",
diff_bet_treat_10="Diff Between Treatments (10th Treatment)",

cum_dias_trat_sin_na_1= "Cum. Days of Treatment (1st Treatment)",
cum_dias_trat_sin_na_2= "Cum. Days of Treatment (2nd Treatment)",
cum_dias_trat_sin_na_3= "Cum. Days of Treatment (3rd Treatment)",
cum_dias_trat_sin_na_4= "Cum. Days of Treatment (4th Treatment)",
cum_dias_trat_sin_na_5= "Cum. Days of Treatment (5th Treatment)",
cum_dias_trat_sin_na_6= "Cum. Days of Treatment (6th Treatment)",
cum_dias_trat_sin_na_7= "Cum. Days of Treatment (7th Treatment)",
cum_dias_trat_sin_na_8= "Cum. Days of Treatment (8th Treatment)",
cum_dias_trat_sin_na_9= "Cum. Days of Treatment (9th Treatment)",
cum_dias_trat_sin_na_10="Cum. Days of Treatment (10th Treatment)",

mean_cum_dias_trat_sin_na_1= "Avg. Cum. Days of Treatment (1st Treatment)",
mean_cum_dias_trat_sin_na_2= "Avg. Cum. Days of Treatment (2nd Treatment)",
mean_cum_dias_trat_sin_na_3= "Avg. Cum. Days of Treatment (3rd Treatment)",
mean_cum_dias_trat_sin_na_4= "Avg. Cum. Days of Treatment (4th Treatment)",
mean_cum_dias_trat_sin_na_5= "Avg. Cum. Days of Treatment (5th Treatment)",
mean_cum_dias_trat_sin_na_6= "Avg. Cum. Days of Treatment (6th Treatment)",
mean_cum_dias_trat_sin_na_7= "Avg. Cum. Days of Treatment (7th Treatment)",
mean_cum_dias_trat_sin_na_8= "Avg. Cum. Days of Treatment (8th Treatment)",
mean_cum_dias_trat_sin_na_9= "Avg. Cum. Days of Treatment (9th Treatment)",
mean_cum_dias_trat_sin_na_10="Avg. Cum. Days of Treatment (10th Treatment)",

cum_diff_bet_treat_1= "Cum. Diff Between Treatments (1st Treatment)",
cum_diff_bet_treat_2= "Cum. Diff Between Treatments (2nd Treatment)",
cum_diff_bet_treat_3= "Cum. Diff Between Treatments (3rd Treatment)",
cum_diff_bet_treat_4= "Cum. Diff Between Treatments (4th Treatment)",
cum_diff_bet_treat_5= "Cum. Diff Between Treatments (5th Treatment)",
cum_diff_bet_treat_6= "Cum. Diff Between Treatments (6th Treatment)",
cum_diff_bet_treat_7= "Cum. Diff Between Treatments (7th Treatment)",
cum_diff_bet_treat_8= "Cum. Diff Between Treatments (8th Treatment)",
cum_diff_bet_treat_9= "Cum. Diff Between Treatments (9th Treatment)",
cum_diff_bet_treat_10="Cum. Diff Between Treatments (10th Treatment)",

mean_cum_diff_bet_treat_1= "Avg. Cum. Diff Between Treatments (1st Treatment)",
mean_cum_diff_bet_treat_2= "Avg. Cum. Diff Between Treatments (2nd Treatment)",
mean_cum_diff_bet_treat_3= "Avg. Cum. Diff Between Treatments (3rd Treatment)",
mean_cum_diff_bet_treat_4= "Avg. Cum. Diff Between Treatments (4th Treatment)",
mean_cum_diff_bet_treat_5= "Avg. Cum. Diff Between Treatments (5th Treatment)",
mean_cum_diff_bet_treat_6= "Avg. Cum. Diff Between Treatments (6th Treatment)",
mean_cum_diff_bet_treat_7= "Avg. Cum. Diff Between Treatments (7th Treatment)",
mean_cum_diff_bet_treat_8= "Avg. Cum. Diff Between Treatments (8th Treatment)",
mean_cum_diff_bet_treat_9= "Avg. Cum. Diff Between Treatments (9th Treatment)",
mean_cum_diff_bet_treat_10="Avg. Cum. Diff Between Treatments (10th Treatment)"
)

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

# no_mostrar_nunca=0
# if(no_mostrar_nunca==1){
# df_def<-
# data.frame(cbind(var_name= names(codebook_data),var_def=data.table(codebook::var_label(codebook_data), keep.rownames = T), type=data.table(sapply(codebook_data, class)),can_be_na=data.table(rep(FALSE,length(names(codebook_data))))))%>%
#   dplyr::rename("var_def"="var_def.V1","type"="type.V1","can_be_na"="can_be_na.V1")
# }    
# 
# #####to_export_labels
# table_labels<-
#   tibble::rownames_to_column(data.frame(Hmisc::label(CONS_C1_df_dup_SEP_2020)))%>% data.frame() %>%
#   dplyr::rename("code" = !!names(.[1]), "label" = !!names(.[2]))%>% data.frame()%>%
#   dplyr::mutate(first= "cap label variable")%>%
#   dplyr::mutate(final= paste0(first, " ",code,' "',label,'"'))%>%
#   dplyr::select(-code,-label,-first)%>%
#   rbind('cap save "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/CONS_C1_df_dup_SEP_2020.dta", replace')%>%
#   rbind('cap drop id id_mod nombre_centro consentimiento_informado')%>%
#   rbind('cap save "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/CONS_C1_df_dup_SEP_2020_exp.dta", replace')
# 
# table_labels<-
#   data.frame(final='use "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/CONS_C1_df_dup_SEP_2020.dta", clear')%>%
#   rbind(table_labels)%>%
#   rename("*final"="final") 
#   #write.csv2(table_labels,"__labels_to_stata_C1_jun_2020.do",row.names =F)
#   write.table(table_labels, file = "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/SUD_CL/_label_var_to_stata.do", sep = "",
#             row.names = FALSE, quote = FALSE,fileEncoding="UTF-8")

 # dplyr::filter(label!="") %>%
#  bind_rows(data.frame("code"=c("fech_egres_imp", 
##                                "dias_trat_imp",
#                                "dias_trat_alta_temprana_imp",
#                                "motivodeegreso_mod_imp"), "label"=c("Date of Discharge (Imputed)", 
##                                                                     "Days of Treatment (Imputed)",
#                                                                     "Days of Treatment for Early Withdrawal (Imputed)",
#                                                                     "Cause of Discharge w/ Early or Late Withdrawal (Imputed)"))) %>% 
# 
# CONS_C1_df_dup_SEP_2020%>%
#   dplyr::arrange(hash_key, fech_ing)%>% 
#   rio::export(file = "G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/CONS_C1_df_dup_SEP_2020.dta")

#save.image("G:/Mi unidad/Alvacast/SISTRAT 2019 (github)/9_2021.RData")

#Agregar nuevas variables al codebook
#hacer codebook
#Agregar procesos a strobe
#Poner olr y arriba pondría los otros modelos.

#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_#_

#2022
time_after_desc<-Sys.time()

paste0("Time in markdown: ");time_after_desc-time_before_desc
[1] "Time in markdown: "
Time difference of 6.504165 mins
CONS_C1_df_dup_SEP_2020%>%
   dplyr::arrange(hash_key, fech_ing)%>% 
   rio::export(file = paste0(path,"/CONS_C1_df_dup_SEP_2020.dta"))

#save.image(paste0(gsub("SUD_CL/Desc_2021.Rmd","",path),"9_2021.RData"))

save.image(paste0(path,"/8.RData"))

#Agregar nuevas variables al codebook
#hacer codebook
#Agregar procesos a strobe
#Poner olr y arriba pondría los otros modelos.
export_lab_stata<-
  tibble::rownames_to_column(data.frame(Hmisc::label(CONS_C1_df_dup_SEP_2020)))%>% data.frame() %>%
  dplyr::rename("code" = !!names(.[1]), "label" = !!names(.[2]))%>% data.frame()%>%
  dplyr::mutate(first= "cap noi label variable")%>%
  dplyr::mutate(final= paste0(first, " ",code,' "',label,'"'))%>%
  dplyr::select(-code,-label,-first)%>%
  dplyr::rename("*clear all"="final") %>% 
  rbind(paste0('cap noi save "', gsub('/', '\\', path, fixed=T),'\\CONS_C1_df_dup_SEP_2020.dta", replace'))%>%
  rbind('cap noi drop id id_mod nombre_centro consentimiento_informado')%>%
  rbind('cap noi drop id id_mod nombre_centro')%>%
  rbind(paste0('cap noi save "', gsub('/', '\\', path, fixed=T),'\\CONS_C1_df_dup_SEP_2020_exp.dta", replace'))

rbind(paste0('cap noi use "', gsub('/', '\\', path, fixed=T),'\\CONS_C1_df_dup_SEP_2020.dta", clear'),export_lab_stata) %>% knitr::kable("html") %>% 
    kableExtra::kable_styling(bootstrap_options = c("striped", "hover"),font_size =10) %>% 
  kableExtra::scroll_box(width = "100%", height = "375px")
*clear all
cap noi use “C:Fondecytunidad (github)_C1_df_dup_SEP_2020.dta”, clear
cap noi label variable row “Numerador de los eventos presentes en la Base de Datos (Último registro)/Events in the Dataset (Last Entry)”
cap noi label variable row_cont_entries “Numerador de los eventos presentes en la Base de Datos()/Events in the Dataset()”
cap noi label variable hash_key “Codificación del RUN/Masked Identifier (RUN)”
cap noi label variable hash_rut_completo “HASH alternativo, en el escenario en que se asuma que el individuo al que se le codificó el RUN presente mayor edad/Alternative HASH-Key”
cap noi label variable id “Codigo Identificación de SENDA/SENDA ID”
cap noi label variable id_mod “ID de SENDA para Presentación en Página Web (enmascara caracteres 5 y 6)/SENDA ID (mask characters 5 & 6)”
cap noi label variable fech_ing “Fecha de Ingreso a Tratamiento (Primera Entrada)/Date of Admission to Treatment (First Entry)”
cap noi label variable fech_egres_imp “Fecha de Egreso (Imputados KNN & Lógico) del Último Registro(b)/Date of Discharge (Imputed KNN & Logic) of the Last Entry(b)”
cap noi label variable tipo_de_plan_2_largest_treat “Tipo de Plan del Registro Más Largo entre entradas intermedias(f)/Type of Plan of the Largest Entry Among Intermediate Entries(f)”
cap noi label variable tipo_de_plan_2_concat_a “Tipo de Plan()/Type of Plan()”
cap noi label variable tipo_de_programa_2 “Tipo de Programa del Registro Más Largo entre Entradas Intermedias/Type of Program of the Largest Entry Among Intermediate Entries”
cap noi label variable id_centro “ID de Centro(b)/Treatment center ID(b)”
cap noi label variable nombre_centro “Nombre del Centro de Tratamiento()/Treatment Center()”
cap noi label variable id_centro_concat_a “ID de Centro()/Treatment center ID()”
cap noi label variable tipo_centro “Tipo de Centro del Último Registro/Type of Center of the Last Entry”
cap noi label variable servicio_de_salud “Servicio de Salud()/Health Service()”
cap noi label variable senda “SENDA del Último Registro/SENDA of the Last Entry”
cap noi label variable numero_de_hijos_mod “Número de Hijos (Valor Max.)/Number of Children (Max. Value)”
cap noi label variable num_hijos_trat_res_mod “Número de Hijos para Ingreso a Tratamiento Residencial del Último Registro/Number of Children to Residential Treatment of the Last Entry”
cap noi label variable tipo_centro_derivacion “Tipo de Centro al que el Usuario es Derivado del Último Registro(b)/Type of Center of Derivation of the Last Entry(b)”
cap noi label variable macrozona “Macrozona del Centro del Último Registro(b)/Macrozones of the Center of the Last Entry(b)”
cap noi label variable nombre_region “Región del Centro del Último Registro(b)/Chilean Region of the Center of the Last Entry(b)”
cap noi label variable comuna_residencia_cod “Comuna de Residencia del Último Registro(b)/Municipality or District of Residence of the Last Entry(b)”
cap noi label variable fecha_ingreso_a_convenio_senda “Fecha de Ingreso a Convenio SENDA (aún no formateada como fecha) (Primera Entrada)/Date of Admission to SENDA Agreement (First Entry)”
cap noi label variable identidad_de_genero “Identidad de Género (Último Registro)(b)/Gender Identity (Last Entry)(b)”
cap noi label variable edad_al_ing “Edad a la Fecha de Ingreso a Tratamiento (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First Entry)”
cap noi label variable origen_ingreso_mod “Origen de Ingreso (Primera Entrada)/Motive of Admission to Treatment (First Entry)”
cap noi label variable x_se_trata_mujer_emb “Mujer Embarazada al Ingreso (d)/Pregnant at Admission (d)”
cap noi label variable compromiso_biopsicosocial “Compromiso Biopsicosocial(d)/Biopsychosocial Involvement(d)”
cap noi label variable dg_global_nec_int_soc_or “Diagnóstico Global de Necesidades de Integración Social (Al Ingreso)(d)/Global Diagnosis of Social Integration (At Admission)(d)”
cap noi label variable dg_nec_int_soc_cap_hum_or “Diagnóstico de Necesidades de Integración Social en Capital Humano (Al Ingreso)(d)/Global Diagnosis of Social Integration in Human Capital (At Admission)(d)”
cap noi label variable dg_nec_int_soc_cap_fis_or “Diagnóstico de Necesidades de Integración Social en Capital Físico (Al Ingreso)(d)/Global Diagnosis of Social Integration in Physical Capital (At Admission)(d)”
cap noi label variable dg_nec_int_soc_cap_soc_or “Diagnóstico de Necesidades de Integración Social en Capital Social (Al Ingreso)(d)/Global Diagnosis of Social Integration in Social Capital (At Admission)(d)”
cap noi label variable usuario_tribunal_trat_droga “Usuario de modalidad Tribunales de Tratamiento de Drogas(d)/User of Drug Treatment Courts Modality(d)”
cap noi label variable evaluacindelprocesoteraputico “Evaluación del Proceso Terapéutico(d)/Evaluation of the Therapeutic Process(d)”
cap noi label variable eva_consumo “Evaluación al Egreso Respecto al Patrón de consumo(d)/Evaluation at Discharge regarding to Consumption Pattern(d)”
cap noi label variable eva_fam “Evaluación al Egreso Respecto a Situación Familiar(d)/Evaluation at Discharge regarding to Family Situation(d)”
cap noi label variable eva_relinterp “Evaluación al Egreso Respecto a Relaciones Interpersonales(d)/Evaluation at Discharge regarding to Interpersonal Relations(d)”
cap noi label variable eva_ocupacion “Evaluación al Egreso Respecto a Situación Ocupacional(d)/Evaluation at Discharge regarding to Occupational Status(d)”
cap noi label variable eva_sm “Evaluación al Egreso Respecto a Salud Mental(d)/Evaluation at Discharge regarding to Mental Health(d)”
cap noi label variable eva_fisica “Evaluación al Egreso Respecto a Salud Física(d)/Evaluation at Discharge regarding to Physical Health(d)”
cap noi label variable eva_transgnorma “Evaluación al Egreso Respecto a Trasgresión a la Norma Social(d)/Evaluation at Discharge regarding to Transgression to the Norm(d)”
cap noi label variable dg_global_nec_int_soc_or_1 “Diagnóstico Global de Necesidades de Integración Social (Al Egreso)(d)/Global Diagnosis of Social Integration (At Discharge)(d)”
cap noi label variable dg_nec_int_soc_cap_hum_or_1 “Diagnóstico de Necesidades de Integración Social en Capital Humano (Al Egreso)(d)/Global Diagnosis of Social Integration in Human Capital (At Discharge)(d)”
cap noi label variable dg_nec_int_soc_cap_fis_or_1 “Diagnóstico de Necesidades de Integración Social en Capital Físico (Al Egreso)(d)/Global Diagnosis of Social Integration in Physical Capital (At Discharge)(d)”
cap noi label variable dg_nec_int_soc_cap_soc_or_1 “Diagnóstico de Necesidades de Integración Social en Capital Social (Al Egreso)(d)/Global Diagnosis of Social Integration in Social Capital (At Discharge)(d)”
cap noi label variable tiene_menores_de_edad_a_cargo “Menores de Edad A Cargo(d)/Minor Dependants(d)”
cap noi label variable ha_estado_embarazada_egreso “¿Ha estado embarazada? (al Egreso)(d)/Have you been Pregnant (at Discharge)(d)”
cap noi label variable discapacidad “Presenta Discapacidad(d)/Disability(d)”
cap noi label variable opcion_discapacidad “Origen de Discapacidad(d)/Cause of Disability(d)”
cap noi label variable escolaridad “Escolaridad: Nivel Eduacional(d)/Educational Attainment(d)”
cap noi label variable edad_al_ing_grupos “Edad a la Fecha de Ingreso a Tratamiento en Grupos(c)/Age at Admission to Treatment In Groups(c)”
cap noi label variable nacionalidad “Nacionalidad/Nationality”
cap noi label variable sexo_2 “Sexo Usuario/Sex of User”
cap noi label variable embarazo “Embarazo al Ingreso(c)/Pregnant at Admission(c)”
cap noi label variable fech_nac “Fecha de Nacimiento/Date of Birth”
cap noi label variable edad_ini_cons “Edad de Inicio de Consumo/Age of Onset of Drug Use”
cap noi label variable edad_ini_sus_prin “Edad de Inicio de Consumo Sustancia Principal/Age of Onset of Drug Use of Primary Substance”
cap noi label variable estado_conyugal_2 “Estado Conyugal/Marital Status”
cap noi label variable edad_grupos “Edad agrupada/Age in groups”
cap noi label variable freq_cons_sus_prin “Frecuencia de Consumo de la Sustancia Principal (30 días previos a la admisión)(f)/Frequency of Consumption of the Primary or Main Substance (30 days previous to admission)(f)”
cap noi label variable via_adm_sus_prin_act “Vía de Administración de la Sustancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administration of the Primary or Main Substance (Tidy)(f)”
cap noi label variable etnia_cor “Etnia/Ethnic Group”
cap noi label variable nacionalidad_2 “Segunda Nacionalidad/Second Nationality”
cap noi label variable etnia_cor_2 “Etnia (2)/Second Ethnic Group”
cap noi label variable sus_ini_2_mod “Segunda Sustancia de Inicio(Sólo más frecuentes)/Second Starting Substance”
cap noi label variable sus_ini_3_mod “Tercera Sustancia de Inicio(Sólo más frecuentes)/Third Starting Substance”
cap noi label variable sus_ini_mod “Sustancia de Inicio (Sólo más frecuentes)/Starting Substance (Only more frequent)”
cap noi label variable con_quien_vive “Persona con la que vive el Usuario(f)/People that Share Household with the User (Cohabitation Status)(f)”
cap noi label variable estatus_ocupacional “Condición Ocupacional(f)/Occupational Status(f)”
cap noi label variable cat_ocupacional “Categoría Ocupacional(f)/Occupational Category(f)”
cap noi label variable sus_principal_mod “Sustancia Principal de Consumo (Sólo más frecuentes)(f)/Primary or Main Substance of Consumption at Admission (Only more frequent)(f)”
cap noi label variable tipo_de_vivienda_mod “Tipo de Vivienda(f)/Type of Housing(f)”
cap noi label variable tenencia_de_la_vivienda_mod “Tenencia de la Vivienda(f)/Tenure status of Households(f)”
cap noi label variable rubro_trabaja_mod “Rubro de Trabajo(f)/Area of Work(f)”
cap noi label variable otras_sus1_mod “Otras Sustancias (1)(Sólo más frecuentes)(f)/Other Substances (1)(Only more frequent)(f)”
cap noi label variable otras_sus2_mod “Otras Sustancias (2)(Sólo más frecuentes)(f)/Other Substances (2)(Only more frequent)(f)”
cap noi label variable otras_sus3_mod “Otras Sustancias (3)(Sólo más frecuentes)(f)/Other Substances (3)(Only more frequent)(f)”
cap noi label variable dg_trs_cons_sus_or “Diagnósico de Trastorno por Consumo de Sustancias(d)/Diagnosed of Substance Use Disorder(d)”
cap noi label variable diagnostico_trs_fisico “Diagnóstico de Trastorno Físico(g)/Diagnosis of Physical Disorder(g)”
cap noi label variable otros_probl_at_sm_or “Otros Problemas de Atención Vinculados a Salud Mental(g)/Other problems linked to Mental Health(g)”
cap noi label variable ano_bd_first “Año de la Base de Datos(c)/Year of the Dataset (Source)(c)”
cap noi label variable ano_bd_last “Año de la Base de Datos(b)/Year of the Dataset (Source)(b)”
cap noi label variable obs “Observaciones al Proceso de Limpieza y Estandarización de Casos(e)/Observations to the Process of Data Tidying & Standardization(e)”
cap noi label variable obs_concat_a “Observaciones al Proceso de Limpieza y Estandarización de Casos()/Observations to the Process of Data Tidying & Standardization()”
cap noi label variable rn_common_treats2 “Cuenta de Entradas Comunes(b)/Count of Common Entries(b)”
cap noi label variable concat_hash_id_treatments “Combination of User & Distint Entries”
cap noi label variable at_least_one_cont_entry “Casos de Usuarios con más de una entrada después de otra/Cases of users with more than one entry after another one”
cap noi label variable senda_concat_a “SENDA()/SENDA()”
cap noi label variable tipo_centro_concat_a “Tipo de Centro()/Type of Center()”
cap noi label variable fech_ing_num “Fecha de Ingreso a Tratamiento (Numérico)(c)/Date of Admission to Treatment (Numeric)(c)”
cap noi label variable fech_egres_num “Fecha de Egreso (Imputados KNN & Lógico)(Numérico)(b)/Date of Discharge (Imputed KNN & Logic)(Numeric)(b) of the Next Treatment”
cap noi label variable fech_ing_next_treat “Fecha de Ingreso a Tratamiento (Numérico)(c) del Tratamiento Posterior/Date of Admission to Treatment (Numeric)(c)”
cap noi label variable id_centro_sig_trat “ID del Centro del Tratamiento Posterior/Center ID of the Next Treatment”
cap noi label variable tipo_plan_sig_trat “Tipo de Plan del Tratamiento Posterior/Type of Plan of the Next Treatment”
cap noi label variable tipo_programa_sig_trat “Tipo de Programa del Tratamiento Posterior/Type of Program of the Next Treatment”
cap noi label variable senda_sig_trat “SENDA del Tratamiento Posterior/SENDA of the Next Treatment”
cap noi label variable menor_60_dias_diff “Menor a 60 días de diferencia con el Tratamiento Posterior/Menor a 60 days of difference between the Next Treatment”
cap noi label variable menor_45_dias_diff “Menor a 45 días de diferencia con el Tratamiento Posterior/Less than 45 days of difference between the Next Treatment”
cap noi label variable motivoegreso_derivacion “Motivo de Egreso= Derivación(b)/Cause of Discharge= Derivación(b)”
cap noi label variable abandono_temprano “Abandono temprano(<3 meses)/ Early Drop-out(<3 months)”
cap noi label variable obs_cambios “Cambios del tratamiento en comparación al Tratamiento Posterior/Changes in treatment compared to the Next Treatment”
cap noi label variable obs_cambios_ninguno “Sin cambios del tratamiento en comparación al Tratamiento Posterior/No changes in treatment compared to the Next Treatment”
cap noi label variable obs_cambios_num “Recuento de cambios del tratamiento en comparación al Tratamiento Posterior/Count of changes in treatment compared to the Next Treatment”
cap noi label variable obs_cambios_fac “Recuento de cambios del tratamiento en comparación al Tratamiento Posterior(factor)/Count of changes in treatment compared to the Next Treatment(factor)”
cap noi label variable edad_ini_sus_prin_grupos “Edad de Inicio de Consumo Sustancia Principal (en Grupos)/Age of Onset of Drug Use of Primary Substance (in Groups)”
cap noi label variable hash_key_sex_program “Usuarios a los que se le ha cambiado el sexo de acuerdo al tipo de plan/Users that changed of sex considering the types of plan”
cap noi label variable centro_muj “ID de centro que alude a un centro específico para mujeres/Center ID aludes to a women-specific center”
cap noi label variable cie_10 “Diagnóstico CIE-10 (1 o más)/Psychiatric Diagnoses (ICD-10)(one or more)”
cap noi label variable dsm_iv “Diagnóstico DSM-IV (1 o más)/Psychiatric Diagnoses (DSM-IV)(one or more)”
cap noi label variable con_quien_vive_rec “Persona con la que vive el Usuario (Recodificada)(f)/People that Share Household with the User (Cohabitation Status)(Recoded)(f)”
cap noi label variable cnt_mod_dsm_iv_or “Recuento de Valores en Diagnóstico DSM-IV (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric Diagnoses (DSM-IV)”
cap noi label variable cnt_mod_cie_10_or “Recuento de Valores en Diagnóstico CIE-10 (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric Diagnoses (ICD-10)”
cap noi label variable cnt_diagnostico_trs_fisico “Recuento de Diagnóstico de Trastorno Físico/Count of Physical Disorder”
cap noi label variable dg_trs_psiq_sub_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification)(g)”
cap noi label variable dg_trs_psiq_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria(g)”
cap noi label variable dg_trs_psiq_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria(g)”
cap noi label variable x2_dg_trs_psiq_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (2)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (2)(g)”
cap noi label variable x3_dg_trs_psiq_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (3)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (3)(g)”
cap noi label variable x4_dg_trs_psiq_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (4)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (4)(g)”
cap noi label variable x5_dg_trs_psiq_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (5)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (5)(g)”
cap noi label variable x2_dg_trs_psiq_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (2)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (2)(g)”
cap noi label variable x3_dg_trs_psiq_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (3)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (3)(g)”
cap noi label variable x4_dg_trs_psiq_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (4)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (4)(g)”
cap noi label variable dg_trs_psiq_sub_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification)(g)”
cap noi label variable x2_dg_trs_psiq_sub_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion) (2)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification) (2)(g)”
cap noi label variable x3_dg_trs_psiq_sub_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion) (3)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification) (3)(g)”
cap noi label variable x4_dg_trs_psiq_sub_cie_10_or “Diagnóstico de Trastorno Psiquiátrico, Criterios CIE-10 (Subclasificacion)(4)(g)/Diagnosis of Psychiatric Disorders, CIE-10 criteria (subclassification)(4)(g)”
cap noi label variable x2_dg_trs_psiq_sub_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion) (2)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification) (2)(g)”
cap noi label variable x3_dg_trs_psiq_sub_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion) (3)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification) (3)(g)”
cap noi label variable x4_dg_trs_psiq_sub_dsm_iv_or “Diagnóstico de Trastorno Psiquiátrico, Criterios DSM IV (Subclasificacion)(4)(g)/Diagnosis of Psychiatric Disorders, DSM-IV criteria (sub-classification)(4)(g)”
cap noi label variable cnt_otros_probl_at_sm_or “Recuento de Otros Problemas de Atención Vinculados a Salud Mental/Count of Other problems linked to Mental Health”
cap noi label variable escolaridad_rec “Escolaridad: Nivel Eduacional(d) Normalizado a Progresión de Tratamientos/Educational Attainment(d) & Normalized Following Progression of Treatments”
cap noi label variable tipo_de_plan_2 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry”
cap noi label variable motivodeegreso_mod_imp “Motivo de Egreso (con abandono temprano y tardío)(Imputados KNN & Lógico) del Último Registro(b)/Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b)”
cap noi label variable rn_hash “Número de Tratamientos por Usuario (menor, tratamiento más antiguo)/Number of Treatments by User (less, older treatment)”
cap noi label variable n_hash “Número total de Tratamientos por Usuario/Total Number of Treatments by User”
cap noi label variable cum_dias_trat_sin_na “Suma acumulada de Días de Tratamiento por Usuario/Cumulative Days of Treatment by User”
cap noi label variable mean_cum_dias_trat_sin_na “Promedio acumulado de Días de Tratamiento por Usuario/Cumulative Average Days of Treatment by User”
cap noi label variable cum_diff_bet_treat “Suma acumulada de Diferencia en Días con Tratamiento Siguiente por Usuario/Cumulative sum of Days of difference between the Next Treatment by User”
cap noi label variable mean_cum_diff_bet_treat “Promedio acumulado de Diferencia en Días entre Tratamientos por Usuario/Cumulative Average Days of Differences Between Treatments By User”
cap noi label variable tipo_de_plan_2_1 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (1st Treatment)”
cap noi label variable tipo_de_plan_2_2 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (2nd Treatment)”
cap noi label variable tipo_de_plan_2_3 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (3rd Treatment)”
cap noi label variable tipo_de_plan_2_4 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (4th Treatment)”
cap noi label variable tipo_de_plan_2_5 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (5th Treatment)”
cap noi label variable tipo_de_plan_2_6 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (6th Treatment)”
cap noi label variable tipo_de_plan_2_7 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (7th Treatment)”
cap noi label variable tipo_de_plan_2_8 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (8th Treatment)”
cap noi label variable tipo_de_plan_2_9 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (9th Treatment)”
cap noi label variable tipo_de_plan_2_10 “Tipo de Plan del Último Registro/Type of Plan of the Last Entry (10th Treatment)”
cap noi label variable motivodeegreso_mod_imp_1 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (1st Treatment)”
cap noi label variable motivodeegreso_mod_imp_2 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (2nd Treatment)”
cap noi label variable motivodeegreso_mod_imp_3 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (3th Treatment)”
cap noi label variable motivodeegreso_mod_imp_4 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (4th Treatment)”
cap noi label variable motivodeegreso_mod_imp_5 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (5th Treatment)”
cap noi label variable motivodeegreso_mod_imp_6 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (6th Treatment)”
cap noi label variable motivodeegreso_mod_imp_7 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (7th Treatment)”
cap noi label variable motivodeegreso_mod_imp_8 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (8th Treatment)”
cap noi label variable motivodeegreso_mod_imp_9 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (9th Treatment)”
cap noi label variable motivodeegreso_mod_imp_10 “Cause of Discharge (with late and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (10th Treatment)”
cap noi label variable dias_treat_imp_sin_na_1 “Days of Treatment (1st Treatment)”
cap noi label variable dias_treat_imp_sin_na_2 “Days of Treatment (2nd Treatment)”
cap noi label variable dias_treat_imp_sin_na_3 “Days of Treatment (3rd Treatment)”
cap noi label variable dias_treat_imp_sin_na_4 “Days of Treatment (4th Treatment)”
cap noi label variable dias_treat_imp_sin_na_5 “Days of Treatment (5th Treatment)”
cap noi label variable dias_treat_imp_sin_na_6 “Days of Treatment (6th Treatment)”
cap noi label variable dias_treat_imp_sin_na_7 “Days of Treatment (7th Treatment)”
cap noi label variable dias_treat_imp_sin_na_8 “Days of Treatment (8th Treatment)”
cap noi label variable dias_treat_imp_sin_na_9 “Days of Treatment (9th Treatment)”
cap noi label variable dias_treat_imp_sin_na_10 “Days of Treatment (10th Treatment)”
cap noi label variable diff_bet_treat_1 “Diff Between Treatments (1st Treatment)”
cap noi label variable diff_bet_treat_2 “Diff Between Treatments (2nd Treatment)”
cap noi label variable diff_bet_treat_3 “Diff Between Treatments (3rd Treatment)”
cap noi label variable diff_bet_treat_4 “Diff Between Treatments (4th Treatment)”
cap noi label variable diff_bet_treat_5 “Diff Between Treatments (5th Treatment)”
cap noi label variable diff_bet_treat_6 “Diff Between Treatments (6th Treatment)”
cap noi label variable diff_bet_treat_7 “Diff Between Treatments (7th Treatment)”
cap noi label variable diff_bet_treat_8 “Diff Between Treatments (8th Treatment)”
cap noi label variable diff_bet_treat_9 “Diff Between Treatments (9th Treatment)”
cap noi label variable diff_bet_treat_10 “Diff Between Treatments (10th Treatment)”
cap noi label variable cum_dias_trat_sin_na_1 “Cum. Days of Treatment (1st Treatment)”
cap noi label variable cum_dias_trat_sin_na_2 “Cum. Days of Treatment (2nd Treatment)”
cap noi label variable cum_dias_trat_sin_na_3 “Cum. Days of Treatment (3rd Treatment)”
cap noi label variable cum_dias_trat_sin_na_4 “Cum. Days of Treatment (4th Treatment)”
cap noi label variable cum_dias_trat_sin_na_5 “Cum. Days of Treatment (5th Treatment)”
cap noi label variable cum_dias_trat_sin_na_6 “Cum. Days of Treatment (6th Treatment)”
cap noi label variable cum_dias_trat_sin_na_7 “Cum. Days of Treatment (7th Treatment)”
cap noi label variable cum_dias_trat_sin_na_8 “Cum. Days of Treatment (8th Treatment)”
cap noi label variable cum_dias_trat_sin_na_9 “Cum. Days of Treatment (9th Treatment)”
cap noi label variable cum_dias_trat_sin_na_10 “Cum. Days of Treatment (10th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_1 “Avg. Cum. Days of Treatment (1st Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_2 “Avg. Cum. Days of Treatment (2nd Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_3 “Avg. Cum. Days of Treatment (3rd Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_4 “Avg. Cum. Days of Treatment (4th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_5 “Avg. Cum. Days of Treatment (5th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_6 “Avg. Cum. Days of Treatment (6th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_7 “Avg. Cum. Days of Treatment (7th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_8 “Avg. Cum. Days of Treatment (8th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_9 “Avg. Cum. Days of Treatment (9th Treatment)”
cap noi label variable mean_cum_dias_trat_sin_na_10 “Avg. Cum. Days of Treatment (10th Treatment)”
cap noi label variable cum_diff_bet_treat_1 “Cum. Diff Between Treatments (1st Treatment)”
cap noi label variable cum_diff_bet_treat_2 “Cum. Diff Between Treatments (2nd Treatment)”
cap noi label variable cum_diff_bet_treat_3 “Cum. Diff Between Treatments (3rd Treatment)”
cap noi label variable cum_diff_bet_treat_4 “Cum. Diff Between Treatments (4th Treatment)”
cap noi label variable cum_diff_bet_treat_5 “Cum. Diff Between Treatments (5th Treatment)”
cap noi label variable cum_diff_bet_treat_6 “Cum. Diff Between Treatments (6th Treatment)”
cap noi label variable cum_diff_bet_treat_7 “Cum. Diff Between Treatments (7th Treatment)”
cap noi label variable cum_diff_bet_treat_8 “Cum. Diff Between Treatments (8th Treatment)”
cap noi label variable cum_diff_bet_treat_9 “Cum. Diff Between Treatments (9th Treatment)”
cap noi label variable cum_diff_bet_treat_10 “Cum. Diff Between Treatments (10th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_1 “Avg. Cum. Diff Between Treatments (1st Treatment)”
cap noi label variable mean_cum_diff_bet_treat_2 “Avg. Cum. Diff Between Treatments (2nd Treatment)”
cap noi label variable mean_cum_diff_bet_treat_3 “Avg. Cum. Diff Between Treatments (3rd Treatment)”
cap noi label variable mean_cum_diff_bet_treat_4 “Avg. Cum. Diff Between Treatments (4th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_5 “Avg. Cum. Diff Between Treatments (5th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_6 “Avg. Cum. Diff Between Treatments (6th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_7 “Avg. Cum. Diff Between Treatments (7th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_8 “Avg. Cum. Diff Between Treatments (8th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_9 “Avg. Cum. Diff Between Treatments (9th Treatment)”
cap noi label variable mean_cum_diff_bet_treat_10 “Avg. Cum. Diff Between Treatments (10th Treatment)”
cap noi label variable diff_bet_treat “Días de diferencia con el Tratamiento Posterior/Days of difference between the Next Treatment”
cap noi label variable dias_treat_imp_sin_na “Días de Tratamiento (valores perdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13)/Days of Treatment (missing dates of discharge were replaced with difference from 2019-11-13)”
cap noi label variable dup “Número de Tratamientos por HASH (posición a medida que aparecen, menor más antiguo)/Number of Treatments by User”
cap noi label variable duplicates_filtered “Número de Tratamientos por HASH (Total)/Number of Treatments by User (Total)”
cap noi label variable a_botar “Descarta tratamientos posteriores por usuario(Perdidos= Trat Posteriores)/Discard posterior treatments by each user (Missing= Posterior Treatments)”
cap noi label variable dias_trat_alta_temprana “Si el tratamiento duró 90 días o más (1= <90d)/If the treatment lasted 90 days or more (1= Less than 90d)”
cap noi label variable event “Usuarios con Tratamiento Posterior (1=Reingreso x HASH)/Users with Posterior Treatments (1=Readmission)”
cap noi label variable startdate “Fecha de Ingreso Primer Tratamiento (x HASH)/Date of Admission to the First Treatment (By User)”
cap noi label variable enddate “Fecha de Egreso del Último Tratamiento (x HASH)/Date of Discharge of the Last Treatment (By User)”
cap noi label variable person_days “Días en los que estado un usuario en el sistema para el estudio/User’s Days available in the system for the study”
cap noi label variable person_years “Años en los que estado un usuario en el sistema para el estudio/User’s Years available in the system for the study”
cap noi label variable dg_total_cie_10 “Conteo de Diagnósticos CIE-10(sólo diagnósticos)/Count of ICD-10 Diagnostics(only diagnoses)”
cap noi label variable dg_cie_10_rec “Diagnóstico CIE-10 (1 o más)(Recodificado)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded)”
cap noi label variable dg_total_dsm_iv “Conteo de Diagnósticos DSM-IV(sólo diagnósticos)/Count of DSM-IV Diagnostics(only diagnoses)”
cap noi label variable dg_dsm_iv_rec “Diagnóstico DSM-IV (1 o más)(Recodificado)/Psychiatric Diagnoses (DSM-IV)(one or more)(Recoded)”
cap noi label variable sus_ini_mod_mvv “Sustancia de Inicio (Valor más vulnerable)/Starting Substance (Most vulnerable value)”
cap noi label variable estatus_ocupacional_rec “Condición Ocupacional, corregido por categoría(f)/Occupational Status, corrected for categories(f)”
cap noi label variable hijos_trat_res “Tiene Hijos en Ingreso a Tratamiento Residencial del Último Registro/Have Children in Residential Treatment of the Last Entry”
cap noi label variable dg_fis_anemia “Dignóstico Físico, Anemia/Physical Dg. Anemia”
cap noi label variable dg_fis_card “Dignóstico Físico, Enfermedad Cardiaca/Physical Dg. Heart Disease”
cap noi label variable dg_fis_in_study “Dignóstico Físico, En estudio/Physical Dg. Under Study”
cap noi label variable dg_fis_enf_som “Dignóstico Físico, /Physical Dg. Somatic illnesses”
cap noi label variable dg_fis_ets “Dignóstico Físico, ETSs/Physical Dg. STDs”
cap noi label variable dg_fis_hep_alc “Dignóstico Físico, Hepatitis Alcohólica Subaguda/Physical Dg. Alcoholic hepatitis”
cap noi label variable dg_fis_hep_b “Dignóstico Físico, Hepatitis B, C, D/Physical Dg. Hepatitis B, C, D”
cap noi label variable dg_fis_hep_cro “Dignóstico Físico, Hepatitis Crónica/Physical Dg. Chronic hepatitis”
cap noi label variable dg_fis_inf “Dignóstico Físico, Enfermedades Infeccisas Rel. con Consumo/Physical Dg. Infectuous diseases related to SUDs”
cap noi label variable dg_fis_otr_cond_fis_ries_vit “Dignóstico Físico, Otras condiciones de riesgo vital/Physical Dg. Other phsysical conditions of vital risk”
cap noi label variable dg_fis_otr_cond_fis “Dignóstico Físico, Otras condiciones limitantes/Physical Dg. Other phsysical limitations”
cap noi label variable dg_fis_pat_buc “Dignóstico Físico, Patología Bucal/Physical Dg. Oral-care pathologies”
cap noi label variable dg_fis_pat_ges_intrau “Dignóstico Físico, Patología de la gestión y del niño intrauterino/Physical Dg. Development or Intrauterine”
cap noi label variable dg_fis_trau_sec “Dignóstico Físico, Traumatismos y secuelas secundarias/Physical Dg. Traumatisms and disabling sequelae”
cap noi label variable otros_pr_sm_abu_sex “Otros problemas de Atención de Salud Mental, Abuso Sexual/Other mental health problems, Sexual abuse”
cap noi label variable otros_pr_sm_exp_com_sex “Otros problemas de Atención de Salud Mental, Explotación Sexual Comercial/Other mental health problems, Commercial sexual exploitation”
cap noi label variable otros_pr_sm_otros “Otros problemas de Atención de Salud Mental, Otros/Other mental health problems, Other”
cap noi label variable otros_pr_sm_vif “Otros problemas de Atención de Salud Mental, Violencia Intrafamiliar/Other mental health problems, Domestic violence”
cap noi label variable condicion_ocupacional_corr “Condición Ocupacional Criterio Corregido(f)/Occupational Status Corrected(f)”
cap noi label variable cat_ocupacional_corr “Categoría Ocupacional Criterio Corregido(f)/Occupational Category Corrected(f)”
cap noi save “C:Fondecytunidad (github)_C1_df_dup_SEP_2020.dta”, replace
cap noi drop id id_mod nombre_centro consentimiento_informado
cap noi drop id id_mod nombre_centro
cap noi save “C:Fondecytunidad (github)_C1_df_dup_SEP_2020_exp.dta”, replace
write.table(rbind(paste0('cap noi use "', gsub('/', '\\', path, fixed=T),'\\CONS_C1_df_dup_SEP_2020.dta",clear'),export_lab_stata), file = paste0(path,"/SUD_CL/_label_var_to_stata.do"), sep = "",row.names = FALSE, quote = FALSE, fileEncoding="UTF-8")


. cap noi do _label_var_to_stata. *clear all
. cap noi use "C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2019 (github)\
> CONS_C1_df_dup_SEP_2020.dta",clear

. cap noi label variable row "Numerador de los eventos presentes en la Base de 
> Datos (Último registro)/Events in the Dataset (Last Entry)"
note: label truncated to 80 characters

. cap noi label variable row_cont_entries "Numerador de los eventos presentes e
> n la Base de Datos(*)/Events in the Dataset(*)"
note: label truncated to 80 characters

. cap noi label variable hash_key "Codificación del RUN/Masked Identifier (RUN)
> "

. cap noi label variable hash_rut_completo "HASH alternativo, en el escenario e
> n que se asuma que el individuo al que se le codificó el RUN presente mayor e
> dad/Alternative HASH-Key"
note: label truncated to 80 characters

. cap noi label variable id "Codigo Identificación de SENDA/SENDA ID"

. cap noi label variable id_mod "ID de SENDA para Presentación en Página Web (e
> nmascara caracteres 5 y 6)/SENDA ID (mask characters 5 & 6)"
note: label truncated to 80 characters

. cap noi label variable fech_ing "Fecha de Ingreso a Tratamiento (Primera Entr
> ada)/Date of Admission to Treatment (First Entry)"
note: label truncated to 80 characters

. cap noi label variable fech_egres_imp "Fecha de Egreso (Imputados KNN & Lógic
> o) del Último Registro(b)/Date of Discharge (Imputed KNN & Logic) of the Last
>  Entry(b)"
note: label truncated to 80 characters

. cap noi label variable tipo_de_plan_2_largest_treat "Tipo de Plan del Registr
> o Más Largo entre entradas intermedias(f)/Type of Plan of the Largest Entry A
> mong Intermediate Entries(f)"
note: label truncated to 80 characters

. cap noi label variable tipo_de_plan_2_concat_a "Tipo de Plan(*)/Type of Plan(
> *)"

. cap noi label variable tipo_de_programa_2 "Tipo de Programa del Registro Más 
> Largo entre Entradas Intermedias/Type of Program of the Largest Entry Among I
> ntermediate Entries"
note: label truncated to 80 characters

. cap noi label variable id_centro "ID de Centro(b)/Treatment center ID(b)"

. cap noi label variable nombre_centro "Nombre del Centro de Tratamiento(*)/Tre
> atment Center(*)"

. cap noi label variable id_centro_concat_a "ID de Centro(*)/Treatment center I
> D(*)"

. cap noi label variable tipo_centro "Tipo de Centro del Último Registro/Type o
> f Center of the Last Entry"

. cap noi label variable servicio_de_salud "Servicio de Salud(*)/Health Service
> (*)"

. cap noi label variable senda "SENDA del Último Registro/SENDA of the Last Ent
> ry"

. cap noi label variable numero_de_hijos_mod "Número de Hijos (Valor Max.)/Numb
> er of Children (Max. Value)"

. cap noi label variable num_hijos_trat_res_mod "Número de Hijos para Ingreso a
>  Tratamiento Residencial del Último Registro/Number of Children to Residentia
> l Treatment of the Last Entry"
note: label truncated to 80 characters

. cap noi label variable tipo_centro_derivacion "Tipo de Centro al que el Usuar
> io es Derivado del Último Registro(b)/Type of Center of Derivation of the Las
> t Entry(b)"
note: label truncated to 80 characters

. cap noi label variable macrozona "Macrozona del Centro del Último Registro(b)
> /Macrozones of the Center of the Last Entry(b)"
note: label truncated to 80 characters

. cap noi label variable nombre_region "Región del Centro del Último Registro(b
> )/Chilean Region of the Center of the Last Entry(b)"
note: label truncated to 80 characters

. cap noi label variable comuna_residencia_cod "Comuna de Residencia del Último
>  Registro(b)/Municipality or District of Residence of the Last Entry(b)"
note: label truncated to 80 characters

. cap noi label variable fecha_ingreso_a_convenio_senda "Fecha de Ingreso a Con
> venio SENDA (aún no formateada como fecha) (Primera Entrada)/Date of Admissio
> n to SENDA Agreement (First Entry)"
note: label truncated to 80 characters

. cap noi label variable identidad_de_genero "Identidad de Género (Último Regis
> tro)(b)/Gender Identity (Last Entry)(b)"

. cap noi label variable edad_al_ing "Edad a la Fecha de Ingreso a Tratamiento 
> (numérico continuo) (Primera Entrada)/Age at Admission to Treatment (First En
> try)"
note: label truncated to 80 characters

. cap noi label variable origen_ingreso_mod "Origen de Ingreso (Primera Entrada
> )/Motive of Admission to Treatment (First Entry)"
note: label truncated to 80 characters

. cap noi label variable x_se_trata_mujer_emb "Mujer Embarazada al Ingreso (d)/
> Pregnant at Admission (d)"

. cap noi label variable compromiso_biopsicosocial "Compromiso Biopsicosocial(d
> )/Biopsychosocial Involvement(d)"

. cap noi label variable dg_global_nec_int_soc_or "Diagnóstico Global de Necesi
> dades de Integración Social (Al Ingreso)(d)/Global Diagnosis of Social Integr
> ation (At Admission)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_hum_or "Diagnóstico de Necesidades 
> de Integración Social en Capital Humano (Al Ingreso)(d)/Global Diagnosis of S
> ocial Integration in Human Capital (At Admission)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_fis_or "Diagnóstico de Necesidades 
> de Integración Social en Capital Físico (Al Ingreso)(d)/Global Diagnosis of S
> ocial Integration in Physical Capital (At Admission)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_soc_or "Diagnóstico de Necesidades 
> de Integración Social en Capital Social (Al Ingreso)(d)/Global Diagnosis of S
> ocial Integration in Social Capital (At Admission)(d)"
note: label truncated to 80 characters

. cap noi label variable usuario_tribunal_trat_droga "Usuario de modalidad Trib
> unales de Tratamiento de Drogas(d)/User of Drug Treatment Courts Modality(d)"
note: label truncated to 80 characters

. cap noi label variable evaluacindelprocesoteraputico "Evaluación del Proceso 
> Terapéutico(d)/Evaluation of the Therapeutic Process(d)"

. cap noi label variable eva_consumo "Evaluación al Egreso Respecto al Patrón d
> e consumo(d)/Evaluation at Discharge regarding to Consumption Pattern(d)"
note: label truncated to 80 characters

. cap noi label variable eva_fam "Evaluación al Egreso Respecto a Situación Fam
> iliar(d)/Evaluation at Discharge regarding to Family Situation(d)"
note: label truncated to 80 characters

. cap noi label variable eva_relinterp "Evaluación al Egreso Respecto a Relacio
> nes Interpersonales(d)/Evaluation at Discharge regarding to Interpersonal Rel
> ations(d)"
note: label truncated to 80 characters

. cap noi label variable eva_ocupacion "Evaluación al Egreso Respecto a Situaci
> ón Ocupacional(d)/Evaluation at Discharge regarding to Occupational Status(d)
> "
note: label truncated to 80 characters

. cap noi label variable eva_sm "Evaluación al Egreso Respecto a Salud Mental(d
> )/Evaluation at Discharge regarding to Mental Health(d)"
note: label truncated to 80 characters

. cap noi label variable eva_fisica "Evaluación al Egreso Respecto a Salud Físi
> ca(d)/Evaluation at Discharge regarding to Physical Health(d)"
note: label truncated to 80 characters

. cap noi label variable eva_transgnorma "Evaluación al Egreso Respecto a Trasg
> resión a la Norma Social(d)/Evaluation at Discharge regarding to Transgressio
> n to the Norm(d)"
note: label truncated to 80 characters

. cap noi label variable dg_global_nec_int_soc_or_1 "Diagnóstico Global de Nece
> sidades de Integración Social (Al Egreso)(d)/Global Diagnosis of Social Integ
> ration (At Discharge)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_hum_or_1 "Diagnóstico de Necesidade
> s de Integración Social en Capital Humano (Al Egreso)(d)/Global Diagnosis of 
> Social Integration in Human Capital (At Discharge)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_fis_or_1 "Diagnóstico de Necesidade
> s de Integración Social en Capital Físico (Al Egreso)(d)/Global Diagnosis of 
> Social Integration in Physical Capital (At Discharge)(d)"
note: label truncated to 80 characters

. cap noi label variable dg_nec_int_soc_cap_soc_or_1 "Diagnóstico de Necesidade
> s de Integración Social en Capital Social (Al Egreso)(d)/Global Diagnosis of 
> Social Integration in Social Capital (At Discharge)(d)"
note: label truncated to 80 characters

. cap noi label variable tiene_menores_de_edad_a_cargo "Menores de Edad A Cargo
> (d)/Minor Dependants(d)"

. cap noi label variable ha_estado_embarazada_egreso "¿Ha estado embarazada? (a
> l Egreso)(d)/Have you been Pregnant (at Discharge)(d)"

. cap noi label variable discapacidad "Presenta Discapacidad(d)/Disability(d)"

. cap noi label variable opcion_discapacidad "Origen de Discapacidad(d)/Cause o
> f Disability(d)"

. cap noi label variable escolaridad "Escolaridad: Nivel Eduacional(d)/Educatio
> nal Attainment(d)"

. cap noi label variable edad_al_ing_grupos "Edad a la Fecha de Ingreso a Trata
> miento en Grupos(c)/Age at Admission to Treatment In Groups(c)"
note: label truncated to 80 characters

. cap noi label variable nacionalidad "Nacionalidad/Nationality"

. cap noi label variable sexo_2 "Sexo Usuario/Sex of User"

. cap noi label variable embarazo "Embarazo al Ingreso(c)/Pregnant at Admission
> (c)"

. cap noi label variable fech_nac "Fecha de Nacimiento/Date of Birth"

. cap noi label variable edad_ini_cons "Edad de Inicio de Consumo/Age of Onset 
> of Drug Use"

. cap noi label variable edad_ini_sus_prin "Edad de Inicio de Consumo Sustancia
>  Principal/Age of Onset of Drug Use of Primary Substance"
note: label truncated to 80 characters

. cap noi label variable estado_conyugal_2 "Estado Conyugal/Marital Status"

. cap noi label variable edad_grupos "Edad agrupada/Age in groups"

. cap noi label variable freq_cons_sus_prin "Frecuencia de Consumo de la Sustan
> cia Principal (30 días previos a la admisión)(f)/Frequency of Consumption of 
> the Primary or Main Substance (30 days previous to admission)(f)"
note: label truncated to 80 characters

. cap noi label variable via_adm_sus_prin_act "Vía de Administración de la Sust
> ancia Principal (Se aplicaron criterios de limpieza)(f)/Route of Administrati
> on of the Primary or Main Substance (Tidy)(f)"
note: label truncated to 80 characters

. cap noi label variable etnia_cor "Etnia/Ethnic Group"

. cap noi label variable nacionalidad_2 "Segunda Nacionalidad/Second Nationalit
> y"

. cap noi label variable etnia_cor_2 "Etnia (2)/Second Ethnic Group"

. cap noi label variable sus_ini_2_mod "Segunda Sustancia de Inicio(Sólo más fr
> ecuentes)/Second Starting Substance"

. cap noi label variable sus_ini_3_mod "Tercera Sustancia de Inicio(Sólo más fr
> ecuentes)/Third Starting Substance"

. cap noi label variable sus_ini_mod "Sustancia de Inicio (Sólo más frecuentes)
> /Starting Substance (Only more frequent)"
note: label truncated to 80 characters

. cap noi label variable con_quien_vive "Persona con la que vive el Usuario(f)/
> People that Share Household with the User (Cohabitation Status)(f)"
note: label truncated to 80 characters

. cap noi label variable estatus_ocupacional "Condición Ocupacional(f)/Occupati
> onal Status(f)"

. cap noi label variable cat_ocupacional "Categoría Ocupacional(f)/Occupational
>  Category(f)"

. cap noi label variable sus_principal_mod "Sustancia Principal de Consumo (Sól
> o más frecuentes)(f)/Primary or Main Substance of Consumption at Admission (O
> nly more frequent)(f)"
note: label truncated to 80 characters

. cap noi label variable tipo_de_vivienda_mod "Tipo de Vivienda(f)/Type of Hous
> ing(f)"

. cap noi label variable tenencia_de_la_vivienda_mod "Tenencia de la Vivienda(f
> )/Tenure status of Households(f)"

. cap noi label variable rubro_trabaja_mod "Rubro de Trabajo(f)/Area of Work(f)
> "

. cap noi label variable otras_sus1_mod "Otras Sustancias (1)(Sólo más frecuent
> es)(f)/Other Substances (1)(Only more frequent)(f)"
note: label truncated to 80 characters

. cap noi label variable otras_sus2_mod "Otras Sustancias (2)(Sólo más frecuent
> es)(f)/Other Substances (2)(Only more frequent)(f)"
note: label truncated to 80 characters

. cap noi label variable otras_sus3_mod "Otras Sustancias (3)(Sólo más frecuent
> es)(f)/Other Substances (3)(Only more frequent)(f)"
note: label truncated to 80 characters

. cap noi label variable dg_trs_cons_sus_or "Diagnósico de Trastorno por Consum
> o de Sustancias(d)/Diagnosed of Substance Use Disorder(d)"
note: label truncated to 80 characters

. cap noi label variable diagnostico_trs_fisico "Diagnóstico de Trastorno Físic
> o(g)/Diagnosis of Physical Disorder(g)"

. cap noi label variable otros_probl_at_sm_or "Otros Problemas de Atención Vinc
> ulados a Salud Mental(g)/Other problems linked to Mental Health(g)"
note: label truncated to 80 characters

. cap noi label variable ano_bd_first "Año de la Base de Datos(c)/Year of the D
> ataset (Source)(c)"

. cap noi label variable ano_bd_last "Año de la Base de Datos(b)/Year of the Da
> taset (Source)(b)"

. cap noi label variable obs "Observaciones al Proceso de Limpieza y Estandariz
> ación de Casos(e)/Observations to the Process of Data Tidying & Standardizati
> on(e)"
note: label truncated to 80 characters

. cap noi label variable obs_concat_a "Observaciones al Proceso de Limpieza y E
> standarización de Casos(*)/Observations to the Process of Data Tidying & Stan
> dardization(*)"
note: label truncated to 80 characters

. cap noi label variable rn_common_treats2 "Cuenta de Entradas Comunes(b)/Count
>  of Common Entries(b)"

. cap noi label variable concat_hash_id_treatments "Combination of User & Disti
> nt Entries"

. cap noi label variable at_least_one_cont_entry "Casos de Usuarios con más de 
> una entrada después de otra/Cases of users with more than one entry after ano
> ther one"
note: label truncated to 80 characters

. cap noi label variable senda_concat_a "SENDA(*)/SENDA(*)"

. cap noi label variable tipo_centro_concat_a "Tipo de Centro(*)/Type of Center
> (*)"

. cap noi label variable fech_ing_num "Fecha de Ingreso a Tratamiento (Numérico
> )(c)/Date of Admission to Treatment (Numeric)(c)"
note: label truncated to 80 characters

. cap noi label variable fech_egres_num "Fecha de Egreso (Imputados KNN & Lógic
> o)(Numérico)(b)/Date of Discharge (Imputed KNN & Logic)(Numeric)(b) of the Ne
> xt Treatment"
note: label truncated to 80 characters

. cap noi label variable fech_ing_next_treat "Fecha de Ingreso a Tratamiento (N
> umérico)(c) del Tratamiento Posterior/Date of Admission to Treatment (Numeric
> )(c)"
note: label truncated to 80 characters

. cap noi label variable id_centro_sig_trat "ID del Centro del Tratamiento Post
> erior/Center ID of the Next Treatment"

. cap noi label variable tipo_plan_sig_trat "Tipo de Plan del Tratamiento Poste
> rior/Type of Plan of the Next Treatment"

. cap noi label variable tipo_programa_sig_trat "Tipo de Programa del Tratamien
> to Posterior/Type of Program of the Next Treatment"

. cap noi label variable senda_sig_trat "SENDA del Tratamiento Posterior/SENDA 
> of the Next Treatment"

. cap noi label variable menor_60_dias_diff "Menor a 60 días de diferencia con 
> el Tratamiento Posterior/Menor a 60 days of difference between the Next Treat
> ment"
note: label truncated to 80 characters

. cap noi label variable menor_45_dias_diff "Menor a 45 días de diferencia con 
> el Tratamiento Posterior/Less than 45 days of difference between the Next Tre
> atment"
note: label truncated to 80 characters

. cap noi label variable motivoegreso_derivacion "Motivo de Egreso= Derivación(
> b)/Cause of Discharge= Derivación(b)"

. cap noi label variable abandono_temprano "Abandono temprano(<3 meses)/ Early 
> Drop-out(<3 months)"

. cap noi label variable obs_cambios "Cambios del tratamiento en comparación al
>  Tratamiento Posterior/Changes in treatment compared to the Next Treatment"
note: label truncated to 80 characters

. cap noi label variable obs_cambios_ninguno "Sin cambios del tratamiento en co
> mparación al Tratamiento Posterior/No changes in treatment compared to the Ne
> xt Treatment"
note: label truncated to 80 characters

. cap noi label variable obs_cambios_num "Recuento de cambios del tratamiento e
> n comparación al Tratamiento Posterior/Count of changes in treatment compared
>  to the Next Treatment"
note: label truncated to 80 characters

. cap noi label variable obs_cambios_fac "Recuento de cambios del tratamiento e
> n comparación al Tratamiento Posterior(factor)/Count of changes in treatment 
> compared to the Next Treatment(factor)"
note: label truncated to 80 characters

. cap noi label variable edad_ini_sus_prin_grupos "Edad de Inicio de Consumo Su
> stancia Principal (en Grupos)/Age of Onset of Drug Use of Primary Substance (
> in Groups)"
note: label truncated to 80 characters

. cap noi label variable hash_key_sex_program "Usuarios a los que se le ha camb
> iado el sexo de acuerdo al tipo de plan/Users that changed of sex considering
>  the types of plan"
note: label truncated to 80 characters

. cap noi label variable centro_muj "ID de centro que alude a un centro específ
> ico para mujeres/Center ID aludes to a women-specific center"
note: label truncated to 80 characters

. cap noi label variable cie_10 "Diagnóstico CIE-10 (1 o más)/Psychiatric Diagn
> oses (ICD-10)(one or more)"

. cap noi label variable dsm_iv "Diagnóstico DSM-IV (1 o más)/Psychiatric Diagn
> oses (DSM-IV)(one or more)"

. cap noi label variable con_quien_vive_rec "Persona con la que vive el Usuario
>  (Recodificada)(f)/People that Share Household with the User (Cohabitation St
> atus)(Recoded)(f)"
note: label truncated to 80 characters

. cap noi label variable cnt_mod_dsm_iv_or "Recuento de Valores en Diagnóstico 
> DSM-IV (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric 
> Diagnoses (DSM-IV)"
note: label truncated to 80 characters

. cap noi label variable cnt_mod_cie_10_or "Recuento de Valores en Diagnóstico 
> CIE-10 (Excluye en Estudio si hay más de uno)/Count of Values in Psychiatric 
> Diagnoses (ICD-10)"
note: label truncated to 80 characters

. cap noi label variable cnt_diagnostico_trs_fisico "Recuento de Diagnóstico de
>  Trastorno Físico/Count of Physical Disorder"

. cap noi label variable dg_trs_psiq_sub_dsm_iv_or "Diagnóstico de Trastorno Ps
> iquiátrico, Criterios DSM IV (Subclasificacion)(g)/Diagnosis of Psychiatric D
> isorders, DSM-IV criteria (sub-classification)(g)"
note: label truncated to 80 characters

. cap noi label variable dg_trs_psiq_dsm_iv_or "Diagnóstico de Trastorno Psiqui
> átrico, Criterios DSM IV(g)/Diagnosis of Psychiatric Disorders, DSM-IV criter
> ia(g)"
note: label truncated to 80 characters

. cap noi label variable dg_trs_psiq_cie_10_or "Diagnóstico de Trastorno Psiqui
> átrico, Criterios CIE-10(g)/Diagnosis of Psychiatric Disorders, CIE-10 criter
> ia(g)"
note: label truncated to 80 characters

. cap noi label variable x2_dg_trs_psiq_cie_10_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios CIE-10 (2)(g)/Diagnosis of Psychiatric Disorders, CIE-10
>  criteria (2)(g)"
note: label truncated to 80 characters

. cap noi label variable x3_dg_trs_psiq_cie_10_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios CIE-10 (3)(g)/Diagnosis of Psychiatric Disorders, CIE-10
>  criteria (3)(g)"
note: label truncated to 80 characters

. cap noi label variable x4_dg_trs_psiq_cie_10_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios CIE-10 (4)(g)/Diagnosis of Psychiatric Disorders, CIE-10
>  criteria (4)(g)"
note: label truncated to 80 characters

. cap noi label variable x5_dg_trs_psiq_cie_10_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios CIE-10 (5)(g)/Diagnosis of Psychiatric Disorders, CIE-10
>  criteria (5)(g)"
note: label truncated to 80 characters

. cap noi label variable x2_dg_trs_psiq_dsm_iv_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios DSM IV (2)(g)/Diagnosis of Psychiatric Disorders, DSM-IV
>  criteria (2)(g)"
note: label truncated to 80 characters

. cap noi label variable x3_dg_trs_psiq_dsm_iv_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios DSM IV (3)(g)/Diagnosis of Psychiatric Disorders, DSM-IV
>  criteria (3)(g)"
note: label truncated to 80 characters

. cap noi label variable x4_dg_trs_psiq_dsm_iv_or "Diagnóstico de Trastorno Psi
> quiátrico, Criterios DSM IV (4)(g)/Diagnosis of Psychiatric Disorders, DSM-IV
>  criteria (4)(g)"
note: label truncated to 80 characters

. cap noi label variable dg_trs_psiq_sub_cie_10_or "Diagnóstico de Trastorno Ps
> iquiátrico, Criterios CIE-10 (Subclasificacion)(g)/Diagnosis of Psychiatric D
> isorders, CIE-10 criteria (subclassification)(g)"
note: label truncated to 80 characters

. cap noi label variable x2_dg_trs_psiq_sub_cie_10_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios CIE-10 (Subclasificacion) (2)(g)/Diagnosis of Psychi
> atric Disorders, CIE-10 criteria (subclassification) (2)(g)"
note: label truncated to 80 characters

. cap noi label variable x3_dg_trs_psiq_sub_cie_10_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios CIE-10 (Subclasificacion) (3)(g)/Diagnosis of Psychi
> atric Disorders, CIE-10 criteria (subclassification) (3)(g)"
note: label truncated to 80 characters

. cap noi label variable x4_dg_trs_psiq_sub_cie_10_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios CIE-10 (Subclasificacion)(4)(g)/Diagnosis of Psychia
> tric Disorders, CIE-10 criteria (subclassification)(4)(g)"
note: label truncated to 80 characters

. cap noi label variable x2_dg_trs_psiq_sub_dsm_iv_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios DSM IV (Subclasificacion) (2)(g)/Diagnosis of Psychi
> atric Disorders, DSM-IV criteria (sub-classification) (2)(g)"
note: label truncated to 80 characters

. cap noi label variable x3_dg_trs_psiq_sub_dsm_iv_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios DSM IV (Subclasificacion) (3)(g)/Diagnosis of Psychi
> atric Disorders, DSM-IV criteria (sub-classification) (3)(g)"
note: label truncated to 80 characters

. cap noi label variable x4_dg_trs_psiq_sub_dsm_iv_or "Diagnóstico de Trastorno
>  Psiquiátrico, Criterios DSM IV (Subclasificacion)(4)(g)/Diagnosis of Psychia
> tric Disorders, DSM-IV criteria (sub-classification)(4)(g)"
note: label truncated to 80 characters

. cap noi label variable cnt_otros_probl_at_sm_or "Recuento de Otros Problemas 
> de Atención Vinculados a Salud Mental/Count of Other problems linked to Menta
> l Health"
note: label truncated to 80 characters

. cap noi label variable escolaridad_rec "Escolaridad: Nivel Eduacional(d) Norm
> alizado a Progresión de Tratamientos/Educational Attainment(d) & Normalized F
> ollowing Progression of Treatments"
note: label truncated to 80 characters

. cap noi label variable tipo_de_plan_2 "Tipo de Plan del Último Registro/Type 
> of Plan of the Last Entry"

. cap noi label variable motivodeegreso_mod_imp "Motivo de Egreso (con abandono
>  temprano y tardío)(Imputados KNN & Lógico) del Último Registro(b)/Cause of D
> ischarge (with late and early withdrawal)(Imputed KNN & Logic) of the Last En
> try(b)"
note: label truncated to 80 characters

. cap noi label variable rn_hash "Número de Tratamientos por Usuario (menor, tr
> atamiento más antiguo)/Number of Treatments by User (less, older treatment)"
note: label truncated to 80 characters

. cap noi label variable n_hash "Número total de Tratamientos por Usuario/Total
>  Number of Treatments by User"

. cap noi label variable cum_dias_trat_sin_na "Suma acumulada de Días de Tratam
> iento por Usuario/Cumulative Days of Treatment by User"
note: label truncated to 80 characters

. cap noi label variable mean_cum_dias_trat_sin_na "Promedio acumulado de Días 
> de Tratamiento por Usuario/Cumulative Average Days of Treatment by User"
note: label truncated to 80 characters

. cap noi label variable cum_diff_bet_treat "Suma acumulada de Diferencia en Dí
> as con Tratamiento Siguiente por Usuario/Cumulative sum of Days of difference
>  between the Next Treatment by User"
note: label truncated to 80 characters

. cap noi label variable mean_cum_diff_bet_treat "Promedio acumulado de Diferen
> cia en Días entre Tratamientos por Usuario/Cumulative Average Days of Differe
> nces Between Treatments By User"
note: label truncated to 80 characters

. cap noi label variable tipo_de_plan_2_1 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (1st Treatment)"

. cap noi label variable tipo_de_plan_2_2 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (2nd Treatment)"

. cap noi label variable tipo_de_plan_2_3 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (3rd Treatment)"

. cap noi label variable tipo_de_plan_2_4 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (4th Treatment)"

. cap noi label variable tipo_de_plan_2_5 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (5th Treatment)"

. cap noi label variable tipo_de_plan_2_6 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (6th Treatment)"

. cap noi label variable tipo_de_plan_2_7 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (7th Treatment)"

. cap noi label variable tipo_de_plan_2_8 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (8th Treatment)"

. cap noi label variable tipo_de_plan_2_9 "Tipo de Plan del Último Registro/Typ
> e of Plan of the Last Entry (9th Treatment)"

. cap noi label variable tipo_de_plan_2_10 "Tipo de Plan del Último Registro/Ty
> pe of Plan of the Last Entry (10th Treatment)"

. cap noi label variable motivodeegreso_mod_imp_1 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (1st Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_2 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (2nd Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_3 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (3th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_4 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (4th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_5 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (5th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_6 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (6th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_7 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (7th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_8 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (8th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_9 "Cause of Discharge (with lat
> e and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (9th Treatm
> ent)"
note: label truncated to 80 characters

. cap noi label variable motivodeegreso_mod_imp_10 "Cause of Discharge (with la
> te and early withdrawal)(Imputed KNN & Logic) of the Last Entry(b) (10th Trea
> tment)"
note: label truncated to 80 characters

. cap noi label variable dias_treat_imp_sin_na_1 "Days of Treatment (1st Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_2 "Days of Treatment (2nd Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_3 "Days of Treatment (3rd Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_4 "Days of Treatment (4th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_5 "Days of Treatment (5th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_6 "Days of Treatment (6th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_7 "Days of Treatment (7th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_8 "Days of Treatment (8th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_9 "Days of Treatment (9th Treatm
> ent)"

. cap noi label variable dias_treat_imp_sin_na_10 "Days of Treatment (10th Trea
> tment)"

. cap noi label variable diff_bet_treat_1 "Diff Between Treatments (1st Treatme
> nt)"

. cap noi label variable diff_bet_treat_2 "Diff Between Treatments (2nd Treatme
> nt)"

. cap noi label variable diff_bet_treat_3 "Diff Between Treatments (3rd Treatme
> nt)"

. cap noi label variable diff_bet_treat_4 "Diff Between Treatments (4th Treatme
> nt)"

. cap noi label variable diff_bet_treat_5 "Diff Between Treatments (5th Treatme
> nt)"

. cap noi label variable diff_bet_treat_6 "Diff Between Treatments (6th Treatme
> nt)"

. cap noi label variable diff_bet_treat_7 "Diff Between Treatments (7th Treatme
> nt)"

. cap noi label variable diff_bet_treat_8 "Diff Between Treatments (8th Treatme
> nt)"

. cap noi label variable diff_bet_treat_9 "Diff Between Treatments (9th Treatme
> nt)"

. cap noi label variable diff_bet_treat_10 "Diff Between Treatments (10th Treat
> ment)"

. cap noi label variable cum_dias_trat_sin_na_1 "Cum. Days of Treatment (1st Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_2 "Cum. Days of Treatment (2nd Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_3 "Cum. Days of Treatment (3rd Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_4 "Cum. Days of Treatment (4th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_5 "Cum. Days of Treatment (5th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_6 "Cum. Days of Treatment (6th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_7 "Cum. Days of Treatment (7th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_8 "Cum. Days of Treatment (8th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_9 "Cum. Days of Treatment (9th Tr
> eatment)"

. cap noi label variable cum_dias_trat_sin_na_10 "Cum. Days of Treatment (10th 
> Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_1 "Avg. Cum. Days of Treatme
> nt (1st Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_2 "Avg. Cum. Days of Treatme
> nt (2nd Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_3 "Avg. Cum. Days of Treatme
> nt (3rd Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_4 "Avg. Cum. Days of Treatme
> nt (4th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_5 "Avg. Cum. Days of Treatme
> nt (5th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_6 "Avg. Cum. Days of Treatme
> nt (6th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_7 "Avg. Cum. Days of Treatme
> nt (7th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_8 "Avg. Cum. Days of Treatme
> nt (8th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_9 "Avg. Cum. Days of Treatme
> nt (9th Treatment)"

. cap noi label variable mean_cum_dias_trat_sin_na_10 "Avg. Cum. Days of Treatm
> ent (10th Treatment)"

. cap noi label variable cum_diff_bet_treat_1 "Cum. Diff Between Treatments (1s
> t Treatment)"

. cap noi label variable cum_diff_bet_treat_2 "Cum. Diff Between Treatments (2n
> d Treatment)"

. cap noi label variable cum_diff_bet_treat_3 "Cum. Diff Between Treatments (3r
> d Treatment)"

. cap noi label variable cum_diff_bet_treat_4 "Cum. Diff Between Treatments (4t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_5 "Cum. Diff Between Treatments (5t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_6 "Cum. Diff Between Treatments (6t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_7 "Cum. Diff Between Treatments (7t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_8 "Cum. Diff Between Treatments (8t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_9 "Cum. Diff Between Treatments (9t
> h Treatment)"

. cap noi label variable cum_diff_bet_treat_10 "Cum. Diff Between Treatments (1
> 0th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_1 "Avg. Cum. Diff Between Trea
> tments (1st Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_2 "Avg. Cum. Diff Between Trea
> tments (2nd Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_3 "Avg. Cum. Diff Between Trea
> tments (3rd Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_4 "Avg. Cum. Diff Between Trea
> tments (4th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_5 "Avg. Cum. Diff Between Trea
> tments (5th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_6 "Avg. Cum. Diff Between Trea
> tments (6th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_7 "Avg. Cum. Diff Between Trea
> tments (7th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_8 "Avg. Cum. Diff Between Trea
> tments (8th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_9 "Avg. Cum. Diff Between Trea
> tments (9th Treatment)"

. cap noi label variable mean_cum_diff_bet_treat_10 "Avg. Cum. Diff Between Tre
> atments (10th Treatment)"

. cap noi label variable diff_bet_treat "Días de diferencia con el Tratamiento 
> Posterior/Days of difference between the Next Treatment"
note: label truncated to 80 characters

. cap noi label variable dias_treat_imp_sin_na "Días de Tratamiento (valores pe
> rdidos en la fecha de egreso se reemplazaron por la diferencia con 2019-11-13
> )/Days of Treatment (missing dates of discharge were replaced with difference
>  from 2019-11-13)"
note: label truncated to 80 characters

. cap noi label variable dup "Número de Tratamientos por HASH (posición a medid
> a que aparecen, menor más antiguo)/Number of Treatments by User"
note: label truncated to 80 characters

. cap noi label variable duplicates_filtered "Número de Tratamientos por HASH (
> Total)/Number of Treatments by User (Total)"

. cap noi label variable a_botar "Descarta tratamientos posteriores por usuario
> (Perdidos= Trat Posteriores)/Discard posterior treatments by each user (Missi
> ng= Posterior Treatments)"
note: label truncated to 80 characters

. cap noi label variable dias_trat_alta_temprana "Si el tratamiento duró 90 día
> s o más (1= <90d)/If the treatment lasted 90 days or more (1= Less than 90d)"
note: label truncated to 80 characters

. cap noi label variable event "Usuarios con Tratamiento Posterior (1=Reingreso
>  x HASH)/Users with Posterior Treatments (1=Readmission)"
note: label truncated to 80 characters

. cap noi label variable startdate "Fecha de Ingreso Primer Tratamiento (x HASH
> )/Date of Admission to the First Treatment (By User)"
note: label truncated to 80 characters

. cap noi label variable enddate "Fecha de Egreso del Último Tratamiento (x HAS
> H)/Date of Discharge of the Last Treatment (By User)"
note: label truncated to 80 characters

. cap noi label variable person_days "Días en los que estado un usuario en el s
> istema para el estudio/User's Days available in the system for the study"
note: label truncated to 80 characters

. cap noi label variable person_years "Años en los que estado un usuario en el 
> sistema para el estudio/User's Years available in the system for the study"
note: label truncated to 80 characters

. cap noi label variable dg_total_cie_10 "Conteo de Diagnósticos CIE-10(sólo di
> agnósticos)/Count of ICD-10 Diagnostics(only diagnoses)"
note: label truncated to 80 characters

. cap noi label variable dg_cie_10_rec "Diagnóstico CIE-10 (1 o más)(Recodifica
> do)/Psychiatric Diagnoses (ICD-10)(one or more)(Recoded)"
note: label truncated to 80 characters

. cap noi label variable dg_total_dsm_iv "Conteo de Diagnósticos DSM-IV(sólo di
> agnósticos)/Count of DSM-IV Diagnostics(only diagnoses)"
note: label truncated to 80 characters

. cap noi label variable dg_dsm_iv_rec "Diagnóstico DSM-IV (1 o más)(Recodifica
> do)/Psychiatric Diagnoses (DSM-IV)(one or more)(Recoded)"
note: label truncated to 80 characters

. cap noi label variable sus_ini_mod_mvv "Sustancia de Inicio (Valor más vulner
> able)/Starting Substance (Most vulnerable value)"
note: label truncated to 80 characters

. cap noi label variable estatus_ocupacional_rec "Condición Ocupacional, correg
> ido por categoría(f)/Occupational Status, corrected for categories(f)"
note: label truncated to 80 characters

. cap noi label variable hijos_trat_res "Tiene Hijos en Ingreso a Tratamiento R
> esidencial del Último Registro/Have Children in Residential Treatment of the 
> Last Entry"
note: label truncated to 80 characters

. cap noi label variable dg_fis_anemia "Dignóstico Físico, Anemia/Physical Dg. 
> Anemia"

. cap noi label variable dg_fis_card "Dignóstico Físico, Enfermedad Cardiaca/Ph
> ysical Dg. Heart Disease"

. cap noi label variable dg_fis_in_study "Dignóstico Físico, En estudio/Physica
> l Dg. Under Study"

. cap noi label variable dg_fis_enf_som "Dignóstico Físico, /Physical Dg. Somat
> ic illnesses"

. cap noi label variable dg_fis_ets "Dignóstico Físico, ETSs/Physical Dg. STDs"

. cap noi label variable dg_fis_hep_alc "Dignóstico Físico, Hepatitis Alcohólic
> a Subaguda/Physical Dg. Alcoholic hepatitis"
note: label truncated to 80 characters

. cap noi label variable dg_fis_hep_b "Dignóstico Físico, Hepatitis B, C, D/Phy
> sical Dg. Hepatitis B, C, D"

. cap noi label variable dg_fis_hep_cro "Dignóstico Físico, Hepatitis Crónica/P
> hysical Dg. Chronic hepatitis"

. cap noi label variable dg_fis_inf "Dignóstico Físico, Enfermedades Infeccisas
>  Rel. con Consumo/Physical Dg. Infectuous diseases related to SUDs"
note: label truncated to 80 characters

. cap noi label variable dg_fis_otr_cond_fis_ries_vit "Dignóstico Físico, Otras
>  condiciones de riesgo vital/Physical Dg. Other phsysical conditions of vital
>  risk"
note: label truncated to 80 characters

. cap noi label variable dg_fis_otr_cond_fis "Dignóstico Físico, Otras condicio
> nes limitantes/Physical Dg. Other phsysical limitations"
note: label truncated to 80 characters

. cap noi label variable dg_fis_pat_buc "Dignóstico Físico, Patología Bucal/Phy
> sical Dg. Oral-care pathologies"

. cap noi label variable dg_fis_pat_ges_intrau "Dignóstico Físico, Patología de
>  la gestión y del niño intrauterino/Physical Dg. Development or Intrauterine"
note: label truncated to 80 characters

. cap noi label variable dg_fis_trau_sec "Dignóstico Físico, Traumatismos y sec
> uelas secundarias/Physical Dg. Traumatisms and disabling sequelae"
note: label truncated to 80 characters

. cap noi label variable otros_pr_sm_abu_sex "Otros problemas de Atención de Sa
> lud Mental, Abuso Sexual/Other mental health problems, Sexual abuse"
note: label truncated to 80 characters

. cap noi label variable otros_pr_sm_exp_com_sex "Otros problemas de Atención d
> e Salud Mental, Explotación Sexual Comercial/Other mental health problems, Co
> mmercial sexual exploitation"
note: label truncated to 80 characters

. cap noi label variable otros_pr_sm_otros "Otros problemas de Atención de Salu
> d Mental, Otros/Other mental health problems, Other"
note: label truncated to 80 characters

. cap noi label variable otros_pr_sm_vif "Otros problemas de Atención de Salud 
> Mental, Violencia Intrafamiliar/Other mental health problems, Domestic violen
> ce"
note: label truncated to 80 characters

. cap noi label variable condicion_ocupacional_corr "Condición Ocupacional Crit
> erio Corregido(f)/Occupational Status Corrected(f)"

. cap noi label variable cat_ocupacional_corr "Categoría Ocupacional Criterio C
> orregido(f)/Occupational Category Corrected(f)"

. cap noi save "C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2019 (github)
> \CONS_C1_df_dup_SEP_2020.dta", replace
file C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2019 (github)\CONS_C1_df
> _dup_SEP_2020.dta saved

. cap noi drop id id_mod nombre_centro consentimiento_informado
variable consentimiento_informado not found

. cap noi drop id id_mod nombre_centro

. cap noi save "C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2019 (github)
> \CONS_C1_df_dup_SEP_2020_exp.dta", replace
file C:\Users\CISS Fondecyt\Mi unidad\Alvacast\SISTRAT 2019 (github)\CONS_C1_df
> _dup_SEP_2020_exp.dta saved

. 
end of do-file



Session Info

sessionInfo()
R version 4.0.2 (2020-06-22)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 19042)

Matrix products: default

locale:
[1] LC_COLLATE=Spanish_Chile.1252  LC_CTYPE=Spanish_Chile.1252   
[3] LC_MONETARY=Spanish_Chile.1252 LC_NUMERIC=C                  
[5] LC_TIME=Spanish_Chile.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] gdtools_0.2.2           lubridate_1.7.9         compareGroups_4.5.1    
 [4] magrittr_1.5            DiagrammeR_1.0.6.1.9000 flexsurv_2.0           
 [7] muhaz_1.2.6.1           survMisc_0.5.5          ggfortify_0.4.10       
[10] rateratio.test_1.0-2    survminer_0.4.8         ggpubr_0.3.0           
[13] epiR_1.0-15             forcats_0.5.0           purrr_0.3.4            
[16] readr_1.3.1             tibble_3.0.1            tidyverse_1.3.0        
[19] choroplethrAdmin1_1.1.1 choroplethrMaps_1.0.1   acs_2.1.4              
[22] XML_3.99-0.3            tidylog_1.0.1           dplyr_1.0.0            
[25] treemapify_2.5.3        ggiraph_0.7.0           chilemapas_0.2         
[28] sf_0.9-3                finalfit_1.0.1          lsmeans_2.30-0         
[31] emmeans_1.4.7           RColorBrewer_1.1-2      panelr_0.7.3           
[34] lme4_1.1-23             Matrix_1.2-18           data.table_1.12.8      
[37] codebook_0.9.2          devtools_2.3.0          usethis_1.6.1          
[40] sqldf_0.4-11            RSQLite_2.2.0           gsubfn_0.7             
[43] proto_1.0.0             car_3.0-12              carData_3.0-5          
[46] broom_0.7.12            zoo_1.8-8               rbokeh_0.5.1           
[49] janitor_2.0.1           plotly_4.9.2.1          kableExtra_1.1.0       
[52] Hmisc_4.4-0             Formula_1.2-3           lattice_0.20-41        
[55] ggplot2_3.3.2           stringr_1.4.0           stringi_1.4.6          
[58] tidyr_1.1.0             knitr_1.29              matrixStats_0.56.0     
[61] boot_1.3-28             Statamarkdown_0.4.5     biostat3_0.1.6         
[64] MASS_7.3-51.6           survival_3.2-3          here_0.1               

loaded via a namespace (and not attached):
  [1] estimability_1.3      coda_0.19-4           acepack_1.4.1        
  [4] bit64_0.9-7           multcomp_1.4-13       rpart_4.1-15         
  [7] generics_0.0.2        callr_3.7.0           TH.data_1.0-10       
 [10] mice_3.9.0            ggfittext_0.9.0       chron_2.3-55         
 [13] bit_1.1-15.2          webshot_0.5.2         xml2_1.3.2           
 [16] httpuv_1.5.4          assertthat_0.2.1      xfun_0.29            
 [19] hms_0.5.3             jquerylib_0.1.4       data.tree_0.7.11     
 [22] evaluate_0.14         promises_1.1.1        dbplyr_1.4.4         
 [25] readxl_1.3.1          randomizr_0.20.0      Rsolnp_1.16          
 [28] km.ci_0.5-2           DBI_1.1.0             tmvnsim_1.0-2        
 [31] htmlwidgets_1.5.1     jsonvalidate_1.1.0    ellipsis_0.3.1       
 [34] import_1.1.0          backports_1.1.8       V8_3.1.0             
 [37] markdown_1.1          vctrs_0.3.1           remotes_2.4.2        
 [40] abind_1.4-5           withr_2.4.3           pryr_0.1.4           
 [43] HardyWeinberg_1.6.6   checkmate_2.0.0       ggmap_3.0.0          
 [46] prettyunits_1.1.1     mnormt_2.0.0          cluster_2.1.0        
 [49] lazyeval_0.2.2        crayon_1.3.4          crul_0.9.0           
 [52] labeling_0.3          pkgconfig_2.0.3       units_0.6-6          
 [55] nlme_3.1-148          pkgload_1.1.0         nnet_7.3-14          
 [58] RJSONIO_1.3-1.4       rlang_1.0.1           lifecycle_0.2.0      
 [61] sandwich_2.5-1        httpcode_0.3.0        modelr_0.1.8         
 [64] cellranger_1.1.0      tcltk_4.0.2           rprojroot_1.3-2      
 [67] flextable_0.5.10      shinyFiles_0.8.0.9003 KMsurv_0.1-5         
 [70] reprex_2.0.1          base64enc_0.1-3       processx_3.5.2       
 [73] rjson_0.2.20          png_0.1-7             viridisLite_0.3.0    
 [76] clisymbols_1.2.0      mstate_0.2.12         bitops_1.0-7         
 [79] KernSmooth_2.23-17    visNetwork_2.0.9      pander_0.6.3         
 [82] blob_1.2.2            classInt_0.4-3        rstatix_0.5.0        
 [85] jpeg_0.1-8.1          shinyAce_0.4.1        ggsignif_0.6.0       
 [88] scales_1.1.1          memoise_1.1.0         plyr_1.8.6           
 [91] hexbin_1.28.1         compiler_4.0.2        snakecase_0.11.0     
 [94] cli_3.1.1             patchwork_1.0.1       ps_1.3.3             
 [97] htmlTable_2.0.0       tidyselect_1.1.0      highr_0.8            
[100] jtools_2.0.5          yaml_2.2.1            radiant.model_1.3.12 
[103] latticeExtra_0.6-29   ggrepel_0.8.2         grid_4.0.2           
[106] tools_4.0.2           rmapshaper_0.4.4      rio_0.5.16           
[109] RgoogleMaps_1.4.5.3   parallel_4.0.2        rstudioapi_0.11      
[112] uuid_0.1-4            foreign_0.8-80        NeuralNetTools_1.5.2 
[115] gridExtra_2.3         pdp_0.7.0             gistr_0.5.0          
[118] farver_2.0.3          digest_0.6.25         shiny_1.5.0          
[121] quadprog_1.5-8        geojsonlint_0.4.0     Rcpp_1.0.4.6         
[124] later_1.1.0.1         writexl_1.3           httr_1.4.2           
[127] WDI_2.6.0             psych_1.9.12.31       colorspace_1.4-1     
[130] rvest_0.3.5           fs_1.4.2              radiant.data_1.3.9   
[133] ranger_0.12.1         truncnorm_1.0-8       splines_4.0.2        
[136] statmod_1.4.34        sp_1.4-2              xgboost_1.1.1.1      
[139] sessioninfo_1.1.1     systemfonts_0.2.3     xtable_1.8-4         
[142] jsonlite_1.7.0        nloptr_1.2.2.1        testthat_2.3.2       
[145] R6_2.4.1              pillar_1.4.6          htmltools_0.5.2      
[148] mime_0.9              glue_1.4.1            fastmap_1.1.0        
[151] minqa_1.2.4           deSolve_1.28          class_7.3-17         
[154] codetools_0.2-18      maps_3.3.0            pkgbuild_1.1.0       
[157] mvtnorm_1.1-1         numDeriv_2016.8-1.1   BiasedUrn_1.07       
[160] curl_4.3              officer_0.3.11        openxlsx_4.1.5       
[163] zip_2.0.4             rmarkdown_2.11        desc_1.2.0           
[166] munsell_0.5.0         e1071_1.7-3           labelled_2.5.0       
[169] haven_2.3.1           reshape2_1.4.4        gtable_0.3.0