Home > database >  Dreaded compound quoting in macros
Dreaded compound quoting in macros

Time:10-29

I think of myself as an advanced Stata user, but I just cannot figure out how to make the compound quotes work for me.

I have two macros, each containing compound quoted lists like `"name with space"' `"nospace"'. I can't figure out how to treat those chunks of string as space-separated lists.

If I create a local with no quotes, it looks like a list separated by a space. E.g.,

 local test a b

. di "`test'"
a b

But if I put them in compound quotes, I get something very different:

. local test `"a"' `"b"'

. di "`test'"
a' invalid name
r(198);

. di `"`test"'
ab

This is a problem if I want to have chunks of strings treated as individual items, as below:

local set1 `"name with space"' `"nospace"'

. di `"`set1'"'
name with spacenospace

How can I make the macro be a list of string chunks (with or without spaces), separated by spaces? I'd ultimately like to iteratively build up such a macro and use it to name rows in a matrix.

CodePudding user response:

I do not know if this answers all your questions. Perhaps edit the questions and remove everything about matrices. As far as I understand your question is about strings and matrices just happens to be the context of your question. But I might be wrong. If your question is about strings only, then a reproducible example focusing on strings would improve your chances on getting a complete answer.

In local set1 `"name with space"' `"nospace"' you concatenate two strings "name with space" and "nospace" but you do not tell Stata to add a space in between those strings. Stata will not add anything in between those strings without you telling it to do so.

You could, for example, do:

local set1 `"name with space"' `" "' `"nospace"'
di `"`set1'"'

or:

local set1 `"name with space"' `" nospace"'
di `"`set1'"'

or save a string of strings:

local set1 `" "name with space" "nospace" "'
di `"`set1'"'
  • Related