Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down on April 23, and its replacement, statalist.org is already up and running.


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

Re: st: populating a local macro with a stringvariable


From   Nick Cox <njcoxstata@gmail.com>
To   statalist@hsphsun2.harvard.edu
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-
statement.

That fixed, your code is fine.

. inp str30 alt

                                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"
means!

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.

Nick

On Mon, Dec 19, 2011 at 12:33 PM, Daniel Pilsgaard Henriksen
<henriksen.dp@gmail.com> 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:
*   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   |   Site index