Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Help break my aggregate data

    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input int year_reported str14 type_cases_reported str4 gender_children byte number_cases
    2019 "Neglect"        "girl"  1
    2019 "Neglect"        "boy"   1
    2020 "Child Marriage" "girl"  1
    2020 "Physical Abuse" "boy"   1
    2019 "Defilement"     "girl"  2
    2021 "Child Labour"   "boy"   2
    2020 "Physical Abuse" "girl"  4
    2021 "Physical Abuse" "boy"   4
    2020 "Defilement"     "girl"  7
    2020 "Neglect"        "boy"   8
    2019 "Physical Abuse" "boy"   8
    2019 "Physical Abuse" "girl"  8
    2021 "Physical Abuse" "girl" 13
    2021 "Defilement"     "girl" 15
    2021 "Neglect"        "girl" 18
    2021 "Neglect"        "boy"  18
    end
    /* CODE TO ANALYSE DATA FOR AFRICAN INLAND MISSION - KENYA
    DATE CREATED:02-Feb-2022
    DATE UPDATED:
    STATA Version: 15
    PURPOSE: Surmarise and clean the data for the report for the survey

    */
    clear

    cap set maxvar 32767

    set more off

    //set the global paths for the data
    global drive P:\Stata

    *** define the global variables
    //define the paths based on the location of data in your laptop
    global DATA ${drive}\Data\Lovender
    global CODES ${drive}\codes
    global OUTPUT ${drive}\Output\Lovender

    // import the excel file for where its saved

    import excel using "${DATA}\Baseline_Assessment_Survey_results.xl sx", sheet("Baseline_Assessment_Survey_resu") firstrow clear

    // Recode the Cases reported at Police station
    rename Cases_policeyr_report year_reported
    rename Cases_policecases_report type_cases_reported
    rename Cases_policegender_report gender_children
    rename Cases_policenumber_of_cases number_cases
    rename Cases_officersteacher_bearer reported_teachers
    rename Cases_officerspoliceofficer_bea reported_police
    rename Cases_officerschief_bearer reported_chief
    rename Cases_officersrelative_bearer reported_relative
    rename Cases_officerssibling_bearer reported_sibling
    rename Cases_officersparent_bearer reported_parent

    // recode the type of cases
    replace type_cases_reported = "Child Labour" if type_cases_reported=="c_lab"
    replace type_cases_reported = "Child Marriage" if type_cases_reported=="c_mar"
    replace type_cases_reported = "Defilement" if type_cases_reported=="def"
    replace type_cases_reported = "Neglect" if type_cases_reported=="neg"
    replace type_cases_reported = "Physical Abuse" if type_cases_reported=="phy"


    // handle the cases
    drop if number_cases==0

    // recode the cases
    gen cases = 1 if number_cases==1
    replace cases = 2 if number_cases==2
    replace cases = 4 if number_cases==4
    replace cases = 7 if number_cases==7
    replace cases = 8 if number_cases==8
    replace cases = 13 if number_cases==13
    replace cases = 15 if number_cases==15
    replace cases = 18 if number_cases==18

    bysort cases : gen or_test=_n
    bysort cases : gen gen_test=_N

    gen indvid_cases =.
    **gen reported_cas = 0
    set trace on
    gen d =0

    // create a loop to create single records of the cases
    // I am getting stuck in this section of the LOOP, its not listing all 111 cases, my code is ending up with 16.
    foreach y of numlist 1 2 4 7 8 13 15 18 {

    disp `y'

    }

    forvalues j = 1/`y' {
    replace indvid_cases=1
    }

  • #2
    From the comments near the end of your code, I infer that you want to take the existing data, where each observation represents a group of cases, and go to a larger data set where each observation represents an individual case. The code that follows, however, doesn't even seem to be related to that, so I'm not sure what you really want. Anyway, ignoring the code, and taking the comment "create a loop to create single records of the cases" as your goal:
    Code:
    expand number_cases
    drop number_cases
    will do precisely that.

    Notice by the way that, as is so often the case in questions on this forum about loops, no loop is required.

    Added: Although it is probably not directly related to the question posed, I note that there are several lines of code under the comment "recode the cases" creating a new variable cases. I don't see why that new variable is even needed. But assuming that there is a good reason to have it, because no actual recoding is taking place, all of that code can be replaced with just one line: -clonevar cases = number_cases-.
    Last edited by Clyde Schechter; 10 Mar 2022, 15:43.

    Comment


    • #3
      Wow. Thank you so much for the input, yes your code is what I needed to help me with this process. Thank you so much for the insights which have made me see the need to improve on my use of STATA. Thank you so much for this insights

      Comment

      Working...
      X