Unfortunately, despite re-installing I am still having difficulties. Summing the dummy variables within each family.Dear Kyle - many thanks for your prompt response. collapse (mean) avgage=age avgwt=wt (count) numkids=birth, by(famid)Ĭounts the number of boys and girls in each family by using tabulate to create dummy variables based on sex and then Same as above example, but also counts the number of kids within each family calling that To create one record per family ( famid) with the average of age (called avgage) and average weight (called avgwt) within each family.Ĭollapse (mean) avgage=age avgwt=wt, by(famid) To create one record per family ( famid) with the average of age within each family. list famid boys girls numkids famid boys girls numkids We can list out the data to confirm that it worked correctly. collapse (count) numkids=birth (sum) girls=sexdum1 boys=sexdum2, by(famid) The command below creates girls which is the number of girls in the family, andīoys which is the number of boys in the family. List famid sex sexdum1 sexdum2 famid sex sexdum1 sexdum2 The sum of sexdum2 is the number of boys in the family. Sexdum1 is the number of girls in the family. Tabulate sex, generate(sexdum) sex | Freq. We use tabulate with the generate option to make the dummy variables. The sum of theīoy dummy variable is the number of boys and the sum of the girl dummy variable is the number of girls.įirst, let’s use the kids file (and clear out the existing data). We will create a dummy variable that is 1 if the kid is a boy (0 if not), and a dummy variable that is 1 if the kid is a girl (and 0 if not). Suppose you wanted a count of the number of boysĪnd girls in the family. use, clear collapse (mean) avgage=age avgwt=wt (count) numkids=birth, by(famid) list famid avgage avgwt numkids use, clear collapse (mean) avgage=age avgwt=wt, by(famid) list famid avgage avgwtĪnd wt like the command above, and also computes numkids which is the count of the number of kids in each family (obtained by counting the number of observations with valid values ofīirth). Here we get the average forĪge and for wt all in the same command. We can request averages for more than one variable. use, clear collapse (mean) avgage=age, by(famid) list famid avgage The following collapse command does the exact same thing as above, except that the average ofĪge is named avgage and we have explicitly told the collapse command that we want it to compute the Use, clear collapse age, by(famid) list famid age The above collapse command was not very useful, but you can combine it with theĪnd then it creates one record for each family that contains the average age of the kids in the family. It collapses across all of the observations to make a single record with the average age of the kids. Use, clear list famid kidname birth age wt sexĬonsider the collapse command below. We will use this file for showing how to collapse data across observations. (i.e., 1 is first), age wt and sex are the child’s age, weight and sex. Here is a file containing information about the kids in We will illustrate this using an example showing how you can collapse data across kids to make family level data. For example, you might have student data but you really want classroom data, or you might have weekly data but you want monthly data,Įtc. Sometimes you have data files that need to be collapsed to be useful to you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |