Notice: On April 23, 2014, Statalist moved from an email list to a forum, based at statalist.org.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: st: How to save and use the output of stata command -list-
From
Nick Cox <[email protected]>
To
"[email protected]" <[email protected]>
Subject
Re: st: How to save and use the output of stata command -list-
Date
Thu, 22 Aug 2013 09:16:42 +0100
Roberto Ferrer proposed a much simpler solution, but for future
reference note that there are more problems with your code than you
realise.
Even with the unnecessary loop, you can trim this down. The -replace-
statements wouldn't work as you must use -in `i'-, not subscripts. The
answer to your specific question on -list- is to repeat the same
-summarize- trick used just before.
I haven't tested this, but it should have fewer problems.
g IDclosest = .
forval i = 1/52 {
g gdp_diff = abs(gdp-gdp[`i']) if _n != `i'
su gdp_diff, meanonly
su IDnumber if gdp_diff ==r(min), meanonly
replace IDclosest = r(min) in `i'
drop gdp_diff
}
Roberto's approach remains much better.
Nick
[email protected]
On 22 August 2013 01:21, Md Alauddin Majumder <[email protected]> wrote:
> Hi Nick,
> Thanks for your instructive comments. Let me modify the description of my problem. My dataset is the following:
>
> IDnumber State gdp
>
> 1 Alabama 232
> 2 Alaska 145
> .. .... ...
> .. .... ...
> .. .... ...
> 52 Wyoming 210
>
> I want to create a variable (IDclosest) whose each cell will contain the IDnumber of a state closest to the state in question in terms of gdp. I have constructed the following loop to create the variable:
>
> g IDclosest=.
> forval i=1/52 {
> g gdp`i'=gdp[`i']
> g gdp_diff`i'=abs(gdp-gdp`i')
> replace gdp_diff`i'[`i']=.
> qui sum gdp_diff`i'
> list IDnumber if gdp_diff`i'==r(min)
> replace IDclosest[`i']=???
> drop gdp`i' gdp_diff`i'
> }
>
> The 7th and the 8th lines of the loop are what I need to address. Thanks.
>
>
> Regards
> Majumder
>
>
> ________________________________________
> From: [email protected] [[email protected]] on behalf of Nick Cox [[email protected]]
> Sent: Wednesday, August 21, 2013 5:05 PM
> To: [email protected]
> Subject: Re: st: How to save and use the output of stata command -list-
>
> You have asked several variants of this question already:
>
> http://www.stata.com/statalist/archive/2013-08/msg00697.html
>
> http://www.stata.com/statalist/archive/2013-08/msg00844.html
>
> http://www.stata.com/statalist/archive/2013-08/msg00847.html
>
> and the replies have clearly not helped you. So, first, it seems that
> people aren't clear on the real problem, you don't understand their
> replies, or quite possibly both.
>
> If you don't understand the replies you should be asking more
> questions, or trying to make the problem clearer, or both.
>
> -list- does not save anything. Its main job is to list data. See the
> documentation http://www.stata.com/manuals13/dlist.pdf to understand
> this.
>
> That said, I don't understand your question. In your example, the
> first observation is for Alabama. It makes no obvious sense why you
> want to put a descriptor for Wyoming there.
>
> Regardless, Eric Booth answered essentially the same question
>
> http://www.stata.com/statalist/archive/2013-08/msg00846.html
>
> http://www.stata.com/statalist/archive/2013-08/msg00848.html
>
> but you didn't explain what was wrong with his answer.
>
> We need much more detail from you.
>
> Nick
> [email protected]
>
> On 21 August 2013 22:46, Md Alauddin Majumder <[email protected]> wrote:
>
>> I want to save and subsequently use the output that I find from the command -list-. Can anyone tell how to do that in stata? Let me give an example to further clarify. I have a data set like the following:
>>
>> IDnumber State NewID
>>
>> 1 Alabama .
>> 2 Alaska .
>> .. .... ...
>> .. .... ...
>> .. .... ...
>> 52 Wyoming .
>>
>> If I input -list IDnumber if State=="Wyoming"-, it returns '52'. Now I want to fill the first cell of the variable NewID by this output. How can I write code for that? Thanks in advance.
*
* For searches and help try:
* http://www.stata.com/help.cgi?search
* http://www.stata.com/support/faqs/resources/statalist-faq/
* http://www.ats.ucla.edu/stat/stata/