I would do it like this:
local price "price"
sysuse auto, clear
unab vars: _all
local ok : list price in vars
if `ok' {
do whatever you want to do with price
}
sysuse nlsw88, clear
unab vars: _all
local ok : list price in vars
if `ok' {
in this dataset price is absent
so this block won't be executed
}
Hope this helps,
Maarten
--- Tim Wade <[email protected]> wrote:
> Hi Statalisters:
>
> I have a series of datasets with many variables but some variables
> are
> missing from some data sets. I would like to evaluate the data sets
> to
> see if certain variables are present before executing some code. My
> idea was to store all the names of the variables in a dataset in a
> macro variable, then evaluate that macro variable using "strpos" to
> see if the variable is present in the list, ending the code if strpos
> returns 0. However, "strpos" does not seem to be able to evaluate my
> macro variable. I have a feeling my problem may be the brute force
> way
> I am creating the macro (see below). When I manually create a local
> variable, it works fine. Can anyone suggest a better approach?
> Thanks
> much in advance for any suggestions!
>
> For example:
>
> . sysuse auto.dta
> (1978 Automobile Data)
>
> /*create local macro with list of variable names*/
> . local x ""
>
> . foreach var of varlist _all {
> 2. local x "`x'" " " "`var'"
> 3. }
>
> . di "`x'"
> make price mpg rep78 headroom trunk weight length turn displacement
> gear_ratio foreign
>
> . di strpos("`x'", "price")
> 0
>
> *"price" is not found by the "strpos" function
>
> /*create local variable manually*/
>
> . local y make price
>
> . di strpos("`y'", "price")
> 6
>
> Tim
> *
> * For searches and help try:
> * http://www.stata.com/support/faqs/res/findit.html
> * http://www.stata.com/support/statalist/faq
> * http://www.ats.ucla.edu/stat/stata/
>
-----------------------------------------
Maarten L. Buis
Department of Social Research Methodology
Vrije Universiteit Amsterdam
Boelelaan 1081
1081 HV Amsterdam
The Netherlands
visiting address:
Buitenveldertselaan 3 (Metropolitan), room Z434
+31 20 5986715
http://home.fsw.vu.nl/m.buis/
-----------------------------------------
__________________________________________________________
Sent from Yahoo! Mail - a smarter inbox http://uk.mail.yahoo.com
*
* For searches and help try:
* http://www.stata.com/support/faqs/res/findit.html
* http://www.stata.com/support/statalist/faq
* http://www.ats.ucla.edu/stat/stata/