Announcement

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

  • Putting output from --tab-- or --list-- in a macro

    Hello,

    I have a dataset with a string variable called V1 that is a description of the observation. For select observations, one at a time, I'd like to put the value of V1 for that observation into a local. Something like:
    Code:
    foreach n of 1/5 {
      list V1 if _n == `n'
      local temp = r(output)
    }
    I know this seems like a tremendously silly thing to do, but I swear for my application it is ideal.

    If V1 were numeric, I could easily do this as:
    Code:
    foreach n of 1/5 {
      sum V1 if _n == `n'
      local temp = r(mean)
    }
    V1 is a string. Is there anyway to programmatically reference the output of --list-- or --tab-- to put its output into a local like this?

    Thank you,
    Mitch
    Mitch Downey, Grad student, UCSD Economics

  • #2
    valuesof does this nicely. The code would be:
    Code:
    foreach n of 1/5 {  
      valuesof V1 if _n == `n'
     local temp = r(values)
    }
    Mitch Downey, Grad student, UCSD Economics

    Comment


    • #3
      That's like going from San Diego to Los Angeles via San Francisco. You're pushing a single value at another command (valuesof is user-written, as you are asked to explain) only to pull out precisely what you put in first of all. How about

      Code:
      forval n = 1/5 {    
           local temp = V1[`n']      
          <something else using the local, presumably>  
      }
      In fact, not even the local is obviously necessary here. Why not use successive values directly?

      Whether this is a good idea is context is not something you have to swear to, but we might have better ideas for your context if you told us what it is.

      Conversely, if you've simplified your real example, we might be missing what you simplified out of the way.

      Note for the innocent:

      Code:
       
       foreach n of 1/5 {
      wouldn't work as the keyword numlist is omitted.
      Last edited by Nick Cox; 09 Oct 2017, 07:46.

      Comment


      • #4
        Thanks Nick. Much more elegant. I never use brackets in that way, so it didn't occur to me.

        Best,
        Mitch
        Mitch Downey, Grad student, UCSD Economics

        Comment

        Working...
        X