Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Referring to elements of a varlist in a program


From   Dani Tilley <[email protected]>
To   [email protected]
Subject   Re: st: Referring to elements of a varlist in a program
Date   Fri, 9 Jul 2010 09:50:16 -0700 (PDT)

Thanks so much to both of you.



----- Original Message ----

Other ways to do


token `varlist'    //added//
qui su `2'  /* changed */

are

local y : word 2 of `varlist'
qui su `y'

and

qui su `: word 2 of `varlist''

Nick
[email protected]

Eric Booth

<>
Hi Dani:

You were close, just a few minor fixes.  The trick in choosing the first  or 
second variable in the `varlist' is  to use -tokenize- :

**********!
cap program drop example   //added//

program example, rclass
version 10.0
syntax varlist(min=2 max=2 numeric)  /* changed */

loc m=0
foreach v of local varlist {   /* changed */

qui su `v',mean
loc m=`m'+`r(mean)'
}

//second variable in varlist only //
token `varlist'    //added//
qui su `2'  /* changed */

local sd = `r(sd)'
local final = `m' / `sd'
**drop m sd      //these aren't vars--you don't need to drop  them//
return local final = `final'
di as txt  "Final result is: `final'"  //added//
end


webuse auto, clear
example price mpg

**these should fail**
example price //too few vars
example rep78 price mpg for  //too many vars
example rep78 make  //fails b/c non-numeric string
**********!

~ Eric

__
Eric A. Booth
Public Policy Research Institute
Texas A&M University
[email protected]
Office: +979.845.6754

From: Dani Tilley <[email protected]>
To: stata <[email protected]>
Sent: Thu, July 8, 2010 11:57:05 PM
Subject: st: Referring to elements of a varlist in a program

Hi,

I'm trying to write a program that will automate some commands, but to increase 
its usability I need to include 2 variables in the syntax. I need to figure out 
a way to refer to a user-specified variables and  manipulate them in the body of 

the program. For instance, if the user  were to specify two variables -example 
var1 var2- ,my program would find the mean for each  and divide the sum of the 
means by the standard deviation of the second  variable. (this is just an 
exercise, it doesn't mean anything) 


I've declared the following


program example, rclass
version 10.0
syntax varlist(min=2 max2)

loc m=0
foreach v of varlist `varlist' {
qui su `v',mean
loc m=`m'+`r(mean)'
}

qui su varlist[2] ///I don't know how to refer to this
local sd = `r(sd)'
local final = `m' / `sd'
drop m sd
return local final = `final'

end


Any help is appreciated.

DF Tilley


      
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



      
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index