Statalist


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

Re: st: Creating a new variable with information from other observations


From   "Davide Cantoni" <davide.cantoni@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Creating a new variable with information from other observations
Date   Mon, 19 May 2008 14:07:44 +0200

Wonderful, that is just what I was looking for. Thank you, Maarten and Even.

Davide

2008/5/19 Even Bergseng <even.bergseng@umb.no>:
> Can -levelsof- help you?
>
> Best regards,
> Even
>
>
>>-----Original Message-----
>>From: owner-statalist@hsphsun2.harvard.edu [mailto:owner-
>>statalist@hsphsun2.harvard.edu] On Behalf Of Davide Cantoni
>>Sent: Monday, May 19, 2008 1:39 PM
>>To: statalist@hsphsun2.harvard.edu
>>Subject: Re: st: Creating a new variable with information from other
>>observations
>>
>>Thank you, Teresio. That works fine.
>>
>>More generally, could I ask you statalisters again about the last
>>doubt I expressed in my previous email, i.e.:
>>
>>:: is there a way to run a "foreach" over all (numeric) values that a
>>given variable takes, without having to specify exactly the values
>>that this variable takes? Ok, I see, in most cases this would be a job
>>for "by(sort)", but I cannot help thinking that there are some cases
>>in which bysort does not help me -- for example when I want to run
>>more than one command, as I would do within the braces of
>>"foreach...", or when the units the loop runs over (the `X' in
>>"foreach X", so to speak) are used in some logical condition.
>>
>>In brief: is there a way to create a numlist from the unique values
>>that a variable takes? Or how would you proceed?
>>
>>Thanks,
>>
>>Davide
>>
>>
>>
>>2008/5/18 Teresio Poggio <terlist@gmail.com>:
>>> Dear Davide,
>>>
>>> from your dataset I'd build a just capitals dataset:
>>> - select just the capitals (drop if cityid !=capitalid)
>>> - in the new dataset keep just capitalid and latitude
>>> - rename latitude into latitude_capital
>>> - sort the data by capitalid and save it
>>>
>>> then open you original data set and sort it by capitalid,
>>> merge it with the new "just capital dataset" using capitalid as a key
>>> and the option uniqmaster
>>> (help merge for details)
>>>
>>> Hope this helps.
>>>
>>> Teresio
>>>
>>> On Sun, May 18, 2008 at 2:22 PM, Davide Cantoni
>>> <davide.cantoni@gmail.com> wrote:
>>>> Hello,
>>>>
>>>> I am having a rather intricate problem in creating a new variable in
>>a
>>>> panel dataset, and I appreciate any help you could offer. I hope the
>>>> problem can potentially be of general interest.
>>>>
>>>> I have a panel dataset of cities and their characteristics in
>>>> different countries. I know the latitude of each one of these cities,
>>>> but now I want to create an additional variable reflecting the
>>>> latitude of the capital city of the country a given city lies in. So
>>>> for example: for the cities of New York, Chicago, etc., I want this
>>>> new variable to contain the latitude of Washington, DC.
>>>>
>>>> Here is a description of the dataset's structure: it is a panel in
>>>> long form, with cities in different countries, observed over
>>different
>>>> years. Each city has a unique numeric identifier, "cityid". Then
>>there
>>>> is a country identifier, called "countryid". Finally, there is a
>>>> variable that repeats the capital city's cityid for each city in a
>>>> given country, "capitalid". For instance, if the cityid of London was
>>>> 135, all cities in the dataset that are in the UK would get a value
>>of
>>>> 135 in the variable "capitalid". Finally, there is a variable called
>>>> "latitude" that refelcts the latitude of each city.
>>>>
>>>> How would I now proceed to create this new variable, call it
>>>> "latitude_capital", by using the variables above?
>>>>
>>>> Basically, the problem I'm having is
>>>> - tell stata to look up for each city its capitalid
>>>> - browse the dataset until you find a city that has the cityid equal
>>>> to this capitalid
>>>> - find out the latitude of this capital city
>>>> - go back to the original city and replace "latitude_capital" with
>>the
>>>> latitude you've just retrieved
>>>>
>>>> The additional problem I encounter while trying to construct
>>something
>>>> with "foreach..." (that, at least, is what I was trying so far) is
>>>> that the values that the capitalid variable takes are of course not a
>>>> clean numlist (like "1(1)100"), but rather a sequence of numbers
>>>> without any regularity, such as 11 12 50 54 60 131... and so on.
>>>> Thanks for your suggestions!
>>>>
>>>> Davide Cantoni
>>>> *
>>>> *   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/
>>>>
>>>
>>>
>>>
>>> --
>>> ____________________________________________________
>>> dr. Teresio Poggio
>>> LaboR - Dipartimento di Sociologia e ricerca sociale
>>> UniversitÓ degli studi di Trento
>>> Piazza Venezia, 41
>>> 38100 Trento, Italy
>>> Tel +39 0461/881406
>>> fax: +39 0461/881348
>>>
>>> *
>>> *   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/
>

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index