Announcement

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

  • Splitting a data set into four data sets over all observations but keepin all variables for each splitted data set

    Hi All,
    I am using a data set which contains more than 50 variables and 0.36 observations. I need to split all the observations into four different data sets based on one of the variable named "province". The variable province has one digit for each of the province e.g. 1 is used for Punjab and 2 is used for sindh.
    How i can split this data set into four data sets that will be data sets for each of the different provinces. Thanking in anticipation

  • #2
    Welcome to Statalist.

    I will assume that the values of the variable province are 1 2 3 and 4.
    Code:
    forvalues p = 1/4 {
    preserve
    keep if province == `p'
    save province`p', replace
    restore
    }

    Comment


    • #3
      Thank you very much for your reply and the code,
      Yes you are right in assuming that variable province gets the values 1,2,3 and 4.
      I am new user of stat and I want to save each province observations in a different file, is it possible? or even if it is not possible then how can I use the splitted data for each of the province differently.
      e.g If i want to draw histogram for one of the variables in province 1 only separtaley and a sperate histogram for one of the variable in province 2.

      Comment


      • #4
        The code I wrote in post #2 will create 4 new datasets named province1 province2 province3 province4 each with the observations for just that province.

        However, you may not need to do that. With your dataset containing all four provinces in memory,
        Code:
        histogram X if province==1
        will create a histogram of the variable X using only those observations in province 1.

        But I want to give you some advice about learning to use Stata.

        I'm sympathetic to you as a new user of Stata - it's a lot to absorb. And even worse if perhaps you are under pressure to produce some output quickly. Nevertheless, I'd like to encourage you to take a step back from your immediate tasks.

        When I began using Stata in a serious way, I started, as have others here, by reading my way through the Getting Started with Stata manual relevant to my setup. Chapter 18 then gives suggested further reading, much of which is in the Stata User's Guide, and I worked my way through much of that reading as well. There are a lot of examples to copy and paste into Stata's do-file editor to run yourself, and better yet, to experiment with changing the options to see how the results change.

        All of these manuals are included as PDFs in the Stata installation (since version 11) and are accessible from within Stata - for example, through the PDF Documentation section of Stata's Help menu. The objective in doing the reading was not so much to master Stata as to be sure I'd become familiar with a wide variety of important basic techniques, so that when the time came that I needed them, I might recall their existence, if not the full syntax, and know how to find out more about them in the help files and PDF manuals.

        Stata supplies exceptionally good documentation that amply repays the time spent studying it - there's just a lot of it. The path I followed surfaces the things you need to know to get started in a hurry and to work effectively.

        Comment


        • #5
          William Lisowski Thank you very much for your help and advice. Yes I will follow the way you suggested to learn more about stata. I really wanted to do that before posting here but since the time I had to give some quick results was short . But I really appreciate your help and sincere suggestion.

          Comment

          Working...
          X