Bookmark and Share

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


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

Re: st: populating a local macro with a stringvariable


From   Matthew White <[email protected]>
To   [email protected]
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
<[email protected]> 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–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index