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: Transforming data - horizontal to vertical


From   lreine ycenna <lreine.ycenna@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Transforming data - horizontal to vertical
Date   Wed, 11 May 2011 16:02:24 +0100

Hello,
This is the error message I've been getting:' invalid name
r(198);

 Also the number '2' appeared after I entered this command ' foreach
var of local countries { '


 ds year, not
. local countries 'r(varlist)'

. foreach var of local countries {
  2. rename 'var' country'var'
  3. }
' invalid name
r(198);


On 11 May 2011 15:40, lreine ycenna <lreine.ycenna@gmail.com> wrote:
> Hello,
> I did the command you suggested. However, right after I added the
> prefix 'country' to my list of countries using this foreach command (3
> separate lines of command, which I enter one line at a time), there's
> an error.
>
> foreach var of local varl {
> rename `var' country `var'
>  }
>
> stata gave me an error message ' afghanistan not allowed (101);
>
> I think I might have encountered some bottlenecks!  Must I rename and
> destring them before reshaping? I've tried the same command many times
> but the error message appears each time.
>
> Is there any other way to reshape data? Perhaps a dummy guide to
> reshape...Sorry about that. I don't know what's gone wrong.
>
>
> year    Afghanistan     Albania Algeria
> 1977    2,953,333,419   ..      20,971,901,063
> 1978    3,300,000,108   ..      26,364,491,675
> 1979    3,697,940,345   ..      33,243,421,339
> 1980    3,641,723,447   ..      42,345,276,288
> 1981    3,478,787,910   ..      44,348,670,724
>
>
> ====>the shape wanted:
> year    country                   gdp
> 1977    Afghanistan     2,953,333,419
> 1978    Afghanistan     3,300,000,108
> 1979    Afghanistan     3,697,940,345
> 1980    Afghanistan     3,641,723,447
> 1981    Afghanistan     3,478,787,910
> 1977    Albania ..              3,641,723,447
> 1978    Albania ..              3,641,723,447
> 1979    Albania ..              3,641,723,447
> 1980    Albania ..              3,641,723,447
> 1981    Albania ..              3,641,723,447
>
>
>
>
> ds year, not
> local varl `r(varlist)'
> destring `varl', replace ignore(",") force
> list
>
> foreach var of local varl {
> rename `var' country `var'
>  }
>
> label def country `i' "`var'", add
>
>
> reshape long year, i(year) j(country) string
>> list
>
>
>
> On 11 May 2011 13:39, Maarten Buis <maartenlbuis@gmail.com> wrote:
>> On Wed, May 11, 2011 at 1:48 PM, lreine ycenna wrote:
>>
>>> I tried the command ds gdp year, not local countries `r(gdp)', but
>>> stata tells me option local is not allowed.
>>
>> Looks like you're email program is not displaying statalist messages
>> correctly -ds gdp year, not- and -local countries `r(gdp)'- are two
>> different commands. Look at the statalist archives (for example
>> <http://www.hsph.harvard.edu/cgi-bin/lwgate/STATALIST/archives>) for
>> correctly formatted posts.
>>
>>> am i supposed to create a variable called ' countries'?
>>
>> No, `countries' (in general anything surrounded by a " ` " and a " '
>> ") is a local macro. In this case, you can think of this as a place
>> where you can store lists of variable names. In general, it is just a
>> place where you can store any string. See: -help macro-. Given that
>> this is a local macro, you will need to run the definition of the
>> local and any subsequent command that uses it in one go.
>>
>> Below is one more example using the data you sent. It now assumes that
>> you have only one variable. It takes care of the commas using
>> -destring-. If you have multiple variables you just combine the tricks
>> that have already appeared in this thread.
>>
>> *--------------------- begin example ------------------------
>> clear all
>> input year str12 Afghanistan str12 Albania str12 Algeria
>> 1977    "275,820,910"     ".."      "1,683,573,383"
>> 1978    "390,555,036"     ".."      "1,980,500,369"
>> 1979    "441,210,990"     ".."      "2,658,756,710"
>> 1980    "371,168,398"     ".."      "3,772,614,849"
>> 1981    "274,293,425"     ".."      "3,695,345,372"
>> end
>>
>> ds year, not
>> local varl `r(varlist)'
>> destring `varl', replace ignore(",") force
>> list
>>
>> foreach var of local varl {
>>        rename `var' gdp`var'
>> }
>>
>> reshape long gdp, i(year) j(country) string
>> list
>> *--------------------- end example ----------------------------
>> (For more on examples I sent to the Statalist see:
>> http://www.maartenbuis.nl/example_faq )
>>
>> Hope this helps,
>> Maarten
>>
>> --------------------------
>> Maarten L. Buis
>> Institut fuer Soziologie
>> Universitaet Tuebingen
>> Wilhelmstrasse 36
>> 72074 Tuebingen
>> Germany
>>
>>
>> http://www.maartenbuis.nl
>> --------------------------
>> *
>> *   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/
>>
>

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