Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

st: store variable names in local macro


From   "Tim Wade" <[email protected]>
To   [email protected]
Subject   st: store variable names in local macro
Date   Tue, 29 Jan 2008 15:02:31 -0500

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/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index