Announcement

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

  • Tabulation issues with "if" statements

    Hi folks,

    I am unsure of where to find this, and I have looked. Potentially I am trying to treat Stata too much like Matlab or Mathematica.

    All I want to do is tabulate variables using many if statements.

    i.e

    Tab variableX if (weight>90kg in 2000) & (weight<90kg in 2005).

    How would I do this?

    Thanks in advance

  • #2
    You must tell more about the structure of your data. For example, do you have one observation per person (wide format), or do you have one observation per person and year (long format)?

    Other points: tab (not Tab) is an abbreviation of tabulate. If weight contains the weight in kg, it is ...weight>90..., not ...weight>90kg...

    Comment


    • #3
      This depends a lot on how your data is organized. E.g., is weight in 2000 one variable, and weight in 2005 another, or do you have panel data with person ID, weight and moment of measurement?

      Comment


      • #4
        Hi guys,

        I was being silly. I reshaped my data into wide format and now I can do everything I need.
        Rookie error on my part.

        Thanks for the replies.

        Comment


        • #5
          It can be done with a long structure too. Presumably you have variables something like identifier, weight, year.

          Code:
           
          egen wanted = total((weight > 90 & year == 2005) | (weight < 90 & year == 2010)), by(id) 
          tab <something> if wanted == 2
          The conditions

          Code:
           
          weight > 90 & year == 2005 
          
          weight < 90 & year == 2010
          will be evaluated as 1 when true and 0 when false. You want persons for which both are true, so they will score a total of 2.

          A key detail is that the conditions will not be (cannot be) true in the same observation with a long structure, so you need logical or, not logical and, even though you are looking for persons for whom both are true.

          For more, see http://www.stata-journal.com/sjpdf.h...iclenum=dm0055


          Comment

          Working...
          X