Statalist


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

st: (Il)Legal variable/macro names?


From   "Schaffer, Mark E" <[email protected]>
To   <[email protected]>
Subject   st: (Il)Legal variable/macro names?
Date   Thu, 25 Oct 2007 13:23:22 +0100

Hi everybody.  I've just been bitten by an odd inconsistency between what constitutes a legal name for a variable and a legal name for a macro.  8-bit ascii characters are apparently legal in variable names, but when used in a macro name, no macro is created.

Here's an example using the auto dataset.  The first part shows that the variable name u� is legal.  The second part shows that when I try to use -tempvar- to create a macro called "u�", nothing is created - when Stata gets to the next line, macro substitution means `u�' becomes ... nothing.

. do "C:\DOCUME~1\MARKSC~1\LOCALS~1\Temp\STD0l000000.tmp"

. sysuse auto, clear
(1978 Automobile Data)

. 
. capture program drop legalnames

. program define legalnames
  1. gen u� = mpg
  2. sum u�
  3. tempvar u�
  4. gen `u�' = mpg
  5. sum `u�'
  6. end

. 
. set trace on

. legalnames
  ----------------------------------------------- begin legalnames ---
  - gen u� = mpg
  - sum u�

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
          u� |        74     21.2973    5.785503         12         41
  - tempvar u�
  - gen `u�' = mpg
  = gen  = mpg
too few variables specified
  ------------------------------------------------- end legalnames ---
r(102);

end of do-file
r(102);


I can't find anything about this in the manuals, but the behavior of -tempvar- does look bug-like - if an illegal macro name is used, shouldn't -tempvar- complain?

In programs I sometimes generate macro names based on variable names, so if the naming rules are actually different for variable names and macro names, this is not a good strategy.

--Mark

Stata 9.2
Windows XP


Prof. Mark Schaffer
Director, CERT
Department of Economics
School of Management & Languages
Heriot-Watt University, Edinburgh EH14 4AS
tel +44-131-451-3494 / fax +44-131-451-3296
email: [email protected]
web: http://www.sml.hw.ac.uk/ecomes

*
*   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