Announcement

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

  • Defining varlists for temporary variables

    I'm stumped on how to create a varlist vt that contains the temporary variables
    Code:
    `_ty1' `_ty2' `_ty3'
    that are defined in the following code.

    The foreach loop I use at the bottom accomplishes this but I'm assuming there must be a one-line local definition using a * wildcard. However I can't seem to get the single/double quote nesting correct to accomplish this. (The real application has lots of temporary variables, thus the desire to use a wildcard.)

    Can anyone advise? I'm happy to use the foreach loop but this seems unnecessarily clunky. Also in the real example the variables aren't indexed by a simple `j' thus the desire to use y* as the varlist in my current foreach loop.

    Thanks in advance.

    Code:
    cap preserve
    cap drop _all
    
    set obs 100
    
    forval j=1/3 {
     tempvar _ty`j'
     gen y`j'=10*uniform()
     gen `_ty`j''=floor(y`j')
    }
    
    local vt=" "
    foreach y of varlist y* {
     local vt="`vt'"+"`_t`y'' "
    }
    sum `vt'
    
    cap restore

  • #2
    One possibility would be to collect the temporary variables already in the first loop:
    Code:
    cap preserve
    cap drop _all
    
    set obs 100
    
    forval j=1/3 {
     tempvar _ty`j'
     gen y`j'=10*uniform()
     gen `_ty`j''=floor(y`j')
     local vt = "`vt'"+"`_ty`j'' "
    }
    
    sum `vt'
    cap restore

    Comment


    • #3
      A more general example that doesn't require creating the list of temporary variables at the same time the temporary variables are created, since you suggest the code in post #1 is somewhat simplified.
      Code:
      set obs 100
      
      foreach j in cat dog mouse {
       tempvar fy`j'
       gen y`j'=10*uniform()
       gen `fy`j''=floor(y`j')
       local fy`j' `fy`j''
      }
      ds *
      macro list _fycat _fydog _fymouse
      
      foreach y of varlist y* {
       local vt `vt' `f`y''
      }
      macro list _vt
      sum `vt'
      Code:
      . set obs 100
      number of observations (_N) was 0, now 100
      
      . 
      . foreach j in cat dog mouse {
        2.  tempvar fy`j'
        3.  gen y`j'=10*uniform()
        4.  gen `fy`j''=floor(y`j')
        5.  local fy`j' `fy`j''
        6. }
      
      . ds *
      ycat      __000000  ydog      __000001  ymouse    __000002
      
      . macro list _fycat _fydog _fymouse
      _fycat:         __000000
      _fydog:         __000001
      _fymouse:       __000002
      
      . 
      . foreach y of varlist y* {
        2.  local vt `vt' `f`y''
        3. }
      
      . macro list _vt
      _vt:            __000000 __000001 __000002
      
      . sum `vt'
      
          Variable |        Obs        Mean    Std. Dev.       Min        Max
      -------------+---------------------------------------------------------
          __000000 |        100         4.7    2.966139          0          9
          __000001 |        100        4.74    2.939181          0          9
          __000002 |        100        4.73    2.759209          0          9
      
      .

      Comment


      • #4
        Thanks Dirk and William.

        Comment


        • #5
          Something like this also works, surprisingly or not

          Code:
           
          clear
          set obs 100 
          set seed 2803 
          
          forval j = 1/3 { 
              tempvar new 
              gen `new' = runiform()
              local NEW `NEW' `new'
          }
          
          su `NEW'
          To the objection that you have lost sight of distinct variable names:

          1. In code like this you often don't need them.

          2. Use tokenize to get handles.

          Comment

          Working...
          X