Announcement

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

  • Scalar multiplication problem

    Hi, Stata community!

    I am trying to do a simple multiplication between scalars in two loops and having a problem with it. The new scalar - which is a multiplication of two previous scalars - is empty.
    The loops run over people ("k") and countries ("j"). Let see:

    Code:
      foreach k in `people' {    
        foreach j in `co_list' {
          scalar v_`k'_co_`j' =  scalar(v_all_`k'_co_`j') * scalar(share_`k'_co_`j')
        }
      }
    Note: the two scalars: v_all_`k'_co_`j' and share_`k'_co_`j' are checked to contain values.

    Any suggestion is welcome.

    Thanks.











  • #2
    An example I made using your code, listed below, does produce new scalars with values. My guess, therefore, would be that you are wrong about your new scalars being "empty." Have you tried printing the value of each new scalar in your loop, as I do below? If that produces nothing, then one of the component scalars must be missing. Also, I'm not sure what you mean by "empty." A scalar in Stata can't be empty. It can hold a missing value, but if it exists, it holds something.

    Code:
    // Make locals for test
    scalar drop _all
    set seed 9876
    local people X Y Z
    local co_list Apple MSoft GE
    foreach k of local people {
       foreach j of local co_list  {
          scalar v_all_`k'_co_`j' = ceil(runiform() * 5)
          scalar share_`k'_co_`j' = runiform()
       }
    }  
    scalar list _all // verify that components above exist
    //
    // Try the code and echo each scalar.
    foreach k in `people' {    
        foreach j in `co_list' {
          scalar v_`k'_co_`j' =  scalar(v_all_`k'_co_`j') * scalar(share_`k'_co_`j')
          display "v_`k'_co_`j' = " v_`k'_co_`j'
        }
      }

    Comment


    • #3
      Dear Mike, thank you for taking the time to check this problem.
      Finally, I figured out that one of the scalars in the loops, despite looking like numbers was actually stored as a string after a levelsof. The issue has been resolved.

      Comment

      Working...
      X