Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: expanding numlist


From   "Radu Ban" <[email protected]>
To   [email protected]
Subject   Re: st: expanding numlist
Date   Mon, 2 Jul 2007 11:38:08 -0400

Thanks, this did the trick.

-Radu

2007/7/2, Austin Nichols <[email protected]>:
Radu Ban <[email protected]>:
One way forward is to insert an equals sign to force evaluation rather
than assignment, and use no quotes to trip up -foreach-.  There is
almost certainly a better way of doing this task, though--if you post
a description of the larger problem, you may get more guidance. Note
also you do not need -count-.

input groupid str20 list
 1          "3 8 9 10"
 2         "5/9 11 23/27"
 3        " 6 7/10"
end
 local end = _N
 forvalues i=1/`end' {
 local numbers=list[`i']
 foreach n of numlist `numbers' {
  di `n' /*just to test if this works*/
 }
 }

You can also do the evaluation on the fly:

clear
input groupid str20 list
 1          "3 8 9 10"
 2         "5/9 11 23/27"
 3        " 6 7/10"
end
 forvalues i=1/`=_N' {
 foreach n of numlist `=list[`i']' {
  di `n'
 }
 }


On 7/2/07, Radu Ban <[email protected]> wrote:
> Dear listers,
>
> I have two variables: groupid and list. "groupid" identifies the group
> and "list" is numeric list of members. For example:
>
> groupid list
> 1          3 8 9 10
> 2         5/9 11 23/27
> 3         6 7/10
> ...       ...
>
> Now "list" is always a valid numlist (because I entered it as such). I
> want to get this data into a long format (i.e. expand "list" for each
> "groupid"). But I'm having troubles from the start, I can't even get
> Stata to read in each observation of "list" as a numlist. For example
> I tried the following piece of code.
>
> count
> local end = r(N)
> forvalues i=1/`end' {
>  local numbers "list[`i']"
>  foreach n of numlist `numbers' {
>   di `n' /*just to test if this works*/
>  }
> }
>
> this gives me an error: invalid numlist
>
> Setting the trace on, I see that
> foreach n of numlist `numbers' { is processed as
> foreach n of numlist list[1] { which is clearly an invalid -numlist-
>
> However, when I try this:
>
> count
> local end = r(N)
> forvalues i=1/`end' {
>  local numbers "list[`i']"
>  di `numbers'
> }
>
> I get a list of all the observations in "list", so `numbers' is being
> read as the content of list[`i']. So I am puzzled why `numbers' cannot
> be read as a -numlist-
>
> So my direct question is how to read in "list" as numeric list. I
> think once I do this I can manage to do my reshape.
>
> Thanks,
> Radu
> *
> *   For searches and help try:
> *   http://www.stata.com/support/faqs/res/findit.html
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index