Announcement

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

  • Foreach for numeric values

    Hi, all

    I was trying to work the foreach statement, but I realized that the foreach statement does not work for numeric values. The following code is part of my codes, and I would like to receive any feedback in order for k to work. Please see the red colored place.


    quietly forvalues i=1/1{
    foreach n in v {
    foreach k in 10 50 100 250 {
    gen pro_`n'_`k'_`i' = 1 - gammap(kappa_gglog_`n'`i'^-2, (kappa_gglog_`n'`i'^-2)*(ln(lnb_gglog`i'*(100/(100-`k')))-lnb_gglog`i')))
    }
    }
    }


    Thank you!

    Sincerely,
    Sungchul

  • #2
    I'm not sure what your problem is, but it lies elsewhere—foreach does work for numerical values.

    .ÿ
    .ÿversionÿ14.1

    .ÿ
    .ÿclearÿ*

    .ÿsetÿmoreÿoff

    .ÿ
    .ÿforeachÿkÿinÿ10ÿ50ÿ100ÿ250ÿ{
    ÿÿ2.ÿÿÿÿÿdisplayÿinÿsmclÿasÿtextÿ(100/(100-`k'))
    ÿÿ3.ÿ}
    1.1111111
    2
    .
    -.66666667

    .ÿ
    .ÿexit

    endÿofÿdo-file


    .ÿ

    Comment


    • #3
      I realized that the foreach statement does not work for numeric values
      That is completely wrong. The -foreach- statement handles numerical arguments perfectly well. There are other problems in your code.

      First, there is no need for a -forvalues i = 1/1- loop as it just has one iteration. Similar -foreach n in v- will iterate only once, with `n' = "v". So your three nested loops actually just reduce to a single loop:

      Code:
      foreach k in 10 50 100 250 {
          gen pro_v_`k'_1 = 1 - gammap(kappa_gglog_v1^2, (kappa_gglog_v1^-2)*(ln(lnb_gglog1*(100/(100-`k')))-lnbgglog1)))
      }
      However, this code will not run because the parentheses in that command as a whole do not balance. You are either missing a left parenthesis (there are 6) or have an extra right parenthesis (7). Since I have no idea what this expression is supposed to be, I cannot advise you how to fix that problem.

      As for the expression 100/(100-`k') that you highlighted in red, when `k' = 100, the denominator evaluates to zero, so the expression will return a missing value. And when k = 250, 100/(100-`k') will be negative, which is going to also give your expression overall a missing value as you try to take the log of a negative number, unless lnb_gglog1 is itself negative. But in that case, you will get missing values when k = 10 or 50: either way, you cannot take the logarithm of a negative number.

      In general, when we want to iterate over a list of numbers, we usually use -foreach k of numlist 10 50 100 250-, but -foreach k in 10 50 100 250- will work too. The advantages of using -foreach k of numlist...- is that Stata will check that you have actually specified a valid list of numbers, and it also would run faster (but this loop is too short for the difference in speed to be noticeable.)
      Last edited by Clyde Schechter; 17 Aug 2016, 22:33. Reason: Correct typo.

      Comment


      • #4
        Dear Drs. Coveney and Schechter

        Thank you much very for your comments. The code is not complete because I uploaded part of my code. I tried to do again after simplifying my code and it does work!

        Thank you!

        Sincerely,
        Sungchul

        Comment

        Working...
        X