Announcement

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

  • loops and scalars

    Hi guys,
    I'm very new with stata so.... please forgive me for some stupid questions...

    I need to calculate many scalars. I did it with many and many lines like these

    mean Q1 Q2 Q3 P1 P2 E if YEAR == 2005
    scalar Q1m05 = _b[Q1]
    scalar Q2m05 = _b[Q2]
    scalar Q3m05 = _b[Q3]
    scalar P1m05 = _b[P1]
    scalar P2m05 = _b[P2]
    scalar Em05 = _b[E]

    mean Q1 Q2 Q3 P1 P2 E if YEAR == 2006
    scalar Q1m06 = _b[Q1]
    scalar Q2m06 = _b[Q2]
    scalar Q3m06 = _b[Q3]
    scalar P1m06 = _b[P1]
    scalar P2m06 = _b[P2]
    scalar Em06 = _b[E]

    and so on for each year from 2005 to 2012

    I tried tihs solution but it does not work

    forvalues x=2005/2012 {
    mean Q1 Q2 Q3 P1 P2 E if YEAR == `x'
    scalar Q1m`x’ = _b[Q1]
    scalar Q2m`x’ = _b[Q2]
    scalar Q3m`x’ = _b[Q3]
    scalar P1m`x’ = _b[P1]
    scalar P2m`x’ = _b[P2]
    scalar Em`x’ = _b[E]
    }

    how can I solve?

    many tks

  • #2
    This is a general Stata question instead of a Mata question, so next time you better can ask these in the general Stata section.
    On first sight your code should work. The reason why it doesn't work is that the closing quote for the local that you are using is not the closing quote Stata is expecting ’ vs ' is very subtle though.

    Comment


    • #3
      sorry... next time I will go through general stata
      You're right. Now it works!
      tks a lot

      Comment

      Working...
      X