Announcement

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

  • stata 17 sum across a row but replacing any value greater than 0 with 1

    Hi, I have a dataset that looks a little like this:

    household id stove fridge wardrobe water pump
    1 2 0 4 0
    2 0 1 3 2
    3 4 0 1 1

    I would like to generate a variable summing how many different types of asset each household owns but I am unsure as to how to replace values greater than 1 with just the value 1 to signify whether they own that asset (as I don't need the number of each asset that each has). I have tried the 'egen assets = rowtotal()' but I am not sure how to add the replace command to this.

    would be super appreciative for any help! thanks in advance!

  • #2
    Code:
    gen assets = (stove>0) + (fridge>0) + (wardrobe>0) + (water>0) + (pump>0)

    Comment


    • #3
      Chloe:
      you may want to try:
      Code:
      . foreach x of varlist stove-water_pump {
        2. gen count_`x'=1 if `x'>0 & `x'<.
        3.  }
      
      
      . foreach x of varlist count_stove-count_water_pump {
        2. replace `x'=0 if `x'==.
        3.  }
      
      
      . list
      
           +---------------------------------------------------------------------------------------------+
           | househ~d   stove   fridge   wardrobe   water_~p   count~ve   count~ge   count~be   count_~p |
           |---------------------------------------------------------------------------------------------|
        1. |        1       2        0          4          0          1          0          1          0 |
        2. |        2       0        1          3          2          0          1          1          1 |
        3. |        3       4        0          1          1          1          0          1          1 |
           +---------------------------------------------------------------------------------------------+
      
      .
      PS: crossed in the cyberspace with Scott's helpful reply.
      Last edited by Carlo Lazzaro; 14 Feb 2024, 10:44.
      Kind regards,
      Carlo
      (StataNow 18.5)

      Comment


      • #4
        Thank you so much Carlo and Scott - the code works perfectly now!

        Comment

        Working...
        X