Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Tab, Matrix, & Capturing Frequencies

From   "Eric G. Wruck" <>
Subject   Re: st: Tab, Matrix, & Capturing Frequencies
Date   Fri, 8 Nov 2002 23:03:36 -0500

Steve Fraser wrote:

>Assume the following variation on the auto data:
>Manuf	Brand	Model		MPG	Cost	....
>Ford	Merc	Mountaineer
>Ford	Ford	Explorer
>Ford	Ford	Escape
>GM	GM	Yukon
>GM	Chev	Tahoe
>Chry	Dodge	Durango
>Chry	Dodge	Durango
>Ford	Ford	Escape
>Ford	Ford	Escape	
>GM	Chev	Blazer
>I am trying to capture the number of models offered by the 'brand' Ford as
>opposed to the 'manufacturer' Ford.  If I sort by brand (assuming only one
>manufacturer for now..I will complicate with more manufacturers) and then
>tab brand and model, I get results depicting the frequency of each model
>(i.e. Explorer 1, Escape 3 for 'brand' Ford and Mountaineer 1 for 'brand'
>Merc).  What I need is to capture the value '2' indicating 2 models for the
>'brand' Ford and '1' indicating 1 model for the 'brand' Merc.  The only way
>I have been able to capture this is when I add the 'matcell' command (help
>from a FAQ) and then capturing the number of rows.  However, even while I am
>currently only using one 'manufacturer', I have several 'brands' and need
>the number of models offered by each 'brand', but matrix will only return
>the matrix for the last 'brand' and matrix cannot be used with by.  The

>>> Here is a brute force way that seems to work (if I understand your problem correctly):

. sort brand model

. by brand model: keep if _n == 1
(3 observations deleted)

. egen no_models = count(1), by(brand)

. l

         manuf      brand        model  no_models
  1.        GM       Chev       Blazer          2
  2.        GM       Chev        Tahoe          2
  3.      Chry      Dodge      Durango          1
  4.      Ford       Ford       Escape          2
  5.      Ford       Ford     Explorer          2
  6.        GM         GM        Yukon          1
  7.      Ford       Merc  Mountaineer          1

You may want to keep only the first record of each brand-model pair as each has the number of models for that brand.  You may also want to drop both manuf & model as well since they are kind of artifacts from the exercise.




       Eric G. Wruck
       2535 Sherwood Road
       Columbus, OH  43209

       ph:      614.231.5034
       fax:     614.231.5039
       eFax:    419.730.7809

*   For searches and help try:

© Copyright 1996–2017 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index