Announcement

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

  • Creating an IF or Conditional command

    Hi All,

    It is my first time in this forum and I'm hoping you will be able to assist me.

    My study is on Adolescence Obesity and I have huge data that consists of everyone including adults. I would like to generate a variable called Mother_IBM. So the condition is that the output should be female (numlabel 2) and have No_of_kids>0

    Thanks



  • #2

    Code:
    gen Mother_IBM = female == 2 & No_of_kids > 0
    For more informaiton see

    https://www.stata.com/support/faqs/d...les/index.html

    https://www.stata.com/support/faqs/d...rue-and-false/

    https://www.stata-journal.com/articl...article=dm0099

    Comment


    • #3
      Thanks for the prompt response Nick.

      I was also trying to have a separate column for Adolescence age, separate from the main Age variable that includes all. So I used generate and replace. The results show 1 for adolescence and 0 for others. I would like to have the ages of the adolescents instead of 1 in that column. Is this possible?

      Comment


      • #4
        Sorry, but I have no idea what #3 means precisely. Please back up and read and act on https://www.statalist.org/forums/help#stata and give a data example and show the exact code you used.

        #3 is not far from "I used some code I am not showing you and got a variable with values 0 and 1 but I really want something else." It wouldn't be helpful to say "So, don't do that then", To get the answer you want, please follow our advice intended to help you.

        Comment


        • #5
          You should actually read the material Nick suggested in #2 -- simply copying the code may include cases you don't want to include (e.g. mothers for who the number of kids is not known). Of course Nick is well aware of the issue that in Stata missing values are the largest numbers of a given type, but it may be that you are not. Thus, a fail safe code would be (parentheses for clarification):
          Code:
           gen Mother_IBM = female == 2 & (No_of_kids > 0 & No_of_kids < .)
          Last edited by Dirk Enzmann; 31 May 2021, 12:54.

          Comment


          • #6
            Thanks gentlemen for your assistance. It worked.
            @Nick, I will provide more detail next time

            Comment

            Working...
            X