Thanks much to Nick and Scott. I believe the problem was caused, as
Nick suggested, by switching between do files and the command line in
my experimentation. After restarting Stata I get the expected results
Also thanks Nick for the tips on extended list functions and confirm.
Tim
On 1/29/08, Nick Cox <[email protected]> wrote:
> Please ignore my previous. Premature posting.
>
> I guess you are not telling us everything you're doing.
> I did this, and the answer is 6 in both cases:
>
> sysuse auto.dta, clear
> local x
> foreach var of varlist _all {
> local x `x' `var'
> }
> di "`x'"
> di strpos("`x'", "price")
> local y make price
> di strpos("`y'", "price")
>
> It may be that you are doing some things via the do-file editor
> and some things from your command window. Either way, remember
> that locals are precisely that: local macros (which in Stata terminology
> are _not_ variables at all) are local to their context, so you can't see
> a local from outside. "Context" here is a nonce expression for Stata
> program OR main session OR do-file (including do-file editor).
>
> Going beyond the example, using -strpos()- is not a good approach here.
> At some point pretty soon you will be bitten by limits on the length of
> a string in a string expression. See -help limits-.
>
> Using extended list functions is a better way to go on what you started.
>
>
> But even better may be to consider using -confirm-.
>
> Nick
> [email protected]
>
> -----Original Message-----
> From: Nick Cox
> Sent: 29 January 2008 20:20
> To: '[email protected]'
> Subject: RE: store variable names in local macro
>
>
> The name of the problem is limits. You want to feed a -strpos()-
>
> -----Original Message-----
> From: [email protected]
> [mailto:[email protected]] On Behalf Of Tim Wade
> Sent: 29 January 2008 20:03
> To: [email protected]
> Subject: st: store variable names in local macro
>
> 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/
>
> *
> * 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/
>
*
* 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/