1

I came across a little puzzle with Stata's locals, display, and quotes.. Consider this example:

generate var1 = 54321 in 1

local test: di %10.0gc var1[1]

Why is the call:

di "`test'" 

returning

54,321

Whereas the call:

di `test'

shows

54 321

What is causing such behaviour?

radek
  • 7,240
  • 8
  • 58
  • 83

1 Answers1

4

Complete the sequence with

(1)

. di 54,321
54 321

(2)

. di "54,231" 
54,321 

display interprets (1) as an instruction to display two arguments, one by one. You get the same result with your last line as (first) the local macro test was evaluated and (second) display saw the result of the evaluation.

The difference when quotation marks are supplied is that thereby you insist that the argument is a literal string. You get the same result with your first display command for the same reasons as just given.

In short, the use of local macros here is quite incidental to the differences in results. display never sees the local macro as such; it just sees its contents after evaluation. So, what you are seeing pivots entirely on nuances in what is presented to display.

Note further that while you can use a display format in defining the contents of a local macro, that ends that story. A local does not have an attached format that sticks with it. It's just a string (which naturally may mean a string with numeric characters).

Nick Cox
  • 35,529
  • 6
  • 31
  • 47