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
Nick Cox <[email protected]>
To
"'[email protected]'" <[email protected]>
Subject
RE: st: populating a local macro with a stringvariable
Date
Mon, 19 Dec 2011 18:09:05 +0000
-valuesof- (SSC) lists distinct values in order of occurrence in the dataset, so applying -valuesof- after a -sort- on length is another way to proceed.
Nick
[email protected]
Daniel Pilsgaard Henriksen
Dear Nick and Matt
Thank you for your replies.
I'm sorry for not being clear about the problem, and for the typo in my code (length vs len), I'm glad you could decipher it in spite of that.
Thank you Matt, this is exactly what I needed!!
Cheers!!
2011/12/19 Matthew White <[email protected]>:
> 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.
*
* 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/