Announcement

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

  • Problem with foreach

    Hi dear member,

    I am a beginner in the use of foreach. I need help to execute this code:

    foreach var in eyrs_mf nostu_mf asr_mf {
    gen d10_`var' = `var' - L10.`var'
    su d10_`var' if year==2015, detail
    scalar p50d`var' = r(p50)
    scalar p75d`var' = r(p75)
    }

    When I execute the code above, I get the following error message:

    . foreach var in eyrs_mf nostu_mf asr_mf {
    2. gen d10_`var' = `var' - L10.`var'
    3. su d10_`var' if year==2015, countrynumber
    5. scalar p50d`var' = r(p50)
    6. scalar p75d`var' = r(p75)
    7. }
    not sorted
    r(5);
    end of do-file
    r(5);

    I tell myself that the problem is located in this code «gen d10_`var' = `var' - L10.`var». My variables are ranked from 1950 to 2015 with missing data. I used a panel data. Even when I used xtset and try to sort using "bysort countrynumber eyrs_mf nostu_mf asr_mf: list wbcode" I always get the same error message. Thanks for your help

  • #2
    Detailed comments on almost the same question were posted in your previous thread https://www.statalist.org/forums/for...4576-need-help even before you posted this.

    You did right to post with a more informative title than previously, but you shouldn't ignore previous answers.

    To focus on what is new here: Something like

    Code:
    xtset id year 
    is the way to declare panel data -- and it goes hand in hand with the sort order given by

    Code:
    sort id year
    and that is incompatible with any other sort order. In your case id may well be countrynumber

    I can't see that

    Code:
    bysort countrynumber eyrs_mf nostu_mf asr_mf: list wbcode
    will ever be needed or a good idea.

    Code:
    sort countrynumber year 
    list countrynumber year eyrs_mf nostu_mf asr_mf: list wbcode  , sepby(countrynumber year)
    should work fine.

    If this doesn't answer your questions I think you'll need to read the FAQ Advice, especially https://www.statalist.org/forums/help#stata and

    1. Give us a proper data example

    2. Show the exact code you used to xtset

    Comment

    Working...
    X