Statalist


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

st: RE: Re: How to eliminate the double quotes surrounding letters?


From   "Nick Cox" <n.j.cox@durham.ac.uk>
To   <statalist@hsphsun2.harvard.edu>
Subject   st: RE: Re: How to eliminate the double quotes surrounding letters?
Date   Sat, 29 Aug 2009 16:46:40 +0100

Joseph gave an excellent answer. Let's fill in some of the thought
processes leading to such an answer. 

The generic underlying question is "How do I debug my code?" and,
modifying an old mathematical joke, one answer is "By staring at it
until the correct code becomes obvious". 

We can do a little better than that as generic advice. Some basics of
debugging include: 

1. Read error messages really carefully. If Stata is confused --
meaning, you've confused Stata -- it may not give very informative
messages, but it will do the best it can. 

2. Look (again) at the code really carefully. Read from top to bottom
and from left to right. 

3. Try to see your code as Stata does. Empty your mind of what you want
or what you mean. Stata knows _only_ what you say. Focus on: What does
Stata expect here? What does Stata see here? 

There are other principles too, including 

4. Use -trace- tools to see precisely where a program crashes. 

5. Debug actively, not passively. Vary the code to see what does and
doesn't work. Try simple test cases. See what data or values Stata is
working with. 

-- but the first three, although they may look utterly obvious, get you
a long way, and would have helped Rose to get (closer to) a solution.

1. Read error messages really carefully
=======================================

Stata gave Rose an error message objecting to a left parenthesis "(".
Rose was guessing that the double quotes in what she typed were
problematic. That was not what Stata was objecting to. As it turns out,
the double quotes would also have been problematic, given what Rose
typed, but first things first. So, what is wrong with the "("? 

2. Look (again) at the code really carefully
============================================

Stata should be thought of as like a small child learning to read. It
painstakingly reads each word -- no, each character -- at a time, from
top to bottom and from left to right, making tiny inferential steps one
by one. (It does that really fast, but that's not the issue.) 

There is just one line of code here, so the left-to-right rule is the
only relevant one, but it tells us something: Stata sees nothing wrong
-- in terms of incorrect syntax -- with anything up to and including
"subinstr", but it objects to the "(". 

3. Try to see your code as Stata does
=====================================

Empty your mind of what you want or what you mean. Stata knows _only_
what you say. Focus on: What does Stata expect here? What does Stata see
here?

Look again at the syntax for -rename-. The so-called syntax diagram is
simple, and does not allow for variations: 

rename old_varname new_varname

What does Stata expect, following the -rename- command? An old (meaning
existing) varname followed by a new varname. (If you are really new to
Stata, know that varname means "variable name".) 

Stata is expecting to see a new varname as the third element of the
command, but it is seeing after macro substitution (which I won't
explain here)

subinstr("ab cd ef"," ","",.)

Now "subinstr" could be a legal variable name, but "(" could not be part
of one, so must be (part or all of) some other syntax element. But no
such syntax element is allowed by the syntax, which is why Stata bails
out at this point. At least, that is my reconstruction. 

The underlying error is that Rose wanted or expected -rename- to see the
_result_ of evaluating the expression she typed that uses -subinstr()-.
To get that, she must evaluate it explicitly, either in advance 

local newname = subinstr("`xy'", " ", "", .) 
rename variable `newname' 

or on the fly, as Joseph clearly indicates. 

Nick 
n.j.cox@durham.ac.uk 

Joseph Coveney
==============

Use `=. . .' to evaluate the function.  See example below for details.

Joseph Coveney

. sysuse auto
(1978 Automobile Data)

. keep make

. local xy "ab cd ef"

. rename make `=subinstr("`xy'"," ","",.)'

. describe

Contains data from C:\Program Files\Stata11\ado\base/a/auto.dta
  obs:            74                          1978 Automobile Data
 vars:             1                          13 Apr 2009 17:45
 size:         1,628 (99.9% of memory free)   (_dta has notes)
------------------------------------------------------------------------
--------
------------------------
              storage  display     value
variable name   type   format      label      variable label
------------------------------------------------------------------------
--------
------------------------
abcdef          str18  %-18s                  Make and Model
------------------------------------------------------------------------
--------
------------------------
Sorted by:  
     Note:  dataset has changed since last saved

Rose
====

I have a localname xy containing some letters among which there is
space.

And I want rename a variable the localname xy. Due to the space, I took
the 

-subinstr- funtion to eliminate the space. However, that treatment
produced 

double-quotes surrounding the letters. How to deal with it?


To make what I said clear, I take an example below.

local xy "ab cd ef"

rename variable subinstr("`xy'"," ","",.)


Based on the typing above, Stata gives an error message "( not allowed
r(101)".

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index