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 at the end of May, 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 <n.j.cox@durham.ac.uk>
To   "'statalist@hsphsun2.harvard.edu'" <statalist@hsphsun2.harvard.edu>
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 
n.j.cox@durham.ac.uk 

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 <mwhite@poverty-action.org>:
> 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.

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