Announcement

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

  • Generating a variable to count # of "ideal" in each category per observation

    Dear all,

    I have three variables(idealFruitVeg ; idealfiber ; idealssbweek). Each variable can have either a value of 0 “Ideal” or 1 “Nonideal.”

    I would like to get a count of how many observations fall into the “ideal” category for all three variables, how many observations are in the “ideal” category for only two out of three variables, how many observations are in the “ideal” category for only one of the three variables, and how many observations are not in the “ideal” category for any of the variables.

    I pasted the syntax for how I accomplished this in Stata, but I am assuming there is a more efficient way to do this because the syntax would be rather long if I was trying to do this with a large number of variables. I am new to Stata and am trying to learn best practices.

    Thank you for your help and let me know if you have any clarifying questions. Also, please let me know if there is a better way to post questions in order to accurately convey what I want to accomplish and to allow others to help as easily as possible.



    Code:
    gen       RidealDiet = . 
    replace RidealDiet = 3 if idealFruitVeg ==0 & idealfiber ==0 & idealssbweek ==0
    replace RidealDiet = 2 if RidealDiet !=3 & idealFruitVeg==0 & idealfiber ==0   | RidealDiet !=3 & idealFruitVeg==0  & idealssbweek ==0 | RidealDiet !=3 & idealssbweek ==0  & idealfiber ==0
    replace RidealDiet = 1 if RidealDiet !=3 & RidealDiet !=2 & idealFruitVeg==0 | RidealDiet !=3 & RidealDiet !=2 & idealssbweek ==0  | RidealDiet !=3 & RidealDiet !=2  & idealfiber ==0
    replace RidealDiet = 0 if RidealDiet !=3 & RidealDiet !=2  & RidealDiet !=1 
    
    label define RidealDiet 3 "3 components" 2 "2 components" 1 "1 component" 0 "0 components"
    label value RidealDiet RidealDiet


    Code:
    * Example generated by -dataex-. To install: ssc install dataex
    clear
    input float(idealFruitVeg idealfiber idealssbweek)
    0 0 1
    1 1 1
    1 1 1
    0 0 0
    1 1 0
    1 1 0
    1 1 1
    1 1 0
    1 1 1
    1 1 1
    0 1 1
    0 1 1
    1 1 1
    1 1 1
    0 1 1
    1 1 1
    0 1 1
    0 1 0
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 1
    1 1 0
    1 1 1
    0 1 0
    0 1 1
    0 0 1
    1 1 1
    end
    label values idealFruitVeg idealFruitVeg
    label def idealFruitVeg 0 "ideal", modify
    label def idealFruitVeg 1 "nonideal", modify
    label values idealfiber idealfiber
    label def idealfiber 0 "ideal", modify
    label def idealfiber 1 "nonideal", modify
    label values idealssbweek idealssbweek
    label def idealssbweek 0 "ideal", modify
    label def idealssbweek 1 "nonideal", modify

  • #2

    Code:
    gen wanted = idealFruitVeg + idealfiber + idealssbweek
    is how I would start on this. That's not your definition, but it matches the idea that 1 is good and 0 bad, or not so good.

    Comment


    • #3
      Hi Nick,

      Thank you so much. Of course, makes much more sense to add the rowtotal and then define and label. I used the code below based on your advice and it worked perfectly. Much better way than the long convoluted syntax I wrote originally.


      Code:
      egen RidealDietV2 = rowtotal(idealFruitVeg idealfiber idealssbweek)
      label define RidealDietV2 0 "3 components" 1 "2 components" 2 "1 component" 3 "0 components"
      label value RidealDietV2 RidealDietV2

      Comment


      • #4
        If what you're interested in is just the total number of components that get the ideal score you're making this way too complicated.

        Nick's code should put you on the right track, but the way things are currently coded gets you the number of non-ideal components. In case it's not obvious here are two ways you could get to the count of ideal components.

        1) reverse the coding on your variables so that 1 is ideal and 0 is non-ideal, which is a bit more logical to work with. To do this just replace the variable with 1 minus the current value. If you have a lot of variables you want to do this with, a foreach loop is going to be your best bet.


        2) Use subtraction to get the number of ideal components.

        Code:
        gen nonideal=idealFruitVeg + idealfiber + idealssbweek
        gen ideal = 3 - nonideal

        Comment


        • #5
          Hi Sarah,

          Thank you for showing me how to reverse code my variables. The way I did it before your post effected my mean. I updated my syntax after your advice.

          Code:
          egen nonideal = rowtotal(idealFruitVeg idealfiber idealssbweek)
          gen ideal = 3 - nonideal
          label define ideal 3 "3 components" 2 "2 components" 1 "1 component" 0 "0 components"
          label value ideal ideal

          Comment


          • #6
            I didn't look carefully enough at your definitions. It's a really good idea to have the name of an indicator match whatever is coded 1. More at
            https://journals.sagepub.com/doi/abs...urnalCode=stja

            Comment


            • #7
              Thank you for the link to the journal article. I requested access from my university library and will read it as soon as I get access.

              Comment

              Working...
              X