Bookmark and Share

Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at

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

Re: st: populating a local macro with a stringvariable

From   Nick Cox <>
Subject   Re: st: populating a local macro with a stringvariable
Date   Mon, 19 Dec 2011 12:57:52 +0000

You (say you) are creating a variable -len- and then -sort-ing on
-length-. That won't work unless there is a variable -length- that you
created previously -- or unless you are misreporting your -generate-

That fixed, your code is fine.

. inp str30 alt

  1.  "a"
  2.  "aaa"
  3.  "b"
  4.  "bb"
  5.  "c"
  6.  "d"
  7.  "e"
  8.  "f"
  9.  "g"
 10.  end

. gen length = length(alt)

. sort length

. levelsof alt, loc(i)
`"a"' `"aaa"' `"b"' `"bb"' `"c"' `"d"' `"e"' `"f"' `"g"'

. di `"`i'"'
`"a"' `"aaa"' `"b"' `"bb"' `"c"' `"d"' `"e"' `"f"' `"g"'

I suspect that your problem is one that you are not explaining, namely
that you are doing some of this interactively via the command window
and some via a do-file editor window. However, -local- macros are
indeed _local_ to the locale in which they are defined, where "locale"
means program, do-file editor, do-file on main interactive session.
So, one locale cannot see another's local macros. That's what "local"

The word "locale" here is not, so far as I know, used in Stata's
documentation, but is used here to explain the problem.

If this is not the problem you need to tell us more about what "not
showing up" means.


On Mon, Dec 19, 2011 at 12:33 PM, Daniel Pilsgaard Henriksen
<> wrote:

> I'm new to using macros in stata, and I'm having trouble populating
> local macro with a variable containing strings sorted by their length
> instead of their order of apperence. Here's an example of my problem
> inp str30 alt
> "a"
> "aaa"
> "b"
> "bb"
> "c"
> "d"
> "e"
> "f"
> "g"
> end
> gen len = length(alt)
> sort length
> levelsof alt, loc(i)
> di `"`i'"'
> I'm sure there're a easy way of doing this, but I've tried for some
> time now and it's not showing up.
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index