Bookmark and Share

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

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

Re: st: Transforming data - horizontal to vertical

From   Maarten Buis <>
Subject   Re: st: Transforming data - horizontal to vertical
Date   Wed, 11 May 2011 14:39:15 +0200

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
<>) 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"

ds year, not
local varl `r(varlist)'
destring `varl', replace ignore(",") force

foreach var of local varl {
	rename `var' gdp`var'

reshape long gdp, i(year) j(country) string
*--------------------- end example ----------------------------
(For more on examples I sent to the Statalist see: )

Hope this helps,

Maarten L. Buis
Institut fuer Soziologie
Universitaet Tuebingen
Wilhelmstrasse 36
72074 Tuebingen
*   For searches and help try:

© Copyright 1996–2018 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   Site index