Announcement

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

  • Calsculate the Household production diversity

    I have the following data hhid group
    11001 Other fruits
    11001 legumes nuts and seeds
    11001 legumes nuts and seeds
    11001 legumes nuts and seeds
    11001 Oils and fats
    11002 white roots and tubers
    11002 starchy plants
    11002 legumes nuts and seeds
    11002 starchy plants
    11002 starchy plants
    11002 legumes nuts and seeds
    11002 starchy plants
    11011 starchy plants
    11011 legumes nuts and seeds
    11011 starchy plants
    11016 Other fruits
    11016 starchy plants
    11016 legumes nuts and seeds
    11016 starchy plants
    11016 legumes nuts and seeds
    11018 white roots and tubers
    11018 starchy plants
    11018 legumes nuts and seeds

    I want to gen another variable production showing the different crops grown per household from the variable group which is the group of food for example the house household number 11001 should be 3. Then collapse by hhid

    Thanks

  • #2
    Helo Statalists,
    Using the dataex command the data is as follows
    [CODE]
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float id long(hhid group)
    1 11001 3
    1 11001 7
    1 11001 7
    1 11001 7
    1 11001 2
    2 11002 9
    2 11002 8
    2 11002 7
    2 11002 8
    3 11002 8
    3 11002 7
    3 11002 8
    4 11011 8
    4 11011 7
    4 11011 8
    6 11016 3
    6 11016 8
    6 11016 7
    7 11016 8
    7 11016 7
    8 11018 9
    8 11018 8
    8 11018 7
    8 11018 2
    9 11019 7
    9 11019 9

    I wish to count the number of categorised food groups per household and create the variable Household_Production_Score,then collapse by household.
    Hope this is clear.
    Thanks
    Mwanga Ronald

    Comment


    • #3
      Welcome to Statalist! Here is some code that I think does what you ask. You can ignore the top part before the comment, that is just to re-create your data.

      Code:
      clear
      input int hhid str30 group
      11001 "Other fruits"
      11001 "legumes nuts and seeds"
      11001 "legumes nuts and seeds"
      11001 "legumes nuts and seeds"
      11001 "Oils and fats"
      11002 "white roots and tubers"
      11002 "starchy plants"
      11002 "legumes nuts and seeds"
      11002 "starchy plants"
      11002 "starchy plants"
      11002 "legumes nuts and seeds"
      11002 "starchy plants"
      11011 "starchy plants"
      11011 "legumes nuts and seeds"
      11011 "starchy plants"
      11016 "Other fruits"
      11016 "starchy plants"
      11016 "legumes nuts and seeds"
      11016 "starchy plants"
      11016 "legumes nuts and seeds"
      11018 "white roots and tubers"
      11018 "starchy plants"
      11018 "legumes nuts and seeds"
      end
      
      * start from here
      
      egen byte tag = tag(hhid group)
      collapse (sum) production = tag, by(hhid)
      which produces:

      Code:
        +------------------+
        |  hhid   produc~n |
        |------------------|
        | 11001          3 |
        | 11002          3 |
        | 11011          2 |
        | 11016          3 |
        | 11018          3 |
        +------------------+
      Thank you for providing a sample of your data. But in the future, could you please do this using the dataex command, as advised in the Statalist FAQ? Thank you!

      Edit: crossed with #2. Thanks for using dataex in your very first thread on Statalist!
      Last edited by Hemanshu Kumar; 03 Nov 2022, 08:15.

      Comment


      • #4
        Code:
        egen tag= tag(hhid group)
        bys hhid: egen wanted= total(tag)
        The tag() function of egen will tag exactly one combination of household and group and the total() function will take the sum. See

        Code:
        h egen

        Comment


        • #5


          See also the article on distinct observations in the Stata Journal.

          https://journals.sagepub.com/doi/pdf...867X0800800408

          Andrew Musau's method is discussed on p.563.

          Comment


          • #6
            Thanks alot Hemanshu Kumar your code worked and Nick Cox thanks for the reference.

            Comment

            Working...
            X