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   Matthew White <mwhite@poverty-action.org>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: populating a local macro with a stringvariable
Date   Mon, 19 Dec 2011 08:39:01 -0500

Hi Daniel,

I may be misunderstanding, but it sounds like you want the elements of
local `i' to be sorted by the length of the element. I think Mata
might be the easiest way to do this:

levelsof alt, loc(i)
mata:
levels = tokens(st_local("i"))'
st_local("i", invtokens(J(1, rows(levels), "`" + `"""') +
collate(levels, order((strlen(levels), (1::rows(levels))), (1, 2)))' +
J(1, rows(levels), `"""' + "'")))
end

Best,
Matt

On Mon, Dec 19, 2011 at 7:33 AM, Daniel Pilsgaard Henriksen
<henriksen.dp@gmail.com> wrote:
> Dear Statalist
>
> 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.
>
> Cheers
> Daniel
> --
> Daniel Henriksen
> Ph.d. studerende, læge
> Infektionsmedicinsk afd Q / Akut Modtage Afdelingen
> Odense Universitetshospital
>
> *
> *   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/



-- 
Matthew White
Data Coordinator
Innovations for Poverty Action
101 Whitney Avenue, New Haven, CT 06510 USA
+1 434-305-9861
www.poverty-action.org

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