Announcement

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

  • Splitting a dataset into multiple datasets

    Hey all,

    Newish to STATA - longtime SAS user - sorry for the basic question. I have a dataset of cases and controls. Trying to split them into two datasets based on the value of the variable casecontrol. It would be something like this in SAS:

    data cases controls; set have;
    if casecontrol=1 then output cases;
    if casecontrol=2 then output controls;
    run;

    I guess I am confused since we dont have to specify datasets in STATA - so it seems like if I keep cases - then I lose the controls?

    Am I missing something?

    Thanks!

    nick

  • #2
    Welcome to Statalist!

    Preserve and restore can probably do that:

    Code:
    use case_control, clear
    
    preserve
    keep if casecontrol == 1
    save cases, replace
    restore
    
    preserve
    keep if casecontrol == 2
    save controls, replace
    restore
    Also, use works with if as well:

    Code:
    use case_control if casecontrol == 1, clear
    save cases, replace
    
    use case_control if casecontrol == 2, clear
    save control, replace
    Frame may also be an alternative. To learn more, use command help frames_intro. And for even some more ways (including external package savesome), see this link: https://www.stata.com/support/faqs/d...ts-of-dataset/.
    Last edited by Ken Chui; 23 Jun 2022, 16:13.

    Comment


    • #3
      Thanks Ken!

      Sorry for all the newbie questions - but I am importing my dataset from an excel file. So one of the issues I am having is I don't know what to name my dataset - is there a way to name it? Would I use that save command first?

      Thanks

      Comment


      • #4
        Code:
        h save
        and, within the rules of your OS, choose your own file name

        Comment

        Working...
        X