Announcement

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

  • Subset data and save "output" in different files from a large dataset

    Dear all

    Please assist. I have multiple variables in one long large dataset, with same format that I would like to extract using the "keep" command, and for each group of variables I save/ output them in different files using one procedure. I have been doing this separately for more than 30 variables. Example data for "subst" and "self-harm" is shown below. I have other variables in the large dataset similar to ".....subst" e.g. ".....organic", ".......schizo" etc....

    Is there a way of running syntax with one procedure and saving the subset stata files for each mental health condition?

    From each group I run keep as follows:

    "keep personid delay10 tot_mh_substYR sum_losYR_mh_subst seq_tot_mh_substYR" and save in a separate "subst" file
    "keep personid delay10 tot_mh_selfharmYR sum_losYR_mh_selfharm seq_tot_mh_selfharmYR" and save in a separate "subst" file


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input double personid float(delay10 tot_mh_substYR sum_losYR_mh_subst seq_tot_mh_substYR tot_mh_selfharmYR sum_losYR_mh_selfharm seq_tot_mh_selfharmYR)
     2337  1 .  . . .  . .
     2337  2 .  . . .  . .
     2337  4 .  . . .  . .
     2337  4 .  . . .  . .
     2337  4 .  . . .  . .
     2337  4 .  . . .  . .
     2337  5 .  . . .  . .
     2337  5 .  . . .  . .
     3172 -1 .  . . .  . .
     3172  0 5 15 1 7  8 1
     3172  0 .  . . 7  8 2
     3172  0 .  . . 7  8 3
     3172  0 .  . . .  . .
     3172  0 .  . . 7  8 4
     3172  0 .  . . 7  8 5
     3172  0 .  . . 7  8 6
     3172  1 .  . . .  . .
     3172  2 1 10 1 .  . .
     3172  3 .  . . .  . .
     3172  4 2  2 1 .  . .
     3172  4 2  2 2 .  . .
     3172  5 1  1 1 .  . .
     3172  0 5 15 2 .  . .
     3172  0 5 15 3 .  . .
     3172  0 5 15 4 .  . .
     3172  0 5 15 5 7  8 7
     3653  1 .  . . .  . .
     3653  2 .  . . .  . .
     3653  2 .  . . .  . .
     3653  3 .  . . .  . .
     3653  4 .  . . .  . .
     3653  5 .  . . .  . .
     3653  5 .  . . .  . .
     3653  6 .  . . .  . .
     5625  0 .  . . .  . .
     5625  0 1  0 1 .  . .
     5625  1 .  . . 2  6 1
     5625  1 2 34 1 .  . .
     5625  1 .  . . .  . .
     5625  1 2 34 2 2  6 2
     5625  3 1 26 1 .  . .
     5625  5 .  . . .  . .
    10045  5 .  . . .  . .
    11758 -1 .  . . .  . .
    11758 -1 1  1 1 .  . .
    11758  0 8 68 1 7 44 1
    11758  0 .  . . .  . .
    11758  0 .  . . 7 44 2
    11758  0 8 68 2 .  . .
    11758  0 .  . . 7 44 3
    11758  0 8 68 3 .  . .
    11758  0 8 68 4 .  . .
    11758  0 .  . . .  . .
    11758  0 8 68 5 7 44 4
    11758  0 8 68 6 7 44 5
    11758  0 .  . . 7 44 6
    11758  0 8 68 7 7 44 7
    11758  0 8 68 8 .  . .
    11758  0 .  . . .  . .
    11758  1 .  . . .  . .
    11758  1 2  6 1 .  . .
    11758  1 .  . . .  . .
    11758  1 .  . . 4  5 2
    11758  1 .  . . 4  5 1
    11758  1 .  . . 4  5 3
    11758  1 .  . . .  . .
    11758  1 .  . . .  . .
    11758  1 .  . . 4  5 4
    11758  1 2  6 2 .  . .
    11758  2 4 16 1 .  . .
    11758  2 .  . . .  . .
    11758  2 .  . . 2  3 1
    11758  2 .  . . 2  3 2
    11758  2 .  . . .  . .
    11758  2 .  . . .  . .
    11758  2 4 16 2 .  . .
    11758  2 4 16 3 .  . .
    11758  3 1  2 1 1  2 1
    11758  2 4 16 4 .  . .
    14193 -1 .  . . .  . .
    14193  0 .  . . .  . .
    14193  4 .  . . .  . .
    17206  0 .  . . .  . .
    17206  4 .  . . .  . .
    17206  5 .  . . .  . .
    17353  1 .  . . .  . .
    19252 -1 .  . . .  . .
    19252 -1 .  . . .  . .
    19252  4 .  . . 1  3 1
    19949 -1 .  . . .  . .
    19949 -1 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  0 .  . . .  . .
    19949  1 .  . . .  . .
    end
    label values delay10 delay
    label def delay -1 "pre_1-3yr", modify
    label def delay 0 "pre_0-1yr", modify
    label def delay 1 "0-1 years", modify
    label def delay 2 "1-2 years", modify
    label def delay 3 "2-3 years", modify
    label def delay 4 "3-4 years", modify
    label def delay 5 "4-5 years", modify
    label def delay 6 "5-6 years", modify

  • #2
    Code:
    local conditions subst selfharm organic schizo
    tempfile data
    save `data'
    foreach c of local conditions{
        use `data', clear
        keep personid delay10 tot_mh_`c'YR sum_losYR_mh_`c' seq_tot_mh_`c'YR
        save `c', replace
    }

    Comment


    • #3
      Thanks. This really helps

      Comment


      • #4
        Dear all

        I am working in a secure and protected environment and I have tried to "change directory: cd:" to work in my allocated folder, but my syntax is failing to run. I have therefore used the local folder as specified in the attachment below and I get an error message "type mismatch". What could be the reason for this error?

        ----------------------- copy starting from the next line -----------------------
        Code:
        * Example generated by -dataex-. To install: ssc install dataex
        clear
        input double ppk float(morb_orsubst morb_only tot_morb_only seq_morb_only tot_morb_orsubst seq_morb_orsubst)
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        2337 0 0  .  . . .
        3172 0 0  .  . . .
        3172 1 1 15  2 9 2
        3172 1 1 15  5 9 3
        3172 1 1 15  1 9 1
        3172 0 1 15  8 . .
        3172 1 1 15 12 9 6
        3172 0 1 15  4 . .
        3172 0 0  .  . . .
        3172 1 1 15 15 9 9
        3172 0 1 15  7 . .
        3172 1 1 15 13 9 7
        3172 0 1 15  6 . .
        3172 1 1 15 11 9 5
        3172 1 1 15 14 9 8
        3172 1 1 15 10 9 4
        3172 0 1 15  3 . .
        3172 0 1 15  9 . .
        3172 0 0  .  . . .
        3653 0 0  .  . . .
        3653 0 0  .  . . .
        3653 0 0  .  . . .
        3653 0 0  .  . . .
        end
        ------------------ copy up to and including the previous line ------------------

        Listed 30 out of 7990 observations

        Click image for larger version

Name:	syntax.PNG
Views:	1
Size:	88.9 KB
ID:	1664776

        Click image for larger version

Name:	error message.PNG
Views:	1
Size:	78.2 KB
ID:	1664777






        Comment


        • #5
          I found the solution. The syntax works with numeric variables so I converted "ppk" from string to numeric variable.

          Comment

          Working...
          X