Announcement

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

  • Need help

    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, countrynumber
    sort d10_`var'
    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
    4. sort d10_`var'
    5. scalar p50d`var' = `r(p50)'
    6. scalar p75d`var' = `r(p75)'
    7. }
    not sorted
    r(5);

    end of do-file

    r(5);

    my variables are ranked from 1950 to 2015 with missing data. Even when I 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
    Juny:
    thanks for re-posting under a different title.
    However, your title is far from being of any help from interested listers as the general crying for help without further, substantive details, usually increase the chances that your post is skipped and left unreplied.
    That said, my feeling is that the chunck of code after the comma
    Code:
        su d10_`var' if year==2015, countrynumber
    is illegal.

    As an aside, you can type -set trace on- and see where the problem is lurking.
    Last edited by Carlo Lazzaro; 14 Nov 2019, 00:07.
    Kind regards,
    Carlo
    (Stata 18.0 SE)

    Comment


    • #3
      Code:
      foreach var in eyrs_mf nostu_mf asr_mf {
          gen d10_`var' = `var' - L10.`var'
          su d10_`var' if year==2015, countrynumber
          sort d10_`var'
          scalar p50d`var' = `r(p50)'
          scalar p75d`var' = `r(p75)'
      }
      I see four bugs there, actual or possible.

      1. Using L.10 would depend on a previous tsset or xtset command being successful and the sort order being consistent with that.

      2. A subsequent sort on anything else would undo that and seems pointless in any case.

      3. countrynumber is not an option of summarize. I have no idea what you intend there.

      4. Creating scalars based on median and upper quartile will not be useful unless you previously specified summarize, detail.

      Note that

      Code:
      scalar p50d`var' = r(p50)
      would keep more information, although the difference is usually immaterial.

      PS: Carlo Lazzaro is right as usual. In the FAQ Advice we do say

      7. What is expected of the topic title?

      Few members read every post, so your topic needs to help members decide if they might be able to help you.

      Make the topic line concise but informative. “Question” or “Please help” will not help us or help you. “Problem with instrumental variables regression” lets people decide quickly whether to look at your post.
      Last edited by Nick Cox; 14 Nov 2019, 01:49.

      Comment


      • #4
        I would like to thank you very sincerely for the suggestion about the title. This is my first time posting on the forum and your suggestion is very useful for my future posts.

        Comment


        • #5
          I really want to thanks to Nick Cox and Carlo Lazzaro for your help. I was able to advance on my works. Thank you very much

          Comment

          Working...
          X